component-scan

2024. 3. 11. 17:33Daily Codig Reminder

component-scan

 

public interface DBDao {

public abstract List<String> list();
}

 

@Repository //component -scan   이용 생성
public class DBOracleDAO implements DBDao {
	public List<String> list(){
		return Arrays.asList("oracle 홍길동","oracle 이순신");
	}
}

 

public interface DBService {
public abstract List<String> list();
}

 

 

@Service("myService")
public class DBOracleService {
@Autowired
DBDao dao;

public List<String> list(){
	return this.dao.list();
}

public DBDao getDao() {
	return dao;
}

public void setDao(DBDao dao) {
	this.dao = dao;
}

public DBOracleService(DBDao dao) {
	super();
	this.dao = dao;
}

public DBOracleService() {
	super();
	// TODO Auto-generated constructor stub
}


}

 

@SpringBootApplication
public class Boot05Di4CollectionList3Application {

	public static void main(String[] args) {
		ApplicationContext ctx = SpringApplication.run(Boot05Di4CollectionList3Application.class, args);
		DBOracleService service = ctx.getBean("myService",DBOracleService.class);
		List<String> list = service.list();
		System.out.println(list);
		        }
}

 

 

[oracle 홍길동, oracle 이순신]

 

lifeCycle

@Configuration
public class JavaConfig {
@Bean(value = "myService" , initMethod = "xxx", destroyMethod = "yyy")
public DBService service() {
	return new DBService();
}

}

 

 

public class DBService implements InitializingBean, DisposableBean {
	public void xxx() {
        System.out.println("initMethod.xxx");
    }
    public void yyy() {
        System.out.println("destoryMethod.xxx");
    }
public List<String> list() {
        // TODO Auto-generated method stub
        return Arrays.asList("홍길동", "이순신");
    }
@PostConstruct
public void init() {
	System.out.println("@PostConstruct");
}
@PreDestroy
public void dest() {
	System.out.println("@preDestroy");
}
@Override
public void destroy() throws Exception {
	// TODO Auto-generated method stub
	System.out.println("desrtoy");
	
}
@Override
public void afterPropertiesSet() throws Exception {
	// TODO Auto-generated method stub
	System.out.println("afterProperties");
	
}
}

 

@SpringBootApplication
public class Boot05Di4CollectionList3Application {

	public static void main(String[] args) {
		ApplicationContext ctx = SpringApplication.run(Boot05Di4CollectionList3Application.class, args);
		DBService service = ctx.getBean("myService",DBService.class);
		List<String> list = service.list();
		System.out.println(list);
		        }
}

 

@PostConstruct

afterProperties

initMethod.xxx

