Spring
230131 Spring xml파일을 java파일로 변경하기
주영재
2023. 1. 31. 18:48
XML파일을 Java파일로 변경하기
기존의 bean을 생성하던 Spring Bean Configuration file을 java파일로도 만들 수 있다.
스프링에선 .xml로, 스프링부트에선 .java로 주로 사용. 물론 서로 바꿔가며 할 수 있다.
자바코드가 더 쉬운 편.
@Configuration -스프링 컨테이너를 대신 생성하는 어노테이션
-클래스에 붙는다.
@Bean -빈으로 등록하는 어노테이션
-메서드에 붙는다.
-이 어노테이션이 붙은 메서드를 스프링 컨테이너가 호출시킨다(빈으로 생성된 것).
<bean id="good" class="test01.SpringTest"/>
==
@Bean
public SpringTest good() {
return new SpringTest();
}
이 둘은 똑같이 작동한다.
의존이라면?
<bean id="chef" class="ex02.Chef"/>
<bean id="hotel" class="ex02.Hotel">
<constructor-arg ref="chef"/>
</bean>
==
@Bean
public Chef chef() {
return new Chef();
}
@Bean
public Hotel hotel() {
//Hotel은 생성자로 Chef객체를 받기 때문에 매개값으로 chef()함수를 주입한다.
return new Hotel(chef());
}
이 방식도 마찬가지로 싱글톤 방식이다. 메서드호출이 계속되는게 아니다.
//java설정으로 만들어진 bean파일은
AnnotationConfigApplicationContext ctx=
new AnnotationConfigApplicationContext(JavaConfig.class);로 가져온다.
*Chef, Hotel, DatabaseDev, MemberDAO는 이전에 생성한 클래스들이다.
Hotel은 Chef와 생성자를 통한 의존관계, DatabaseDev는 3가지 멤버변수와 setter를 가지고 있고, MemberDAO는 setter와 DatabaseDev를 멤버변수로 가진다.
JavaConfig.java
package ex07;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import ex02.Chef;
import ex02.DatabaseDev;
import ex02.Hotel;
import ex02.MemberDAO;
//xml을 대신해서 설정파일로 사용할 때
@Configuration
public class JavaConfig {
//@Bean이 붙은 메서드를 스프링 컨테이너가 호출시킴(빈으로생성)
@Bean
public Chef chef() {
return new Chef();
}
@Bean
public Hotel hotel() {
return new Hotel(chef());
}
@Bean
public DatabaseDev dev() {
DatabaseDev dev=new DatabaseDev();
dev.setUrl("주소...");
dev.setUid("아이디");
dev.setUpw("비밀번호...");
return dev;
}
@Bean
public MemberDAO meberdao() {
MemberDAO mem=new MemberDAO();
mem.setDatabaseDev(dev());
return mem;
}
}
MainClass.java
package ex07;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import ex02.MemberDAO;
public class MainClass {
public static void main(String[] args) {
//java설정으로 만들어진 bean파일은
AnnotationConfigApplicationContext ctx=
new AnnotationConfigApplicationContext(JavaConfig.class);
//System.out.println(ctx.getBeanDefinitionCount());//10개. 내부적으로 동작하는 bean 6개 +만든 빈 4개
MemberDAO dao=ctx.getBean("meberdao",MemberDAO.class);
System.out.println(dao.getDatabaseDev().getUid());
System.out.println(dao.getDatabaseDev().getUrl());
System.out.println(dao.getDatabaseDev().getUpw());
}
}
콘솔출력문
1월 31, 2023 6:45:14 오후 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2f686d1f: startup date [Tue Jan 31 18:45:14 KST 2023]; root of context hierarchy
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 (file:/C:/Users/IBK/.m2/repository/org/springframework/spring-core/5.0.7.RELEASE/spring-core-5.0.7.RELEASE.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
아이디
주소...
비밀번호...