2024. 2. 28. 22:03ㆍDaily Codig Reminder
springProfile
<!-- root 로거 기본 설정 -->
<springProfile name="prod">
<root level="trace">
<appender-ref ref="STDOUT" />
<appender-ref ref="DAILYOUT" />
</root>
</springProfile>
<springProfile name="dev">
<root level="info">
<appender-ref ref="STDOUT" />
<appender-ref ref="DAILYOUT" />
</root>
</springProfile>
application.properties
설정값에 따라 log 가 다르게 나옴
spring.profiles.active=prod
#spring-profiles.active=dev
banner
public static void main(String[] args) {
//SpringApplication.run(Boot03Banner01NobannerApplication.class, args);
SpringApplication app = new SpringApplication(Boot03Banner01NobannerApplication.class);
app.setBannerMode(Banner.Mode.OFF); //배너 끄기
app.run(args);//boot app 실행
}
한글설정
spring.banner.location=banner.txt
spring.banner.charset=utf-8
빈등록01다른패키지_명시적등록
main
package com.example;
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Import;
import com.config.JavaConfig;
import com.service.DBService;
@SpringBootApplication
@Import(value=JavaConfig.class) //import , 자동자동실핼, 자동 빈등록
public class Boot04Application {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Boot04Application.class, args);
//SpringApplication.run(Boot04Application.class, args);
//DBService service = new DBService();
//System.out.println(service);
DBService service = ctx.getBean("myService", DBService.class);
List<String> list= service.getList();
System.out.println(list);
}
}
service
package com.service;
import java.util.Arrays;
import java.util.List;
public class DBService {
public DBService() {
super();
System.out.println("service 기본생성자");
}
public List<String> getList(){
return Arrays.asList("홍길동", "이순신");
}
}
config
@Configuration //configurstion.xml과 동일 필수!!
public class JavaConfig {
@Bean("myService") //IDMyService
public DBService service() {
return new DBService();
}
//<bean id="myService" class = "com.service.DBService"/>
}
결과
[2m2024-02-22 10:46:16.844[0;39m [32m INFO[0;39m [35m13792[0;39m [2m---[0;39m [2m[ main][0;39m [36mcom.example.Boot04Application [0;39m [2m:[0;39m Starting Boot04Application using Java 11.0.19 on DESKTOP-SRR9CTJ with PID 13792 (C:\eclipse\stsboot4_stu\workspace\boot04빈등록01다른패키지_명시적등록\target\classes started by acorn in C:\eclipse\stsboot4_stu\workspace\boot04빈등록01다른패키지_명시적등록)
[2m2024-02-22 10:46:16.846[0;39m [32m INFO[0;39m [35m13792[0;39m [2m---[0;39m [2m[ main][0;39m [36mcom.example.Boot04Application [0;39m [2m:[0;39m No active profile set, falling back to 1 default profile: "default"
service 기본생성자
[2m2024-02-22 10:46:17.172[0;39m [32m INFO[0;39m [35m13792[0;39m [2m---[0;39m [2m[ main][0;39m [36mcom.example.Boot04Application [0;39m [2m:[0;39m Started Boot04Application in 0.54 seconds (JVM running for 0.946)
[홍길동, 이순신]
빈등록01다른패키지2_scan
@SpringBootApplication
@ComponentScan("com.*")
public class Boot04Application {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Boot04Application.class, args);
DBService service = ctx.getBean("myService", DBService.class);
List<String> list= service.getList();
System.out.println(list);
}
@Service("myService")
public class DBService {
public List<String> getList(){
return Arrays.asList("홍길동", "이순신");
}
}
@SpringBootApplication(scanBasePackages="com.*")
//@ComponentScan("com.*") //패키지가 다른경우 명시적 component-scan 필요
public class Boot04Application {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Boot04Application.class, args);
DBService service = ctx.getBean("myService", DBService.class);
List<String> list= service.getList();
System.out.println(list);
}
DI_constructor1_method_call
package com.example.service;
import java.util.List;
import com.example.dao.DBOracleDAO;
public class DBService {
DBOracleDAO dao;
public DBOracleDAO getDao() {
return dao;
}
public void setDao(DBOracleDAO dao) {
this.dao = dao;
}
public DBService(DBOracleDAO dao) {
super();
System.out.println("dao 생성자 호출");
this.dao = dao;
}
public List<String> list(){
return this.dao.list();
}
}
package com.example.dao;
import java.util.Arrays;
import java.util.List;
public class DBOracleDAO {
public List<String> list(){
return Arrays.asList("홍길동","이순신");
}
}
package com.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.example.dao.DBOracleDAO;
import com.example.service.DBService;
@Configuration
public class JAVAConfig {
@Bean
public DBOracleDAO dao() {
return new DBOracleDAO();
}
@Bean("myService")
public DBService service() {
return new DBService(dao());
}
}
다른방법
@Configuration
public class JAVAConfig {
@Bean
public DBOracleDAO dao() {
return new DBOracleDAO();
}
@Bean("myService")
public DBService service(DBOracleDAO xxx) {
return new DBService(xxx);
}
}
다른방법2
@Bean
public DBOracleDAO dao() {
return new DBOracleDAO();
}
@Bean("myService")
public DBService service() {
DBOracleDAO dao = new DBOracleDAO();
DBService service = new DBService();
service.setDao(dao);
return service;
}
다른방법3
@Bean
public DBOracleDAO dao() {
return new DBOracleDAO();
}
@Bean("myService")
public DBService service() { //byType 주입
DBService service = new DBService();
service.setDao(dao()); //set 함수이용
return service;
}
다른방법4
@Bean
public DBOracleDAO dao() {
return new DBOracleDAO();
}
@Bean ("myService")
public DBService service (DBOracleDAO xxx) {
DBService service = new DBService();
service.setDao(xxx);
return service;
}
다른방법5
@Configuration
public class JavaConfig {
// @Bean
// public DBOracleDAO dao() {
// return new DBOracleDAO();
// }
//
//두개만들면 가동안됨
//DAO @Repository 로 빈생성
@Bean ("myService")
public DBService service (DBOracleDAO xxx) {
DBService service = new DBService();
service.setDao(xxx);
return service;
}
}
parameter
package com.example;
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Import;
import com.example.config.JavaConfig;
import com.example.service.DBService;
@SpringBootApplication
@Import(value=JavaConfig.class)
public class Boot04Application {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Boot04Application.class, args);
DBService service = ctx.getBean("myService", DBService.class);
List<String> list= service.list();
System.out.println(list);
}
}
package com.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.example.dao.DBOracleDAO;
import com.example.service.DBService;
@Configuration
public class JavaConfig {
// @Bean
// public DBOracleDAO dao() {
// return new DBOracleDAO();
// }
//
//두개만들면 가동안됨
//DAO @Repository 로 빈생성
@Bean ("myService")
public DBService service (DBOracleDAO xxx) {
DBService service = new DBService();
service.setDao(xxx);
return service;
}
}
package com.example.dao;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Repository;
@Repository
public class DBOracleDAO {
public List<String> list(){
return Arrays.asList("홍길동","이순신");
}
}
package com.example.service;
import java.util.List;
import com.example.dao.DBOracleDAO;
public class DBService {
DBOracleDAO dao;
public DBOracleDAO getDao() {
return dao;
}
public void setDao(DBOracleDAO dao) {
this.dao = dao;
}
public DBService(DBOracleDAO dao) {
super();
System.out.println("dao 생성자 호출");
this.dao = dao;
}
public DBService() {
// TODO Auto-generated constructor stub
}
public List<String> list(){
return this.dao.list();
}
}
setter2_method_parameter3_coc
@SpringBootApplication
@Import(value=JavaConfig.class)
public class Boot05Di2Setter2MethodParameter3CocApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Boot05Di2Setter2MethodParameter3CocApplication.class, args);
//config import 안했을때
// DBOracleDAO dao = new DBOracleDAO();
// dao.setDriver("oracle");
// DBOracleService service = new DBOracleService(dao);
// List<String> list = service.list();
// String driver = service.getDriver();
// System.out.println(list);
// System.out.println(driver);
//import 하면
DBOracleDAO dao = ctx.getBean("dao", DBOracleDAO.class);
DBOracleService service = ctx.getBean("myService", DBOracleService.class);
dao.setDriver("oracle");
List<String> list= service.list();
System.out.println(service.getDriver());
System.out.println(service.list());
}
}
package com.example.service;
import java.util.List;
import com.example.dao.DBOracleDAO;
public class DBOracleService {
DBOracleDAO dao;
public DBOracleDAO getDao() {
return dao;
}
public void setDao(DBOracleDAO dao) {
this.dao = dao;
}
public DBOracleService(DBOracleDAO dao) {
super();
System.out.println("dao 생성자 호출");
this.dao = dao;
}
public DBOracleService() {
// TODO Auto-generated constructor stub
}
public List<String> list(){
return this.dao.list();
}
public String getDriver() {
return this.dao.getDriver();
}
}
package com.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.example.dao.DBOracleDAO;
import com.example.service.DBOracleService;
@Configuration
public class JavaConfig {
@Bean
public DBOracleDAO dao() {
DBOracleDAO xxx= new DBOracleDAO();
xxx.setDriver("oracle");
return xxx;
}
@Bean
public DBOracleDAO dao2() {
DBOracleDAO xxx= new DBOracleDAO();
xxx.setDriver("mysql");
return xxx;
}
@Bean ("myService")
public DBOracleService service (DBOracleDAO xxx) {
DBOracleService service = new DBOracleService();
service.setDao(xxx);
return service;
}
}
public class DBOracleDAO {
public String driver="";
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public List<String> list(){
return Arrays.asList("홍길동","이순신");
}
}
singleton: 한 개의 빈 객체 인스턴스가 컨테이너 내에서 오직 하나만 생성되어 공유되는 스코프를 나타냅니다.
즉, 해당 빈 객체는 애플리케이션의 라이프사이클 동안에 한 번만 생성되고,
이후에는 계속해서 동일한 인스턴스를 사용합니다.
prototype: prototype 스코프를 가진 빈은 요청할 때마다 매번 새로운 인스턴스가 생성됩니다.
각각의 요청에 대해 별도의 빈 인스턴스가 생성되어 반환되기 때문에,
이 빈은 싱글톤과는 달리 여러 인스턴스를 가질 수 있습니다.
@Configuration
public class JavaConfig {
@Bean
// @Scope("singleton")
@Scope("prototype")
public DBOracleDAO dao() {
return new DBOracleDAO();
}
@Bean("myService1") //// default Singletone
public DBOracleService service() { // by type
DBOracleService service = new DBOracleService(dao());
return service;
}
// main에서 service getBean 2번 주소 두개 다 출력
@Bean("myService2")
public DBOracleService service2(DBOracleDAO XXX) {
DBOracleService service = new DBOracleService(dao());
return service;
}
}
list
@SpringBootApplication
public class Boot05Di4CollectionListApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Boot05Di4CollectionListApplication.class, args);
DBService service = ctx.getBean("myService", DBService.class);
List<DBDao> list= service.getList();
// List<DBDao> list = new ArrayList<>();
// list.add(new DBOracleDAO());
// list.add(new DBMYSQLDAO());
// DBService service = new DBService(list);
// List<DBDao> list2= service.getList();
System.out.println(list.size());
for (DBDao dao : list) {
List<String> data = dao.list();
System.out.println(data);
}
//System.out.println(dao);
//System.out.println(service);
}
}
@Configuration
public class JavaConfig {
@Bean
public DBOracleDAO dao() {
return new DBOracleDAO();
}
@Bean
public DBMYSQLDAO dao2() {
return new DBMYSQLDAO();
}
@Bean("myService") //// default Singletone
public DBService service(List<DBDao> list) { // by type
return new DBService(list);
}
}
public interface DBDao {
public List<String> list(); //추상함수
}
public class DBMYSQLDAO implements DBDao {
@Override
public List<String> list() {
// TODO Auto-generated method stub
return Arrays.asList("mysql 홍길동2", "mysql 이순신2");
}
}
public class DBOracleDAO implements DBDao {
@Override
public List<String> list() {
// TODO Auto-generated method stub
return Arrays.asList("oracle 홍길동", "oracle 이순신");
}
}
public class DBService {
List<DBDao> list; // 변수의 접근 제어자를 private로 변경
public DBService(List<DBDao> list) {
super();
this.list = list;
}
public DBService() {
// TODO Auto-generated constructor stub
}
public List<DBDao> getList() {
return list;
}
public void setList(List<DBDao> list) {
this.list = list;
}
}
'Daily Codig Reminder' 카테고리의 다른 글
component-scan (0) | 2024.03.11 |
---|---|
list, map, @Autowired (1) | 2024.02.28 |
myBatis2 (0) | 2024.02.16 |
@Reponsebody ,mybatis (0) | 2024.02.16 |
handler, response, json (0) | 2024.02.16 |