2024. 2. 7. 18:03ㆍDaily Codig Reminder
map props
bean
<bean id="pet01" class="com.spring.Cat">
<property name="catName" value="나비"></property>
<property name="catAge" value="3"></property>
</bean>
<bean id="pet02" class="com.spring.Cat">
<property name="catName" value="하늘"></property>
<property name="catAge" value="2"></property>
</bean>
<bean class="com.spring.Student" id="student">
<property name="name" value="홍길동"></property>
<property name="age" value="10">
</property>
<property name="mapCat">
<map>
<entry key="one" value-ref="pet01"></entry>
<entry key="two" >
<ref bean="pet02"/>
</entry>
</map>
</property>
<property name="phones">
<props>
<prop key="one">010</prop>
<prop key="two">011</prop>
<prop key="three">018</prop>
</props>
</property>
</bean>
main
GenericXmlApplicationContext ctx=
new GenericXmlApplicationContext("classpath:com/spring/stu.xml");
Student stu= ctx.getBean("student", Student.class);
Map<String, Cat> map= stu.getMapCat();
Set<String> keys= map.keySet();
for (String key : keys) {
System.out.println(map.get(key));
}
System.out.println("=============");
Properties props= stu.getPhones();
Set<String> xxx= props.stringPropertyNames();//전체 key값을 set형태로 리턴
for (String key : xxx) {
System.out.println(props.getProperty(key));
}
bean
<bean id="another1" class="com.spring.AnotherBean">
<property name="name" value="홍길동"></property>
<property name="age" value="10"></property>
</bean>
<bean id="another2" class="com.spring.AnotherBean">
<property name="name" value="이순신"></property>
<property name="age" value="20"></property>
</bean>
<bean class="com.spring.EchoBean" id="echoBean">
<property name="map">
<map>
<entry key="one" value-ref="another1"></entry>
<entry key="two" >
<ref bean="another2"/>
</entry>
</map>
</property>
</bean>
main
ApplicationContext ctx =
// new GenericXmlApplicationContext("echo.xml");
new GenericXmlApplicationContext("classpath:com/spring/echo.xml");
EchoBean echoRef = ctx.getBean("echoBean", EchoBean.class);
Map<String,AnotherBean> map = echoRef.getMap();
Set<String> keys = map.keySet();
for (String k : keys) {
System.out.println("key : "+ k+ " "+map.get(k).getName()+"\t"+map.get(k).getAge());
}
}
scope
<bean id="stu1" class="com.spring.Student" ></bean>
<bean id="stu2" class="com.spring.Student" > </bean>
main
GenericXmlApplicationContext ctx=
new GenericXmlApplicationContext("classpath:com/spring/studentBean.xml");
Student stu1= (Student)ctx.getBean("stu1");
Student stu2= (Student)ctx.getBean("stu2");
System.out.println(stu1+"\t"+stu2);
System.out.println("stu1==stu2");
System.out.println(stu1==stu2);//prototype
Student stu3= ctx.getBean("stu2", Student.class);
System.out.println(stu1==stu3);
studnet기본생성자 =====
studnet기본생성자 =====
com.spring.Student@6f43c82
com.spring.Student@5db6b9cd
stu1==stu2
false
false
prop 자동주입 byType, byName
- 타입
default-autowire="byType"
bean
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byType"
>
<!-- 기본생성자 사용 필수 -->
<!-- 생성된 빈 (객체 ) 이용타입에 맞는 set 함수에 자동 주입, person에 빈 주입 property 태그 필요없음-->
<bean id="xx" class="com.spring.Cat">
<property name="catName" value="야옹이"></property>
<property name="catAge" value="10"></property>
</bean>
<bean id="yy" class="com.spring.Dog">
<property name="dogName" value="멍멍이"></property>
</bean>
<!-- default - autowire ="byType" 선언으로 자동 주입됨 -->
<bean id="onePerson" class="com.spring.Person">
<property name="username" value="홍길동"></property>
<!-- cat, dog 자동 주입됨 byType -->
<!-- <property name="cat" value="xx"></property>
<property name="dog" value="yy"></property> -->
</bean>
</beans>
console
2.xml에서 빈 생성 , 생성 빈 사용 방법=========
Person [username=홍길동, cat=Cat [catName=야옹이, catAge=10], dog=Dog [DogName=멍멍이]]
Cat [catName=야옹이, catAge=10]
Dog [DogName=멍멍이]
2. 이름
주입시켜줄 bean 이 한개만 있어야 함
<bean id="cat02" class="com.spring.Cat">
<property name="catName" value="야옹이"></property>
<property name="catAge" value="10"></property>
</bean>
<bean id="xx" class="com.spring.Cat">
<property name="catName" value="야옹이"></property>
<property name="catAge" value="10"></property>
</bean>
이렇게 두개 있으면 안생김
byName
을 쓰면 cat 과 똑같은 cat 만 주입이 됨
muti_config
<bean id="cat" class="com.spring.Cat">
<property name="catName" value="야옹이"></property>
<property name="catAge" value="10"></property>
</bean>
<bean id="onePerson" class="com.spring.Person" autowire="byType">
<property name="username" value="홍길동"></property>
</bean>
list_autowired
bean
<bean id="another" class="com.spring.AnotherBean"></bean>
<bean id="echoBean" class="com.spring.EchoBean" autowire="byType"><!-- 멤버변수이름과 같은 id주입 -->
<!-- anotherBean으로 자동 주입 -->
<property name="valueList">
<list>
<value>10</value>
<value>20</value>
<value>30</value>
</list>
</property>
</bean>
echo bean↓
private List valueList;
private AnotherBean anotherBean;
public String sayEcho() {
return "hello";
}
public EchoBean(AnotherBean anotherBean) {
System.out.println("EchoBean(AnotherBean anotherBean) 생성자 호출됨");
this.anotherBean= anotherBean;
}
public void setAnotherBean(AnotherBean anotherBean) {
System.out.println("EchoBean.setAnotherBean(AnotherBean anotherBea)호출됨=="+
anotherBean);
this.anotherBean = anotherBean;
}
public EchoBean() {}
public void setValueList(List valueList) {
this.valueList= valueList;
}
public List getValueList() {
return valueList;
}
public void getInfo() {
System.out.println(valueList);
}
public AnotherBean getAnotherBean() {
return anotherBean;
}
[10, 20, 30]
AnotherBean getInfo()=======================
getInfo 로 찍음
autowired_constructor
<bean id="another" class="com.spring.AnotherBean"></bean>
<!-- autowire constructor -->
<bean id="echoBean" class="com.spring.EchoBean" autowire="constructor"><!-- byType으로 생성자 호출-->
<!-- public EchoBean(AnotherBean anotherBean) {
System.out.println("EchoBean(AnotherBean anotherBean) 생성자 호출됨");
this.anotherBean= anotherBean;
} -->
<!-- anotherBean으로 자동 주입 -->
<property name="valueList">
<list>
<value>10</value>
<value>20</value>
<value>30</value>
</list>
</property>
</bean>
package com.spring;
import java.util.List;
public class EchoBean {
private List valueList;
private AnotherBean anotherBean;
public String sayEcho() {
return "hello";
}
public EchoBean() {
}
public EchoBean(AnotherBean anotherBean) {
System.out.println("EchoBean(AnotherBean anotherBean) 생성자 호출됨");
this.anotherBean= anotherBean;
}
public void setAnotherBean(AnotherBean anotherBean) {
System.out.println("EchoBean.setAnotherBean(AnotherBean anotherBea)호출됨=="+
anotherBean);
this.anotherBean = anotherBean;
}
public void setValueList(List valueList) {
this.valueList= valueList;
}
public List getValueList() {
return valueList;
}
public void getInfo() {
System.out.println(valueList);
}
public AnotherBean getAnotherBean() {
return anotherBean;
}
}
[10, 20, 30]
AnotherBean getInfo()=======================
(autowired_constructor)2
<bean id="another" class="com.spring.AnotherBean"></bean>
<!-- autowire constructor -->
<util:list id="aaaaaa">
<value>10</value>
<value>20</value>
<value>30</value>
</util:list>
<bean id="echoBean" class="com.spring.EchoBean"
autowire="constructor"><!-- byType으로 생성자 호출 -->
<!-- public EchoBean(AnotherBean anotherBean) { System.out.println("EchoBean(AnotherBean
anotherBean) 생성자 호출됨"); this.anotherBean= anotherBean; } -->
<!-- anotherBean으로 자동 주입 -->
<!-- <property name="valueList"> <list> <value>10</value> <value>20</value>
<value>30</value> </list> </property> -->
package com.spring;
import java.util.List;
public class EchoBean {
private List valueList;
private AnotherBean anotherBean;
public EchoBean(List valueList, AnotherBean anotherBean) {
super();
this.valueList = valueList;
this.anotherBean = anotherBean;
System.out.println("생성자 호출 ");
}
public String sayEcho() {
return "hello";
}
public EchoBean(AnotherBean anotherBean) {
System.out.println("EchoBean(AnotherBean anotherBean) 생성자 호출됨");
this.anotherBean= anotherBean;
}
public void setAnotherBean(AnotherBean anotherBean) {
System.out.println("EchoBean.setAnotherBean(AnotherBean anotherBea)호출됨=="+
anotherBean);
this.anotherBean = anotherBean;
}
public EchoBean() {
}
public void setValueList(List valueList) {
this.valueList= valueList;
}
public List getValueList() {
return valueList;
}
public void getInfo() {
System.out.println(valueList);
}
public AnotherBean getAnotherBean() {
return anotherBean;
}
}
AnotherBean getInfo()=======================
[10, 20, 30]
Service_DAO
Dao
public List<String>getList(){
return Arrays.asList(”A”,”B”,”C”); }
Main ⇒ service.getList()⇒ dao.getList() 순회하여 출력 config.xml dao, service
- dao 명시적 주입 (Property 이용) test
- byType 이용 자동 주입 Test
- byName 이용 자동주입 Test
<bean id="dao" class="com.dao.DeptDAO"></bean>
<bean class="com.service.DeptService" id="deptService" autowire="byType">
</bean>
prototype/singletone
System.out.println("2.xml에서 빈 생성 , 생성 빈 사용 방법=========");
ApplicationContext ctx = new
GenericXmlApplicationContext("classpath:com/spring/echo.xml");
EchoBean echo1 = ctx.getBean("echoBean", EchoBean.class);
EchoBean echo2 = ctx.getBean("echoBean", EchoBean.class);
System.out.println(echo1==echo2);
System.out.println(echo1+"\t"+echo2);
System.out.println(echo1.getAnotherBean()==echo2.getAnotherBean());
<bean class="com.spring.AnotherBean" id ="anotherBean1" scope="prototype"></bean>
<bean class="com.spring.EchoBean" id="echoBean" scope="prototype" autowire="byType">
<property name="valueList">
<list>
<value>10</value>
<value>20</value>
<value>30</value>
</list>
</property>
</bean>
false
EchoBean [valueList=[10, 20, 30], anotherBean=com.spring.AnotherBean@72ef8d15]
EchoBean [valueList=[10, 20, 30], anotherBean=com.spring.AnotherBean@6aa8e115]
false
<bean class="com.spring.AnotherBean" id ="anotherBean1" ></bean>
<bean class="com.spring.EchoBean" id="echoBean" scope="singleton" autowire="byType">
<property name="valueList">
<list>
<value>10</value>
<value>20</value>
<value>30</value>
</list>
</property>
</bean>
</beans>
2.xml에서 빈 생성 , 생성 빈 사용 방법=========
true
EchoBean [valueList=[10, 20, 30], anotherBean=com.spring.AnotherBean@23202fce]
EchoBean [valueList=[10, 20, 30], anotherBean=com.spring.AnotherBean@23202fce]
true
@Autowired ❗❗❗❗❗❗❗❗❗❗❗❗❗❗❗❗❗❗❗❗❗
속성 또는 생성자, setter 메소드에 설정, 필수 속성이다
(requires=false 로 필수 속성 해제 가능)
-autowire=”byType”과 동일한 기능 -ㅂ열 , 컬렉션 모두 설정가능
package com.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
public class Person {
String username;
int age;
//***//
@Autowired
Cat cat;
//injection 함수
@Autowired
Dog dog;
public void setCat(Cat cat) {
this.cat = cat;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Cat getCat() {
return cat;
}
@Override
public String toString() {
return "Person [username=" + username + ", age=" + age + ", cat=" + cat + ", dog=" + dog + "]";
}
public Person(String username, int age, Cat cat, Dog dog) {
super();
this.username = username;
this.age = age;
this.cat = cat;
this.dog = dog;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public Person(String username, int age, Cat cat) {
super();
this.username = username;
this.age = age;
this.cat = cat;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
}
bean
<context:annotation-config></context:annotation-config>
<bean class="com.spring.Cat" id ="pet01" >
<constructor-arg name="catName" value="야옹이"></constructor-arg>
<constructor-arg name="catAge" value="20"></constructor-arg>
</bean>
<bean class="com.spring.Dog" id ="pet02" ></bean>
<bean class="com.spring.Person" id="person">
<property name="username" value="홍길동"></property>
<property name="age" value="10"></property>
</bean>
Person [username=홍길동, age=10, cat=Cat
[catName=야옹이, catAge=20], dog=com.spring.Dog@32c4e8b2]
//@Autowired(required = false)//기본주입의무사항이 아님
@Autowired
Cat cat;
xml에서 쓸 때
false 쓰면 null 로 찍히는데 false 안쓰면 작동이 안됨
'Daily Codig Reminder' 카테고리의 다른 글
spring- jdbc (1) | 2024.02.08 |
---|---|
@Autowired, CoC ,@Qualifier ,lifecycle (1) | 2024.02.08 |
spring - property (1) | 2024.02.06 |
스프링 준비 (0) | 2024.02.05 |
cors (0) | 2024.01.15 |