Spring 不同類型的值注入
Spring xml注入
構造器注入值
注入對象
public class Person {
private int id;
private String name;
private int age;
public Person() {
}
public Person(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
getter And Setter
.....
bean的設置
<!-- 給 person 設置多個別名 -->
<bean id="person" class="com.test.spring.model.Person" name="#aa,@123,abc" scope="prototype">
<!-- 構造器設置值 0 :第一個參數, 1:第二參數,2:第三個參數-->
<constructor-arg index="0" value="123"/>
<constructor-arg index="1" value="nice"/>
<constructor-arg index="2" value="18"/>
</bean>
測試
@Test
public void test001(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
Person person = applicationContext.getBean("person",Person.class);
System.out.println("id:"+person.getId()+"\n"+"name:"+person.getName()+"\n"+"年齡:"+person.getAge());
}
普通值注入
創建一個Group對象
public class Group {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<!-- 普通值注入 -->
<bean id="group" class="com.test.spring.model.Group">
<property name="name" value="張三"/>
</bean>
數組,List,Set,Map注入
創建一個USer對象用於數組,List,Set,Map的注入
public class User {
private String[] name;//數組注入
private List<Group> groupList;//List注入
private Set<Group> groupSet;//Set注入
private Map<String,Group> groupMap;//Map注入
public Map<String, Group> getGroupMap() {
return groupMap;
}
public void setGroupMap(Map<String, Group> groupMap) {
this.groupMap = groupMap;
}
......
}
spring.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!--創建一個bean對象-->
<!--scope="singleton" 單例模式 ,默認爲單例模式-->
<!--scope="prototype" 多例模式-->
<bean name="hello" class="com.zx.spring.model.Hello" scope="prototype">
<property name="name" value="spring"/>
</bean>
<!-- 創建Group對象,用於向List裏面添加對象-->
<bean name="group1" class="com.zx.spring.model.Group">
<property name="name" value="group1"></property>
</bean>
<bean name="group2" class="com.zx.spring.model.Group">
<property name="name" value="group2"></property>
</bean>
<!-- 數組注入 -->
<bean name="user" class="com.zx.spring.model.User">
<property name="name">
<array>
<value>戰士</value>
<value>法師</value>
<value>弓箭手</value>
</array>
</property>
<!-- List注入 -->
<property name="groupList">
<list>
<ref bean="group1"/>
<ref bean="group2"/>
</list>
</property>
<!-- set注入 -->
<property name="groupSet">
<set>
<ref bean="group1"/>
<ref bean="group2"/>
</set>
</property>
<!-- map注入 -->
<property name="groupMap">
<map>
<entry key="g1" value-ref="group1"></entry>
<entry key="g2" value-ref="group2"></entry>
</map>
</property>
</bean>
</beans>
加載spring.xml注入的對象
**
* Spring IOC(控制反轉)
* 將控制管理權不由JavaBean管理,交給Spring容器管理
*
* DI 依賴注入
* --分層
* --上層依賴於下層
* --下層服務於上層
*
*
*
* Created by Administrator on 2018/1/18.
*/
public class TestSpringh {
@Test
public void showUser(){
BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring.xml");
User user = beanFactory.getBean("user",User.class);
String[] name = user.getName();
for(int i=0;i<name.length;i++){
System.out.println(name[i]);
}
}
//list集合
@Test
public void showList(){
BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring.xml");
User user = beanFactory.getBean("user",User.class);
List<Group> list = user.getGroupList();
for(Group grp:list){
System.out.println(grp.getName());
}
}
//set集合
@Test
public void showSet(){
BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring.xml");
User user = beanFactory.getBean("user",User.class);
Set<Group> set = user.getGroupSet();
for(Group group:set){
System.out.println(group.getName());
}
}
//Map集合
@Test
public void showMap(){
BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring.xml");
User user = beanFactory.getBean("user",User.class);
Map<String,Group> map = user.getGroupMap();
//遍歷Map集合
for (Map.Entry<String,Group> entry:map.entrySet()){
System.out.println(entry.getKey());
System.out.println(entry.getValue().getName());
}
}