학습하며 정리한 내용을 올리는 것이니, 참고용으로만 봐 주시길 바랍니다.
자바 스프링 프레임워크(renew ver.) - 신입 프로그래머를 위한 강좌
스프링컨테이너 생명주기
- 생성: 스프링컨테이너와 빈 객체들이 생성되고 주입(의존관계)된다.
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:appCtx.xml");
사용
: `BookRegisterService bookRegisterService = ctx.getBean("bookRegisterService", BookRegisterService.class);소멸
:ctx.close();
Bean 객체 생성/제거 시 특정작업을 수행하도록 하기
: 해당 객체가 인증절차(DB연결, 어떤 작업으로 인한인증작업) 시 주로 사용된다.
- 인터페이스 이용하기
public class BookRegisterService implements InitializingBean, DisposableBean {
@Autowired
private BookDao bookDao;
@Override
public void afterPropertiesSet() throws Exception {
System.out.prinltn("Bean 객체 생성 시");
}
@Override
public void destory() throws Exception {
System.out.println("Bean 객체 소멸 시");
}
}
- InitializingBean, DisposableBean 인터페이스를 상속받는다.
- 상속받은 인터페이스의 afterPropertiesSet, destory 함수를 구현한다.
- 스프링 속성 이용하기
<bean id="memberRegisterService" class="com.member.dao.MemberRegisterService" init-method="initMethod" destroy-method="destroyMethod" />
...
public class MemberRegisterService {
...
public void initMethod() {
}
public void destroyMethod() {
}
}
- bean 태그에
init-method
,destroy-method
속성과 함수 이름을 넣어준다. - 해당 Class에 init, destroy 함수를 구현해 준다.
XML 파일이 아닌 Java를 이용해 스프링 설정하기
applicationContext.xml -> MemberConfig.java
이 자바 클래스 파일(MemberConfig.java)이 스프링 설정파일로써 스프링 컨테이너를 만드는 데 사용될 것임을 명시 ->
@Configuration
이 메소드는 Bean객체를 만드는 데 사용될 것임을 명시 ->
@Bean
자바 클래스 파일 명세
@configuration
public class MemberConfig {
/* 기본 Bean 객체 생성하기 */
// <bean id="**studentDao**" class="*ems.member.dao.StudentDao*" />
@Bean
public *StudentDao* **studentDao**() {
return new StudentDao();
}
/* DI가 있는 Bean 객체 생성하기 */
/*
<bean id="registerService" class="ems.member.service.StudentRegisterService">
<constructor-arg ref="studentDao"></constructor-arg>
</bean>
*/
@Bean
public StudentRegisterService registerService() {
return new StudentRegisterService(studentDao());
}
/* setter가 있는 Bean 객체 생성하기 */
/*
<bean id="dataBaseConnectionInfoDev" class="...DataBaseConnectionInfo">
<property name="jdbcUrl" value="jdbc:oracle:...">
</bean>
*/
@Bean
public DataBaseConnectionInfo dataBaseConnectionInfoDev() {
DataBaseConnectionInfo infoDev = new DataBaseConnectionInfo();
infoDev.setJdbcUrl("jdbc:oracle:...");
return infoDev;
}
/* setter에 다른 Bean객체를 넣는? Bean 객체 생성하기 */
/*
<bean id="...">
<property name="dbInfos">
<map>
<entry>
<key>
<value>name</value>
</key>
<value>Jay</value>
</entry>
<entry>
<key>
<value>dev</value>
</key>
<ref bean="dataBaseConnectionInfoDev" />
</entry>
</map>
</property>
*/
@Bean
...
dbInfos.put("name", "Jay");
dbInfos.put("dev", dataBaseConnectionInfoDev());
info.setDbInfos(dbInfos);
}
- 스프링 컨테이너 생성하기
/*
GenericXmlApplicationContext ctx =
new GenericApplicationContext("classpath:applicationContext.xml");
*/
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext(MemberConfig.class);
Java 파일 분리
: DB관련 Dao
, Service 객체
, DB관련 기능
, Utils
로 보통 분리한다.
- 분리된 자바 파일은 다음과 같이 comma를 이용해 여러개를 불러올 수 있다.
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MemberConfig1.class, MemberConfig2.class, MemberConfig3.class);
- 또는,
MemberConfig1.java
파일 내@Import({MemberConfig2.class, MemberConfig3.class})
를 기입해주고MemberConfig1.class
만 불러오면 된다.
'Backend > Spring' 카테고리의 다른 글
Spring - 데이터베이스 (0) | 2021.02.14 |
---|---|
Spring - 세션과 쿠키 && 리다이렉트와 인터셉트 (0) | 2021.02.09 |
Spring - MVC (0) | 2021.02.04 |
Spring - Bean과 의존객체 주입 (0) | 2021.02.01 |
Spring - Dependency Injection (0) | 2021.01.31 |