1.Spring当中的各种值的注入
- 数组
- List
- Set
- Map
如果其对应的值是简单的字面值,直接写就可以了,如果是一个其他类,那么使用内部bean的方式完成
people.java
public class People {
private String name;
private Integer age;
private String[] friends;
private List<Integer> nums;
private List<Cat> cats;
private Set<Pig> pig;
private Map<String,User> users;
//getter and setter
}
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 class="com.cyk.pojo.People" id="people">
<property name="name" value="cyk"/>
<property name="age" value="20"/>
<!--<property name="friends" value="abc def"/> 不科学-->
<property name="friends">
<array>
<value>123</value>
<value>456</value>
<value>789</value>
<value>011</value>
</array>
</property>
<property name="nums">
<list>
<value>8</value>
<value>7</value>
</list>
</property>
<property name="cats">
<list>
<!--内部bean,无法被外部所引用,所以无需id-->
<bean class="com.cyk.pojo.Cat">
<property name="leg" value="4"/>
<property name="skin" value="blue"/>
</bean>
<bean class="com.cyk.pojo.Cat">
<property name="leg" value="4"/>
<property name="skin" value="yellow"/>
</bean>
</list>
</property>
<property name="pig">
<set>
<bean class="com.cyk.pojo.Pig">
<property name="name" value="佩奇"/>
<property name="sleep" value="88"/>
<property name="taste" value="香辣"/>
</bean>
<bean class="com.cyk.pojo.Pig">
<property name="name" value="小宝"/>
<property name="sleep" value="99"/>
<property name="taste" value="酱香"/>
</bean>
</set>
</property>
<property name="users">
<map>
<entry key="user1">
<bean class="com.cyk.pojo.User">
<property name="name" value="韩雪"/>
<property name="address" value="梧桐村"/>
</bean>
</entry>
<entry key="user2">
<bean class="com.cyk.pojo.User">
<property name="name" value="林青霞"/>
<property name="address" value="台湾"/>
</bean>
</entry>
</map>
</property>
</bean>
</beans>
Test.java
@Test
public void m1(){
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
People people = ctx.getBean("people", People.class);
System.out.println(Arrays.toString(people.getFriends()));
System.out.println(people.getFriends().length);
System.out.println(people.getNums());
System.out.println(people.getCats());
System.out.println(people.getPig());
System.out.println(people.getUsers());
}
2.自动注入
- byType:按照数据类型注入
- byName:按照bean对应的pojo里面的属性的名字来进行匹配
- byConstructor:优先按照类型去匹配,如果匹配到一个那么直接注入,不止一个按照名字注入,如果一个都找不到,注入失败。
- default:
- no:不注入
user.java
public class User {
private String name;
private String address;
private Pig pig;
//getter and setter
}
autowired.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 class="com.cyk.pojo.User" id="user" autowire="byType">-->
<!--<property name="name" value="陈慧琳"/>-->
<!--<property name="address" value="香港"/>-->
<!--</bean>-->
<bean class="com.cyk.pojo.User" autowire="constructor" id="user">
<constructor-arg name="name" value="韩红"/>
</bean>
<!--<bean class="com.cyk.pojo.User" id="user" autowire="default">-->
<!---->
<!--</bean>-->
<bean class="com.cyk.pojo.Pig" name="pig2">
<property name="name" value="巨大宝"/>
</bean>
<bean class="com.cyk.pojo.Pig" name="pig">
<property name="name" value="大宝"/>
</bean>
<bean class="com.cyk.Service.ProviderService" id="providerService" autowire="byType">
</bean>
<bean class="com.cyk.dao.ProviderDao" id="providerDao">
</bean>
</beans>
Test.java
@Test
public void m2(){
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("autowired.xml");
User user = ctx.getBean("user", User.class);
System.out.println(user.getPig().getName());
}