package kr.co.softcampus.config;
import org.springframework.beans.factory.annotation.Autowire;
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.DataBean3;
import kr.co.softcampus.beans.TestBean1;
import kr.co.softcampus.beans.TestBean2;
import kr.co.softcampus.beans.TestBean3;
//TestBean1.java 파일에서 정의한 변수와 메소드에 값을 맵핑하는 부분
@Configuration
public class BeanConfigClass {
@Bean
public TestBean1 java1() {
return new TestBean1(200,"문자열",new DataBean1());
}
@Bean
public TestBean1 java2() {
TestBean1 t1 = new TestBean1();
t1.setData1(400);
t1.setData2("문자열4");
t1.setData3(new DataBean1());
return t1;
}
@Bean
public DataBean2 data1() {
return new DataBean2();
}
@Bean
public DataBean2 data2() {
return new DataBean2();
}
@Bean(autowire = Autowire.BY_NAME)
public TestBean2 java3() {
return new TestBean2();
}
@Bean
public DataBean3 data100() {
return new DataBean3();
}
@Bean
public TestBean3 java4() {
return new TestBean3();
}
}
직접 주입하는 방법과 자동주입하는 방법 두가지를 보자.!
MainClass.java
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.beans.TestBean2;
import kr.co.softcampus.beans.TestBean3;
import kr.co.softcampus.config.BeanConfigClass;
//TestBean1.java와 BeanConfigClass.java파일에서 정의하고 생성한 변수와 메소드를 실행시켜주는 부분
public class MainClass {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx1 = new ClassPathXmlApplicationContext("kr/co/softcampus/config/beans.xml");
TestBean1 xml1 = ctx1.getBean("xml1",TestBean1.class);
System.out.printf("xml1.data1 : %d\n",xml1.getData1());
System.out.printf("xml1.data2 : %d\n",xml1.getData2());
System.out.printf("xml1.data3 : %d\n",xml1.getData3());
System.out.println("----------------------------");
TestBean1 xml2 = ctx1.getBean("xml2",TestBean1.class);
System.out.printf("xml2.data1 : %d\n",xml2.getData1());
System.out.printf("xml2.data2 : %d\n",xml2.getData2());
System.out.printf("xml2.data3 : %d\n",xml2.getData3());
TestBean2 xml3 = ctx1.getBean("xml3",TestBean2.class);
System.out.printf("xml3.data1 : %s\n",xml3.getData1());
System.out.printf("xml3.data2 : %s\n",xml3.getData2());
TestBean3 xml4 = ctx1.getBean("xml4",TestBean3.class);
System.out.printf("xml4.data1 : %s\n",xml4.getData1());
System.out.printf("xml4.data2 : %s\n",xml4.getData2());
ctx1.close();
System.out.println("============================");
AnnotationConfigApplicationContext ctx2 = new AnnotationConfigApplicationContext(BeanConfigClass.class);
TestBean1 java1= ctx2.getBean("java1",TestBean1.class);
System.out.println("============================");
TestBean1 java2 = ctx2.getBean("java2",TestBean1.class);
System.out.printf("java2.data1 : %d\n",java2.getData1());
System.out.printf("java2.data2 : %d\n",java2.getData2());
System.out.printf("java2.data3 : %d\n",java2.getData3());
TestBean2 java3 = ctx2.getBean("java3",TestBean2.class);
System.out.printf("java3.data1 : %s\n",java3.getData1());
System.out.printf("java3.data2: %s\n",java3.getData2());
TestBean2 java4 = ctx2.getBean("java3",TestBean2.class);
System.out.printf("java4.data1 : %s\n",java4.getData1());
System.out.printf("java4.data2: %s\n",java4.getData2());
ctx2.close();
}
}
beans.xml
<?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 = "xml1" class="kr.co.softcampus.beans.TestBean1" lazy-init="true">
<constructor-arg value="100" type="int" index="0"/>
<constructor-arg value="문자열1" type="int" index="1"/>
<constructor-arg ref = "data_bean1" index="2"/>
<!-- //<constructor-arg ref = "data_bean1" index="2"/> ref="data_bean1"은 객체를 생성하고 선언할떄 xml에서 쓰인다.-->
</bean>
<bean id="xml2" class="kr.co.softcampus.beans.TestBean1" lazy-init="true">
<property name="data1" value="300"/>
<property name="data2" value="문자열3"/>
<property name="data3" ref="data_bean1"/>
</bean>
<bean id = "data_bean1" class="kr.co.softcampus.beans.DataBean1"/>
<bean id="data1" class="kr.co.softcampus.DataBean2"/>
<bean id="data2" class="kr.co.softcampus.DataBean2"/>
<bean id="xml3" class="kr.co.softcampus.beans.TestBean2" lazy-init="true" autowire="byName"/>
<!-- autowire="byName"은 value가 자동 주입된다. -->
<bean class="kr.co.softcampus.beans.DataBean3"/>
<bean id="xml4" class="kr.co.softcampus.beans.TestBean3"/>
</beans>
BeanConfigClass.java
package kr.co.softcampus.config;
import org.springframework.beans.factory.annotation.Autowire;
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.DataBean3;
import kr.co.softcampus.beans.TestBean1;
import kr.co.softcampus.beans.TestBean2;
import kr.co.softcampus.beans.TestBean3;
//TestBean1.java 파일에서 정의한 변수와 메소드에 값을 맵핑하는 부분
@Configuration
public class BeanConfigClass {
@Bean
public TestBean1 java1() {
return new TestBean1(200,"문자열",new DataBean1());
}
@Bean
public TestBean1 java2() {
TestBean1 t1 = new TestBean1();
t1.setData1(400);
t1.setData2("문자열4");
t1.setData3(new DataBean1());
return t1;
}
@Bean
public DataBean2 data1() {
return new DataBean2();
}
@Bean
public DataBean2 data2() {
return new DataBean2();
}
@Bean(autowire = Autowire.BY_NAME)
public TestBean2 java3() {
return new TestBean2();
}
@Bean
public DataBean3 data100() {
return new DataBean3();
}
@Bean
public TestBean3 java4() {
return new TestBean3();
}
}
DataBean1.java
package kr.co.softcampus.beans;
public class DataBean1 {
}
DataBean2.java
package kr.co.softcampus.beans;
public class DataBean2 {
}
DataBean3.java
package kr.co.softcampus.beans;
public class DataBean3 {
}
TestBean1.java
package kr.co.softcampus.beans;
//변수와 메소드를 정의하는 부분
public class TestBean1 {
private int data1;
private String data2;
private DataBean1 data3;
public TestBean1() {
}
public TestBean1(int data1,String data2,DataBean1 data3) {
this.data1 = data1;
this.data2 =data2;
this.data3 = data3;
}
public int getData1() {
return data1;
}
public void setData1(int data1) {
this.data1 = data1;
}
public String getData2() {
return data2;
}
public void setData2(String data2) {
this.data2 = data2;
}
public DataBean1 getData3() {
return data3;
}
public void setData3(DataBean1 data3) {
this.data3 = data3;
}
}
TestBean2.java
package kr.co.softcampus.beans;
public class TestBean2 {
private DataBean2 data1;
private DataBean2 data2;
public DataBean2 getData1() {
return data1;
}
public void setData1(DataBean2 data1) {
this.data1 = data1;
}
public DataBean2 getData2() {
return data2;
}
public void setData2(DataBean2 data2) {
this.data2 = data2;
}
}
TestBean3.java
package kr.co.softcampus.beans;
public class TestBean3 {
private DataBean3 data1;
private DataBean3 data2;
public DataBean3 getData1() {
return data1;
}
public void setData1(DataBean3 data1) {
this.data1 = data1;
}
public DataBean3 getData2() {
return data2;
}
public void setData2(DataBean3 data2) {
this.data2 = data2;
}
}
'웹 프로그래밍(풀스택-->java) > 웹프로그래밍(백엔드-->java)' 카테고리의 다른 글
60. Init,destroy 메서드 (0) | 2021.12.25 |
---|---|
59. Java 코드를 활용한 빈 등록 (0) | 2021.12.25 |
58. 자동주입 (0) | 2021.12.24 |
57. 컬렉션 주입 (0) | 2021.12.22 |
56. Setter 메서드를 통한 주입 (0) | 2021.12.20 |