[2m2024-02-26 09:58:13.677[0;39m [32m INFO[0;39m [35m12260[0;39m [2m---[0;39m [2m[ main][0;39m [36mc.e.Boot05Di4CollectionList3Application [0;39m [2m:[0;39m Started Boot05Di4CollectionList3Application in 0.537 seconds (JVM running for 0.956)

[홍길동, 이순신]

@preDestroy

desrtoy

destoryMethod.xxx

 

externalizeConfig1@Value

application- properties

user.username=\uD64D\uAE38\uB3D9
user.age=20

 

User dto

public class User {
@Value("${user.username}")
String username;
@Value("${user.age}")
int age;

@Override
public String toString() {
	return "User [username=" + username + ", age=" + age + "]";
}


}

 

 

config

@Configuration
public class JavaConfig {
	// 설정변수, application-properties 설정 = 파일 자동 로딩
@Bean
public User user() {
	return new User();
}

}

 

 

main

@SpringBootApplication
public class Boot05Di4CollectionList3Application {

	public static void main(String[] args) {
		ApplicationContext ctx = SpringApplication.run(Boot05Di4CollectionList3Application.class, args);
		User user = ctx.getBean("user",User.class);
		
		System.out.println(user);
		        }
}

User [username=홍길동, age=20]

 

 

externalizeConfig2_Environment

 

public class User {
String username;
int age;


public User() {
	super();
	// TODO Auto-generated constructor stub
}


public String getUsername() {
	return username;
}


public void setUsername(String username) {
	this.username = username;
}


public int getAge() {
	return age;
}


public void setAge(int age) {
	this.age = age;
}


public User(String username, int age) {
	super();
	this.username = username;
	this.age = age;
}


@Override
public String toString() {
	return "User [username=" + username + ", age=" + age + "]";
}


}

 

@Configuration
public class JavaConfig {
	// 설정변수, application-properties 설정 = 파일 자동 로딩
@Autowired
Environment env;
	@Bean("myUser")
public User service() {
	return new User(env.getProperty("user.username"),Integer.parseInt(env.getProperty("user.age")));
}

}

 

 

user.username=\uD64D\uAE38\uB3D9
user.age=20

 

@SpringBootApplication
public class Boot05Di4CollectionList3Application {

	public static void main(String[] args) {
		ApplicationContext ctx = SpringApplication.run(Boot05Di4CollectionList3Application.class, args);
		User user = ctx.getBean("myUser",User.class);
		
		System.out.println(user);
		        }
}

User [username=홍길동, age=20]

 

 

 

 @ConfigurationProperties

 

MyConfigurationProperties

@ConfigurationProperties(prefix = "user") //scan 으로 자동 생성됨. @autowired 됨, main에서 로딩 필요함
public class MyConfigurationProperties { //application.properties 파일의 환경변수 로딩 저장
	String username;
	int age;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	

}

 

javaconfig

@Configuration
public class JavaConfig {
@Autowired
MyConfigurationProperties p;
//main 에서 @EnableConfigurationProperties(MyConfigurationProperties.class)
	@Bean("myUser")
public User service() {
	return new User(p.getUsername(),p.getAge());
}

}

 

application 이랑 main 같음

User [username=홍길동, age=20]

 

 

@PropertySource1_@Value

@Configuration
@PropertySource(value="classpath:jdbc.properties")
public class JavaConfig {
	// 설정변수, application-properties 설정 = 파일 자동 로딩
@Bean
public User user() {
	return new User();
}

}

 

jdbc.proties

user.username=\uD64D\uAE38\uB3D9
user.age=20

User [username=홍길동, age=20]

 

 

@PropertySource2_Environment

@Configuration
@PropertySource(value = "classpath:jdbc.properties")
public class JavaConfig {
	// 설정변수, application-properties 설정 = 파일 자동 로딩
@Autowired
Environment env;
	@Bean("myUser")
public User service() {
	return new User(env.getProperty("user.username"),Integer.parseInt(env.getProperty("user.age")));
}

}

classpath 추가 하기

 

 

ConfigurationProperties

@ConfigurationProperties(prefix = "user")
public class MyConfigurationProperties {
String username;
int age;
public String getUsername() {
	return username;
}
public void setUsername(String username) {
	this.username = username;
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}
@Override
public String toString() {
	return "MyConfigurationProperties [username=" + username + ", age=" + age + "]";
}
public MyConfigurationProperties(String username, int age) {
	super();
	this.username = username;
	this.age = age;
}
public MyConfigurationProperties() {
	super();
	// TODO Auto-generated constructor stub
}


}

 

 

 

@Configuration
//@PropertySource(value="classpath:jdbc.properties")
public class JavaConfig {
	@Autowired
	MyConfigurationProperties p;
@Bean("myUser")
public User user() {
	return new User(p.getUsername(),p.getAge());
}

}
@SpringBootApplication
@PropertySource(value = "jdbc.properties")
@EnableConfigurationProperties(MyConfigurationProperties.class)
public class Boot08externalizeConfig4PropertySource3ConfigurationPropertiesApplication {

	public static void main(String[] args) {
		ApplicationContext ctx = SpringApplication.run(Boot08externalizeConfig4PropertySource3ConfigurationPropertiesApplication.class, args);
		User user = ctx.getBean("myUser", User.class);
		System.out.println(user);
	}

}
User [username=홍길菙, age=30]

 

 

javaconfig 여러개/한개 /없이

 

여러개

config1

@Configuration
@Profile("prod")
public class JavaConfig {
	
	@Bean("xxx")
	public DBOracleDAO dao() {//coc사용.(함수명이 dao와 같으면 자동으로 찾아감)
		return new DBOracleDAO();
	}

}//end class

 

 

config2

@Configuration
@Profile("dev")
public class JavaConfig2 {
	
	@Bean("yyy")
	public DBMYSQLDAO dao2() {//coc사용.(함수명이 dao와 같으면 자동으로 찾아감)
		return new DBMYSQLDAO();
	}

	
}//end class

 

 

service

@Service("myService")
public class DBService {
	
	@Autowired//byType
	DBDao dao;
	
	public List<String> list() {
		return dao.list();
	}

	public DBDao getDao() {
		return dao;
	}

	public void setDao(DBDao dao) {
		this.dao = dao;
	}

	public DBService() {
		super();
		// TODO Auto-generated constructor stub
	}

	public DBService(DBDao dao) {
		super();
		this.dao = dao;
	}
	
	
	
}//end class

 

 

application.properties

spring.profiles.active=dev 
//prod 로 바꾸면 결과가 다르게 나옴

[mysql 홍길동2, mysql 이순신2]

 

 

한개

하나의 config엣 해결하기

@Configuration

public class JavaConfig {
	
	@Bean("xxx")
	@Profile("prod")
	public DBOracleDAO dao() {//coc사용.(함수명이 dao와 같으면 자동으로 찾아감)
		return new DBOracleDAO();
	}
	
	@Bean("yyy")
	@Profile("dev")
	public DBMYSQLDAO dao2() {//coc사용.(함수명이 dao와 같으면 자동으로 찾아감)
		return new DBMYSQLDAO();
	}
	
}//end class

[mysql 홍길동2, mysql 이순신2]

 

 

 

없이

@Repository 만들어 쓰기 config 파일 없애기

@Repository
@Profile("dev")
public class DBMYSQLDAO implements DBDao {

	@Override
	public List<String> list() {
		// TODO Auto-generated method stub
		return Arrays.asList("mysql 홍길동2","mysql 이순신2");
	}

}

 

@Repository
@Profile("prod")
public class DBOracleDAO implements DBDao{

	@Override
	public List<String> list() {
		// TODO Auto-generated method stub
		return Arrays.asList("oracle 홍길동","oracle 이순신");
	}

}

 

 

externalizeConfiguration

main

@SpringBootApplication
public class Boot09Profile4ExternalizeConfigurationApplication {

	public static void main(String[] args) {
		ApplicationContext ctx = SpringApplication.run(Boot09Profile4ExternalizeConfigurationApplication.class, args);
		ServerConnector s = ctx.getBean("myServer",ServerConnector.class);
		
		System.out.println(s);
	}

}

 

 

@Configuration
public class JavaConfig {
@Autowired
Environment env;
public JavaConfig() {
//	System.out.println(p.getProperty("environments.name"));
}
@Bean("myServer")
public ServerConnector getConnect() {
	return new ServerConnector(env.getProperty("environments.url"),
			env.getProperty("environments.name"));
}
}

 

 

public class ServerConnector {
//@Value("${environments.url}")
String url;
//@Value("${environments.name}")
String name;
public String getUrl() {
	return url;
}
public void setUrl(String url) {
	this.url = url;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
@Override
public String toString() {
	return "ServerConnector [url=" + url + ", name=" + name + "]";
}
public ServerConnector(String url, String name) {
	super();
	this.url = url;
	this.name = name;
}
public ServerConnector() {
	super();
	// TODO Auto-generated constructor stub
}



}

 

 

#default

spring.profiles.active=dev
environments.url=http://localhost:8081/
environments.name=spring boot local

 

 

#dev

environments.url=http://192.168.0.1/
environments.name=spring boot dev

 

 

#prod
environments.url=http://192.168.0.2/
environments.name=spring boot prod

'Daily Codig Reminder' 카테고리의 다른 글

boot-mybatis  (0) 2024.03.12
boot-database  (0) 2024.03.11
list, map, @Autowired  (1) 2024.02.28
springboot  (1) 2024.02.28
myBatis2  (0) 2024.02.16