ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JAVA/SPRING] 어노테이션을 이용한 스프링 설정 – 2
    개발/Java 2019. 3. 1. 15:11

    어노테이션을 이용한 스프링 설정 -2

    스프링 설정파일을 하나로 하는 것은 유지보수의 측면에서 비효율적

    • 기능별로 분리하는게 효율적

    Java 파일 분리

    • 타 파일의 객체가 필요해서 메소드를 호출해야하는 경우에는, 프로퍼티를 선언해서 @Autowired로 자동주입을 한 후, 메소드가아닌 자동주입된 프로퍼티를 명시하면 된다.

    • 여러개의 스프링 설정파일은, 배열타입으로 넣어준다. (MemberConfig1.class, MemberConfig2.class, MemberConfig3.class)

    // MemberConfig1.java

    @Configuration
    public class MemberConfig1 {

    @Bean
    public StudentDao studentDao() {
    return new StudentDao();
    }

    @Bean
    public StudentRegisterService registerService() {
    return new StudentRegisterService(studentDao());
    }

    @Bean
    public StudentModifyService modifyService() {
    return new StudentModifyService(studentDao());
    }

    @Bean
    public StudentSelectService selectService() {
    return new StudentSelectService(studentDao());
    }

    @Bean
    public StudentDeleteService deleteService() {
    return new StudentDeleteService(studentDao());
    }

    @Bean
    public StudentAllSelectService allSelectService() {
    return new StudentAllSelectService(studentDao());
    }

    }

    // MemberConfig2.java

    @Configuration
    public class MemberConfig2 {

    @Bean
    public DataBaseConnectionInfo dataBaseConnectionInfoDev() {
    DataBaseConnectionInfo infoDev = new DataBaseConnectionInfo();
    infoDev.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:xe");
    infoDev.setUserId("scott");
    infoDev.setUserPw("tiger");

    return infoDev;
    }

    @Bean
    public DataBaseConnectionInfo dataBaseConnectionInfoReal() {
    DataBaseConnectionInfo infoReal = new DataBaseConnectionInfo();
    infoReal.setJdbcUrl("jdbc:oracle:thin:@192.168.0.1:1521:xe");
    infoReal.setUserId("masterid");
    infoReal.setUserPw("masterpw");

    return infoReal;
    }

    }

    // MemberConfig3.java

    @Configuration
    public class MemberConfig3 {

    @Autowired
    DataBaseConnectionInfo dataBaseConnectionInfoDev;

    @Autowired
    DataBaseConnectionInfo dataBaseConnectionInfoReal;

    @Bean
    public EMSInformationService informationService() {
    EMSInformationService info = new EMSInformationService();
    info.setInfo("Education Management System program was developed in 2015.");
    info.setCopyRight("COPYRIGHT(C) 2015 EMS CO., LTD. ALL RIGHT RESERVED. CONTACT MASTER FOR MORE INFORMATION.");
    info.setVer("The version is 1.0");
    info.setsYear(2015);
    info.setsMonth(1);
    info.setsDay(1);
    info.seteYear(2015);
    info.seteMonth(2);
    info.seteDay(28);

    ArrayList<String> developers = new ArrayList<String>();
    developers.add("Cheney.");
    developers.add("Eloy.");
    developers.add("Jasper.");
    developers.add("Dillon.");
    developers.add("Kian.");
    info.setDevelopers(developers);

    Map<String, String> administrators = new HashMap<String, String>();
    administrators.put("Cheney", "cheney@springPjt.org");
    administrators.put("Jasper", "jasper@springPjt.org");
    info.setAdministrators(administrators);

    Map<String, DataBaseConnectionInfo> dbInfos = new HashMap<String, DataBaseConnectionInfo>();
    dbInfos.put("dev", dataBaseConnectionInfoDev);
    dbInfos.put("real", dataBaseConnectionInfoReal);
    info.setDbInfos(dbInfos);

    return info;
    }

    }

    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MemberConfig1.class, MemberConfig2.class, MemberConfig3.class);

    @Import 어노테이션

    • 하나의 파일에 다른 설정 파일을 import해서, AnnotationConfigApplicationContext에서 해당 파일 하나만 명시해주면 된다.

    // MemberConfigImport.java

    @Configuration
    @Import({MemberConfig2.class, MemberConfig3.class})
    public class MemberConfigImport {

    @Bean
    public StudentDao studentDao() {
    return new StudentDao();
    }

    @Bean
    public StudentRegisterService registerService() {
    return new StudentRegisterService(studentDao());
    }

    @Bean
    public StudentModifyService modifyService() {
    return new StudentModifyService(studentDao());
    }

    @Bean
    public StudentSelectService selectService() {
    return new StudentSelectService(studentDao());
    }

    @Bean
    public StudentDeleteService deleteService() {
    return new StudentDeleteService(studentDao());
    }

    @Bean
    public StudentAllSelectService allSelectService() {
    return new StudentAllSelectService(studentDao());
    }

    }

    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MemberConfigImport.class);


    댓글

Designed by Tistory.