728x90
setDI
<?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="test1" class="beans.Test">
<property name="d1" value="200"/>
<property name="d2" value="11.11"/>
<property name="d3" value='hi'/>
<property name="d4" value="true"/>
<property name="d5">
<bean class="beans.Data"/>
</property>
<property name="d6" ref="data"/>
</bean>
<bean id="data" class="beans.Data"/>
</beans>
package beans;
public class Test {
private int d1;
private double d2;
private String d3;
private boolean d4;
private Data d5;
private Data d6;
public int getD1() {
return d1;
}
public void setD1(int d1) {
this.d1 = d1;
}
public double getD2() {
return d2;
}
public void setD2(double d2) {
this.d2 = d2;
}
public String getD3() {
return d3;
}
public void setD3(String d3) {
this.d3 = d3;
}
public boolean isD4() {
return d4;
}
public void setD4(boolean d4) {
this.d4 = d4;
}
public Data getD5() {
return d5;
}
public void setD5(Data d5) {
this.d5 = d5;
}
public Data getD6() {
return d6;
}
public void setD6(Data d6) {
this.d6 = d6;
}
}
AutoDI
scope="prototype"
package main;
import beans.*;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainClass {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext("config/beans.xml");
Test t1 = ctx.getBean("test1", Test.class);
System.out.println(t1.getD1());
System.out.println(t1.getD2());
System.out.println();
}
}
<?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">
<!-- set메소드로 주입하는 방법 -->
<bean id="test1" class="beans.Test">
<property name="d1" ref="data"/>
<property name="d2" ref="data"/>
</bean>
<!-- <bean id="data" class="beans.Data"/>-->
<bean id="data" class="beans.Data" scope="prototype"/>
</beans>
package beans;
public class Test {
private Data d1;
private Data d2;
public Data getD1() {
return d1;
}
public void setD1(Data d1) {
this.d1 = d1;
}
public Data getD2() {
return d2;
}
public void setD2(Data d2) {
this.d2 = d2;
}
}
autowire="byName"
package main;
import beans.*;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainClass {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext("config/beans.xml");
Test t2 = ctx.getBean("test2", Test.class);
System.out.println(t2.getD1());
System.out.println(t2.getD2());
}
}
<?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">
<!-- set메소드로 주입하는 방법 -->
<bean id="test1" class="beans.Test">
<property name="d1" ref="data"/>
<property name="d2" ref="data"/>
</bean>
<!-- <bean id="data" class="beans.Data"/>-->
<bean id="data" class="beans.Data" scope="prototype"/>
<!-- autowire="byName": 프로퍼티 이름과 정의된 빈의 이름이 같은 것을 찾아 자동으로 주입 -->
<bean id="test2" class="beans.Test" autowire="byName"/>
<bean id="d1" class="beans.Data"/>
<bean id="d2" class="beans.Data"/>
</beans>
package beans;
public class Test {
private Data d1;
private Data d2;
public Data getD1() {
return d1;
}
public void setD1(Data d1) {
this.d1 = d1;
}
public Data getD2() {
return d2;
}
public void setD2(Data d2) {
this.d2 = d2;
}
}
autowire="byType"
package main;
import beans.*;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainClass {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext("config/beans.xml");
Test2 t = ctx.getBean("test3", Test2.class);
System.out.println(t.getD1());
System.out.println(t.getD2());
}
}
<?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">
<!-- autowire="byType": 프로퍼티 타입과 정의된 빈의 타입이 일치할 경우 자동으로 주입 -->
<bean id="test3" class="beans.Test2" autowire="byType"/>
<bean id="data2" class="beans.Data2"/>
</beans>
package beans;
public class Test2 {
private Data2 d1;
private Data2 d2;
public Data2 getD1() {
return d1;
}
public void setD1(Data2 d1) {
this.d1 = d1;
}
public Data2 getD2() {
return d2;
}
public void setD2(Data2 d2) {
this.d2 = d2;
}
}
CollectionDI
List<String> 주입
package main;
import beans.*;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class MainClass {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext("config/beans.xml");
Test t1 = ctx.getBean("test1", Test.class);
List<String> li = t1.getList1();
for(String str:li) {
System.out.println(str);
}
}
}
<?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="test1" class="beans.Test">
<property name="list1">
<list>
<value>스프링1</value>
<value>스프링2</value>
<value>스프링3</value>
</list>
</property>
<property name="list2">
<list>
<value type="int">10</value>
<value type="int">20</value>
<value type="int">30</value>
</list>
</property>
<property name="list3">
<list>
<bean class="beans.Data"/>
<ref bean="data"/>
</list>
</property>
</bean>
<bean id="data" class="beans.Data"></bean>
</beans>
package beans;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
public class Test {
private List<String> list1;
private List<Integer> list2;
private List<Data> list3;
private Set<String> set1;
private Set<Integer> set2;
private Set<Data> set3;
private Map<String, Objects> map1;
public List<String> getList1() {
return list1;
}
public void setList1(List<String> list1) {
this.list1 = list1;
}
public List<Integer> getList2() {
return list2;
}
public void setList2(List<Integer> list2) {
this.list2 = list2;
}
public List<Data> getList3() {
return list3;
}
public void setList3(List<Data> list3) {
this.list3 = list3;
}
public Set<String> getSet1() {
return set1;
}
public void setSet1(Set<String> set1) {
this.set1 = set1;
}
public Set<Integer> getSet2() {
return set2;
}
public void setSet2(Set<Integer> set2) {
this.set2 = set2;
}
public Set<Data> getSet3() {
return set3;
}
public void setSet3(Set<Data> set3) {
this.set3 = set3;
}
}
List<Integer> / List<Data> 주입
package main;
import beans.*;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class MainClass {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext("config/beans.xml");
System.out.println("----------------------------------------------------------------");
List<Integer> li2 = t1.getList2();
for(int i:li2) {
System.out.println(i);
}
System.out.println("----------------------------------------------------------------");
List<Data> li3 = t1.getList3();
for(Data d:li3) {
System.out.println(d);
}
}
}
<?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="test1" class="beans.Test">
<property name="list1">
<list>
<value>스프링1</value>
<value>스프링2</value>
<value>스프링3</value>
</list>
</property>
<property name="list2">
<list>
<value type="int">10</value>
<value type="int">20</value>
<value type="int">30</value>
</list>
</property>
<property name="list3">
<list>
<bean class="beans.Data"/>
<ref bean="data"/>
</list>
</property>
</bean>
<bean id="data" class="beans.Data"></bean>
</beans>
package beans;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
public class Test {
private List<String> list1;
private List<Integer> list2;
private List<Data> list3;
private Set<String> set1;
private Set<Integer> set2;
private Set<Data> set3;
private Map<String, Objects> map1;
public List<String> getList1() {
return list1;
}
public void setList1(List<String> list1) {
this.list1 = list1;
}
public List<Integer> getList2() {
return list2;
}
public void setList2(List<Integer> list2) {
this.list2 = list2;
}
public List<Data> getList3() {
return list3;
}
public void setList3(List<Data> list3) {
this.list3 = list3;
}
public Set<String> getSet1() {
return set1;
}
public void setSet1(Set<String> set1) {
this.set1 = set1;
}
public Set<Integer> getSet2() {
return set2;
}
public void setSet2(Set<Integer> set2) {
this.set2 = set2;
}
public Set<Data> getSet3() {
return set3;
}
public void setSet3(Set<Data> set3) {
this.set3 = set3;
}
}
Map<String, object> 주입
package main;
import beans.*;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
import java.util.Map;
public class MainClass {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext("config/beans.xml");
Map<String, Object> m1 = t1.getMap1();
String s1 = (String)m1.get("k1");
int s2 = (Integer)m1.get("k2");
Data s3=(Data)m1.get("k3");
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
<?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">
<property name="map1">
<map>
<entry key="k1" value="스프링"/>
<entry key="k2" value="10" value-type="java.lang.Integer"/>
<entry key="k3">
<bean class="beans.Data"/>
</entry>
</map>
</property>
</bean>
package beans;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Test {
private List<String> list1;
private List<Integer> list2;
private List<Data> list3;
private Set<String> set1;
private Set<Integer> set2;
private Set<Data> set3;
private Map<String, Object> map1;
public List<String> getList1() {
return list1;
}
public void setList1(List<String> list1) {
this.list1 = list1;
}
public List<Integer> getList2() {
return list2;
}
public void setList2(List<Integer> list2) {
this.list2 = list2;
}
public List<Data> getList3() {
return list3;
}
public void setList3(List<Data> list3) {
this.list3 = list3;
}
public Set<String> getSet1() {
return set1;
}
public void setSet1(Set<String> set1) {
this.set1 = set1;
}
public Set<Integer> getSet2() {
return set2;
}
public void setSet2(Set<Integer> set2) {
this.set2 = set2;
}
public Set<Data> getSet3() {
return set3;
}
public void setSet3(Set<Data> set3) {
this.set3 = set3;
}
public Map<String, Object> getMap1() {
return map1;
}
public void setMap1(Map<String, Object> map1) {
this.map1 = map1;
}
}
728x90
'Frameworks > Spring' 카테고리의 다른 글
[Spring] MVC (Form요소,태그 / forward/redirect / Request 빈 주입) (2) | 2024.05.08 |
---|---|
[Spring] MVC (추출/ 주입받기 (Mapping / viewResolver)) (2) | 2024.05.08 |
[Spring] @Autowired, @Component를 활용한 자동 구성 (0) | 2024.05.02 |
[Spring] 설정 클래스를 활용한 Bean 수동 구성 (0) | 2024.05.02 |
[Spring] 스프링 프레임워크 개념 (0) | 2024.05.01 |