-
11장 어노테이션을 이용한 빈 설정스프링 프레임워크 기초 공부 2020. 4. 30. 17:25
@Required
spring 5.1버전부터 사용되지 않는 어노테이션이라 뭔지 알기만 하고 넘어가겠다.
@Aurowired
1)자동 주입 메소드
package kr.co.softcampus.beans; public class DataBean1 { }
DataBean1 생성
package kr.co.softcampus.beans; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Required; public class TestBean1 { private int data1; private DataBean1 data2; public int getData1() { return data1; } public void setData1(int data1) { this.data1 = data1; } public DataBean1 getData2() { return data2; } //자동 주입(타입):매개변수 타입이 DataBean1이라고 정의되어있기때문에 DataBean1 타입으로 정의되어있는 bean객체를 찾아서 자동 주입해줌(DataBean1 타입이 정의되어 있지 않으면 오류발생) @Autowired public void setData2(DataBean1 data2) { this.data2 = data2; } }
TestBean1 생성
package kr.co.softcampus.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import kr.co.softcampus.beans.DataBean1; import kr.co.softcampus.beans.TestBean1; @Configuration public class BeanConfigClass { @Bean public TestBean1 java1() { return new TestBean1(); } @Bean public DataBean1 data_bean1() {//DataBean1 타입으로 만든 bean생성 자동주입가능~! return new DataBean1(); } }
BeanConfigClass(java) 생성
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean 클래스에 설정되어 있는 어노테이션을 이용한다. --> <context:annotation-config/> <bean id='xml1' class='kr.co.softcampus.beans.TestBean1'/> <bean class='kr.co.softcampus.beans.DataBean1'/>DataBean1타입 bean 생성 </beans>
xml파일 생성
package kr.co.softcampus.main; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import kr.co.softcampus.beans.TestBean1; import kr.co.softcampus.config.BeanConfigClass; public class MainClass { public static void main(String[] args) { // TODO Auto-generated method stub ClassPathXmlApplicationContext ctx1 = new ClassPathXmlApplicationContext("kr/co/softcampus/config/beans.xml"); TestBean1 xml1 = ctx1.getBean("xml1",TestBean1.class); System.out.println("xml1.data1:"+xml1.getData1()); System.out.println("xml1.data2:"+xml1.getData2()); ctx1.close(); System.out.println("========================"); AnnotationConfigApplicationContext ctx2 = new AnnotationConfigApplicationContext(BeanConfigClass.class); TestBean1 java1 = ctx2.getBean("java1",TestBean1.class); System.out.println("java1.data1:"+java1.getData1()); System.out.println("java1.data2:"+java1.getData2()); ctx2.close(); } }
출력값
xml1.data1:0
xml1.data2:kr.co.softcampus.beans.DataBean1@175c2241========================
java1.data1:0
java1.data2:kr.co.softcampus.beans.DataBean1@2d52216bdata2의 값이 자동으로 주입된것을 볼 수 있다.
2)자동 주입 변수
package kr.co.softcampus.beans; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Required; public class TestBean1 { private int data1; private DataBean1 data2; //자동 주입을 변수에 설정. //자동으로 setter 메서드가 추가되어 setter메서드를 통해 주입 받게된다. @Autowired private DataBean1 data3; public DataBean1 getData3() { return data3; } public int getData1() { return data1; } public void setData1(int data1) { this.data1 = data1; } public DataBean1 getData2() { return data2; } //자동 주입(타입):매개변수 타입이 DataBean1이라고 정의되어있기때문에 DataBean1 타입으로 정의되어있는 bean객체를 찾아서 자동 주입해줌(DataBean1 타입이 정의되어 있지 않으면 오류발생) @Autowired public void setData2(DataBean1 data2) { this.data2 = data2; } }
private DataBean1 data3 -> 자동 주입을 변수에 설정가능하다. 자동으로 setter 메서드가 추가되어 setter메소드를 생성해줄 필요가없음.
package kr.co.softcampus.main; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import kr.co.softcampus.beans.TestBean1; import kr.co.softcampus.config.BeanConfigClass; public class MainClass { public static void main(String[] args) { // TODO Auto-generated method stub ClassPathXmlApplicationContext ctx1 = new ClassPathXmlApplicationContext("kr/co/softcampus/config/beans.xml"); TestBean1 xml1 = ctx1.getBean("xml1",TestBean1.class); System.out.println("xml1.data1:"+xml1.getData1()); System.out.println("xml1.data2:"+xml1.getData2()); System.out.println("xml1.data3:"+xml1.getData3()); ctx1.close(); System.out.println("========================"); AnnotationConfigApplicationContext ctx2 = new AnnotationConfigApplicationContext(BeanConfigClass.class); TestBean1 java1 = ctx2.getBean("java1",TestBean1.class); System.out.println("java1.data1:"+java1.getData1()); System.out.println("java1.data2:"+java1.getData2()); System.out.println("java1.data3:"+java1.getData3()); ctx2.close(); } }
출력값
xml1.data1:0
xml1.data2:kr.co.softcampus.beans.DataBean1@6c6cb480
xml1.data3:kr.co.softcampus.beans.DataBean1@6c6cb480========================
java1.data1:0
java1.data2:kr.co.softcampus.beans.DataBean1@60bd273d
java1.data3:kr.co.softcampus.beans.DataBean1@60bd273ddata3값에 자동으로 객체가 주입된것을 볼 수 있다.~
@Qualifier
package kr.co.softcampus.beans; public class DataBean2 { }
DataBean2 클래스 생성
package kr.co.softcampus.beans; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Required; public class TestBean1 { private int data1; private DataBean1 data2; //자동 주입을 변수에 설정. //자동으로 setter 메서드가 추가되어 setter메서드를 통해 주입 받게된다. @Autowired private DataBean1 data3; @Autowired @Qualifier("obj4") private DataBean2 data4; @Autowired @Qualifier("obj5") private DataBean2 data5; public DataBean2 getData5() { return data5; } public DataBean2 getData4() { return data4; } public DataBean1 getData3() { return data3; } public int getData1() { return data1; } public void setData1(int data1) { this.data1 = data1; } public DataBean1 getData2() { return data2; } //자동 주입(타입):매개변수 타입이 DataBean1이라고 정의되어있기때문에 DataBean1 타입으로 정의되어있는 bean객체를 찾아서 자동 주입해줌(DataBean1 타입이 정의되어 있지 않으면 오류발생) @Autowired public void setData2(DataBean1 data2) { this.data2 = data2; } }
@Autowired
@Qualifier("obj4")
private DataBean2 data4;자동 주입 변수 선언후 Qualifier 설정
@Autowired
@Qualifier("obj5")
private DataBean2 data5;자동 주입 변수 선언후 Qualifier 설정
package kr.co.softcampus.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import kr.co.softcampus.beans.DataBean1; import kr.co.softcampus.beans.DataBean2; import kr.co.softcampus.beans.TestBean1; @Configuration public class BeanConfigClass { @Bean public TestBean1 java1() { return new TestBean1(); } @Bean public DataBean1 data_bean1() {//DataBean1 타입으로 만든 bean생성 자동주입가능~! return new DataBean1(); } @Bean public DataBean2 obj4() { return new DataBean2(); } @Bean public DataBean2 obj5() { return new DataBean2(); } }
Qualifier 설정한 이름과 같은 Bean obj4와 obj5 생성(java)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean 클래스에 설정되어 있는 어노테이션을 이용한다. --> <context:annotation-config/> <bean id='xml1' class='kr.co.softcampus.beans.TestBean1'/> <bean class='kr.co.softcampus.beans.DataBean1'/> <bean id='obj4' class='kr.co.softcampus.beans.DataBean2'/> <bean id='obj5' class='kr.co.softcampus.beans.DataBean2'/> </beans>
Qualifier 설정한 이름과 같은 Bean obj4와 obj5 생성(xml)
package kr.co.softcampus.main; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import kr.co.softcampus.beans.TestBean1; import kr.co.softcampus.config.BeanConfigClass; public class MainClass { public static void main(String[] args) { // TODO Auto-generated method stub ClassPathXmlApplicationContext ctx1 = new ClassPathXmlApplicationContext("kr/co/softcampus/config/beans.xml"); TestBean1 xml1 = ctx1.getBean("xml1",TestBean1.class); System.out.println("xml1.data1:"+xml1.getData1()); System.out.println("xml1.data2:"+xml1.getData2()); System.out.println("xml1.data3:"+xml1.getData3()); System.out.println("xml1.data4:"+xml1.getData4()); System.out.println("xml1.data5:"+xml1.getData5()); ctx1.close(); System.out.println("========================"); AnnotationConfigApplicationContext ctx2 = new AnnotationConfigApplicationContext(BeanConfigClass.class); TestBean1 java1 = ctx2.getBean("java1",TestBean1.class); System.out.println("java1.data1:"+java1.getData1()); System.out.println("java1.data2:"+java1.getData2()); System.out.println("java1.data3:"+java1.getData3()); System.out.println("java1.data4:"+java1.getData4()); System.out.println("java1.data5:"+java1.getData5()); ctx2.close(); } }
출력값
xml1.data1:0
xml1.data2:kr.co.softcampus.beans.DataBean1@45752059
xml1.data3:kr.co.softcampus.beans.DataBean1@45752059
xml1.data4:kr.co.softcampus.beans.DataBean2@34e9fd99
xml1.data5:kr.co.softcampus.beans.DataBean2@3c41ed1d
========================
4월 30, 2020 5:33:06 오
java1.data1:0
java1.data2:kr.co.softcampus.beans.DataBean1@5824a83d
java1.data3:kr.co.softcampus.beans.DataBean1@5824a83d
java1.data4:kr.co.softcampus.beans.DataBean2@537f60bf
java1.data5:kr.co.softcampus.beans.DataBean2@5677323c'스프링 프레임워크 기초 공부' 카테고리의 다른 글
xml namespace (0) 2020.05.01 12장 AOP (0) 2020.04.30 10장 자바코드를 활용한 bean 만들기 (0) 2020.04.29 9장 DI 자동주입 (0) 2020.04.27 8장 setter 메소드를 통한 주입 (0) 2020.04.27