티스토리 뷰
을 통해서 학습했으며, 아래 자료는 위의 자료로부터 가져온 것임을 밝힙니다.
이전 시간까지 xml파일을 이용해서 스프링 설정파일을 만들었다.
스프링에서는 java파일로도 스프링 설정을 할 수가 있다.
@Configuration이라는 어노테이션을 사용하면 된다.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="studentDao" class="ems.member.dao.StudentDao" ></bean>
<bean id="registerService" class="ems.member.service.StudentRegisterService">
<constructor-arg ref="studentDao" ></constructor-arg>
</bean>
<bean id="modifyService" class="ems.member.service.StudentModifyService">
<constructor-arg ref="studentDao" ></constructor-arg>
</bean>
<bean id="deleteService" class="ems.member.service.StudentDeleteService">
<constructor-arg ref="studentDao" ></constructor-arg>
</bean>
<bean id="selectService" class="ems.member.service.StudentSelectService">
<constructor-arg ref="studentDao" ></constructor-arg>
</bean>
<bean id="allSelectService" class="ems.member.service.StudentAllSelectService">
<constructor-arg ref="studentDao" ></constructor-arg>
</bean>
<bean id="dataBaseConnectionInfoDev" class="ems.member.DataBaseConnectionInfo">
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="userId" value="scott" />
<property name="userPw" value="tiger" />
</bean>
<bean id="dataBaseConnectionInfoReal" class="ems.member.DataBaseConnectionInfo">
<property name="jdbcUrl" value="jdbc:oracle:thin:@192.168.0.1:1521:xe" />
<property name="userId" value="masterid" />
<property name="userPw" value="masterpw" />
</bean>
<bean id="informationService" class="ems.member.service.EMSInformationService">
<property name="info">
<value>Education Management System program was developed in 2015.</value>
</property>
<property name="copyRight">
<value>COPYRIGHT(C) 2015 EMS CO., LTD. ALL RIGHT RESERVED. CONTACT MASTER FOR MORE INFORMATION.</value>
</property>
<property name="ver">
<value>The version is 1.0</value>
</property>
<property name="sYear">
<value>2015</value>
</property>
<property name="sMonth">
<value>1</value>
</property>
<property name="sDay">
<value>1</value>
</property>
<property name="eYear" value="2015" />
<property name="eMonth" value="2" />
<property name="eDay" value="28" />
<property name="developers">
<list>
<value>Cheney.</value>
<value>Eloy.</value>
<value>Jasper.</value>
<value>Dillon.</value>
<value>Kian.</value>
</list>
</property>
<property name="administrators">
<map>
<entry>
<key>
<value>Cheney</value>
</key>
<value>cheney@springPjt.org</value>
</entry>
<entry>
<key>
<value>Jasper</value>
</key>
<value>jasper@springPjt.org</value>
</entry>
</map>
</property>
<property name="dbInfos">
<map>
<entry>
<key>
<value>dev</value>
</key>
<ref bean="dataBaseConnectionInfoDev"/>
</entry>
<entry>
<key>
<value>real</value>
</key>
<ref bean="dataBaseConnectionInfoReal"/>
</entry>
</map>
</property>
</bean>
</beans>
이랬던 코드를
package ems.member.configration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import ems.member.DataBaseConnectionInfo;
import ems.member.dao.StudentDao;
import ems.member.service.EMSInformationService;
import ems.member.service.StudentAllSelectService;
import ems.member.service.StudentDeleteService;
import ems.member.service.StudentModifyService;
import ems.member.service.StudentRegisterService;
import ems.member.service.StudentSelectService;
@Configuration
public class MemberConfig {
@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());
}
@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;
}
@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;
}
}
이렇게 바꿀 수가 있다.
보통 XML파일에서
id=" A " class " ~~.~~.~~.B"를
public B A() {
return new B();
}
의 형태로 작성한다.
당연히
//main
// GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:applicationContext.xml");
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MemberConfig.class);
main문이 포함된 코드에서도 GenericXml이 아니라 AnnotationCofig로 바꿔줘야 한다.
개발 환경 : eclipse 2019-12, Oracle, tomcat
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
'웹 > 스프링' 카테고리의 다른 글
웹 프로그래밍 설계 모델 (0) | 2020.06.04 |
---|---|
@Import 자바파일을 분리하여 작성하기 (0) | 2020.05.30 |
@Autowired, @Resource 의존객체 자동 주입 (0) | 2020.05.23 |
@Bean 싱글톤, 프로토타입 (0) | 2020.05.23 |
@Bean 스프링 기본 개념 (0) | 2020.05.23 |
댓글