spring注入方式及集合注入

一、spring注入方式

         spring注入方式分为两种,构造方法注入和设置方法(setter)注入,用得更多的是遵循javabeans规范的设置方法注入

 

       1. 构造方法注入

          使用标签<constructor-arg>进行注入 如一个User对象,它有用户名(name)及密码(password)现个参数,给它添加构造方法这

          两个参数的构造方法,如:

public User(String name, String password) { super(); this.name = name; this.password = password; }

       配置文件中如下配置:

<bean id="user2" class="com.test.inject.User"> <constructor-arg type="java.lang.String" index="1"> <ref bean="pwd1" /> </constructor-arg> <constructor-arg type="java.lang.String" index="0" value="pan1" /> </bean>

大家看到了,用属性"type"指明参数类型 ,index指明该参数在参数列表中的位置(从左到右0、1.2.3.............),也可不写默认按照参数先后顺序配置就行   ,可用到了<ref 。。/>引用其它Bean

           2.设置方法注入

            设置方法注入是用<property>标签配置,在property里指定要设置的属性的名称,spring会调用setter方法来实现注入,所以一定要好相应的setter方法,添加setter方法

public void setName(String name) { this.name = name; } public void setPassword(String password) { this.password = password; }

二、集合注入

看下例子就行了

测试类

package com.test.inject; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class CollectionInjection { private List<String> stringList; private Set<String> stringSet; private Map<String, String> stringMap; private List<User> userList; private Properties pros; public String hello() { return "hello"; } public void print() { for (String s : stringList) { System.out.println(s); } System.out.println("-------------------"); for (String s : stringSet) { System.out.println(s); } System.out.println("-------------------"); for (String s : stringMap.keySet()) { System.out.println(s + " / " + stringMap.get(s)); } System.out.println("-------------------"); for (Object o : pros.keySet()) { System.out.println(o.toString() + " -- " + pros.getProperty(o.toString())); } System.out.println("-------------------"); for (User user : userList) { System.out.println("name:" + user.getName() + ",password:" + user.getPassword()); } } public List<String> getStringList() { return stringList; } public void setStringList(List<String> stringList) { this.stringList = stringList; } public Set<String> getStringSet() { return stringSet; } public void setStringSet(Set<String> stringSet) { this.stringSet = stringSet; } public Map<String, String> getStringMap() { return stringMap; } public void setStringMap(Map<String, String> stringMap) { this.stringMap = stringMap; } public Properties getPros() { return pros; } public void setPros(Properties pros) { this.pros = pros; } public List<User> getUserList() { return userList; } public void setUserList(List<User> userList) { this.userList = userList; } }

配置:

<bean id="collectionInject" class="com.test.inject.CollectionInjection"> <property name="stringList"> <list> <value>panjinfeng0</value> <value>panjinfeng1</value> <value>panjinfeng2</value> </list> </property> <property name="stringSet"> <set> <value>set0</value> <value>set1</value> <value>set2</value> </set> </property> <property name="stringMap"> <map> <entry> <key> <value>key1</value> </key> <value>value1</value> </entry> <entry key="key2" value="value2" /> </map> </property> <property name="pros"> <props> <prop key="prop1k">prop1v</prop> <prop key="prop2k">prop2v</prop> <prop key="prop3k">prop3v</prop> <prop key="prop4k">prop4v</prop> </props> </property> <property name="userList"> <list> <ref bean="user1" /> <ref bean="user2" /> </list> </property> </bean>

 

测试结果:

panjinfeng0
panjinfeng1
panjinfeng2
-------------------
set0
set1
set2
-------------------
key1 / value1
key2 / value2
-------------------
prop1k -- prop1v
prop2k -- prop2v
prop3k -- prop3v
prop4k -- prop4v
-------------------
name:pan0,password:132456-0
name:pan1,password:132456-1

你可能感兴趣的:(spring注入方式及集合注入)