-
[JAVA/SPRING] 어노테이션을 이용한 스프링 설정 – 2개발/Java 2019. 3. 1. 15:11
스프링 설정파일을 하나로 하는 것은 유지보수의 측면에서 비효율적
기능별로 분리하는게 효율적
타 파일의 객체가 필요해서 메소드를 호출해야하는 경우에는, 프로퍼티를 선언해서 @Autowired로 자동주입을 한 후, 메소드가아닌 자동주입된 프로퍼티를 명시하면 된다.
여러개의 스프링 설정파일은, 배열타입으로 넣어준다. (MemberConfig1.class, MemberConfig2.class, MemberConfig3.class)
// MemberConfig1.java
public class MemberConfig1 {
public StudentDao studentDao() {
return new StudentDao();
}
public StudentRegisterService registerService() {
return new StudentRegisterService(studentDao());
}
public StudentModifyService modifyService() {
return new StudentModifyService(studentDao());
}
public StudentSelectService selectService() {
return new StudentSelectService(studentDao());
}
public StudentDeleteService deleteService() {
return new StudentDeleteService(studentDao());
}
public StudentAllSelectService allSelectService() {
return new StudentAllSelectService(studentDao());
}
}// MemberConfig2.java
public class MemberConfig2 {
public DataBaseConnectionInfo dataBaseConnectionInfoDev() {
DataBaseConnectionInfo infoDev = new DataBaseConnectionInfo();
infoDev.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:xe");
infoDev.setUserId("scott");
infoDev.setUserPw("tiger");
return infoDev;
}
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
public class MemberConfig3 {
DataBaseConnectionInfo dataBaseConnectionInfoDev;
DataBaseConnectionInfo dataBaseConnectionInfoReal;
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
MemberConfig2.class, MemberConfig3.class}) ({
public class MemberConfigImport {
public StudentDao studentDao() {
return new StudentDao();
}
public StudentRegisterService registerService() {
return new StudentRegisterService(studentDao());
}
public StudentModifyService modifyService() {
return new StudentModifyService(studentDao());
}
public StudentSelectService selectService() {
return new StudentSelectService(studentDao());
}
public StudentDeleteService deleteService() {
return new StudentDeleteService(studentDao());
}
public StudentAllSelectService allSelectService() {
return new StudentAllSelectService(studentDao());
}
}AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MemberConfigImport.class);
'개발 > Java' 카테고리의 다른 글
[JAVA/SPRING] 어노테이션을 이용한 스프링 설정 – 1 (0) 2019.02.28 [JAVA/SPRING] 생명주기 (0) 2019.02.28 [JAVA/SPRING] 의존객체 선택 (0) 2019.02.28 [JAVA/SPRING] 의존객체 자동 주입 (0) 2019.02.27 [JAVA/SPRING] 스프링 설정 파일 분리 (0) 2019.02.26