Spring IOC注入(一)set方式注入

来源:互联网 发布:python爬虫克隆网页 编辑:程序博客网 时间:2024/06/05 21:52

IOC注入的方式有好几种,现在就来学习一下set方式注入~

可以注入的内容有:

1.基本类型(8中基本类型+字符串)的装配
2.对象类型的装配
3.集合的装配

现在就来讲讲怎么用set方式注入的吧~

1.基本类型的装配:

配置文件set.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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-3.2.xsd">     <bean id="helloBean" class="com.x.spring.bean.HelloBean"><property name="name"><value>tom</value></property><property name="age" value="20"></property></bean>    </beans>

id是Bean的唯一标识,要求在整个配置文件中要唯一,也可使用name属性,bean标签里面的id和name属性都可以用来标识这个配置的对象,但是id会帮我们检查给对象起的名字是否规范(名字不能重复、不能用数字开头、不能有空格等等),如果检查出来了那么就会报错。name属性不会帮检查这些东西。

property 对于所有用set方式来注入的必须该标签

value是对以基本类型,都用value(标签/属性)来注入,可以实现自动的数据类型转换


HelloBean类:

public class HelloBean {private String name;private int age;public String sayHello(){return "hello "+name +",your age is" + age;}public HelloBean() {super();// TODO Auto-generated constructor stub}public HelloBean(String name, int age) {super();this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
测试类SetTest:

public class SetTest {public static void main(String[] args) {//基本类型的装配ApplicationContext ac = new ClassPathXmlApplicationContext("set.xml");//获取容器的一个实例HelloBean hb = (HelloBean) ac.getBean("helloBean");System.out.println(hb.sayHello());}}


效果图:


2.对象类型的装配:

 (1)、<ref local=" "/> 用于涉及的对象的id在当前配置文件中(用于在本配置文件中配置了
的bean的引入同ref="..")

(2)、<ref bean=" "/>  用于涉及的对象的id不在本配置文件中(用于引用不在本配置文件中配置的bean)

(3)、使用property的ref属性引用

下面的是第三种方式的~

写了俩个xml文件:some.xml和other.xml

some.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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-3.2.xsd">   <bean id="someBean" class="com.x.spring.bean.SomeBean"><property name="ob"><ref bean="otherBean" /></property></bean>         </beans>


other.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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-3.2.xsd">   <bean id="otherBean" class="com.x.spring.bean.OtherBean"><property name="str1"><value>string1</value></property></bean></beans>

SomeBean:

public class SomeBean {private OtherBean ob;public void printInfo(){System.out.println("someBean "+ob);}public OtherBean getOb() {return ob;}public void setOb(OtherBean ob) {this.ob = ob;}}
OtherBean:

public class OtherBean {private String str1;public String getStr1() {return str1;}public void setStr1(String str1) {this.str1 = str1;}public String toString(){return "OtherBean "+str1;}}

测试类SetTest:

public class SetTest {public static void main(String[] args) {//对象类型的装配        String[] path = {"some.xml","other.xml"};ApplicationContext ac = new ClassPathXmlApplicationContext(path);SomeBean sb = (SomeBean) ac.getBean("someBean");sb.printInfo();}}

效果图:


在这里写一下第二种方式的~

只需要改一下配置文件some.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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-3.2.xsd"><bean id="someBean" class="com.x.spring.bean.SomeBean"><property name="ob" ref="otherBean">  </property></bean>         </beans>


效果图就不贴了,和上面的一样~

3.集合的装配:

方式:配置元素<list> <set> <map> <props>

list.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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-3.2.xsd">   <bean id="listBean" class="com.x.spring.bean.ListBean"><property name="listProperty"> <list>    <value>list1</value>    <value>list1</value>    <value>list3</value> </list></property><property name="setProperty"> <set>    <value>set1</value>    <value>set1</value>    <value>set3</value> </set></property><property name="mapProperty"> <map>    <entry key="key1">  <value>value1</value>    </entry>    <entry key="key2">  <value>value2</value>    </entry> </map></property><property name="property">     <props>  <prop key="key1">prop1</prop>  <prop key="key2">prop2</prop>  <prop key="key3">prop3</prop>     </props></property></bean></beans>
ListBean类:

public class ListBean {private List listProperty;private Set setProperty;private Map mapProperty;private Properties property;public List getListProperty() {return listProperty;}public void setListProperty(List listProperty) {this.listProperty = listProperty;}public Set getSetProperty() {return setProperty;}public void setSetProperty(Set setProperty) {this.setProperty = setProperty;}public Map getMapProperty() {return mapProperty;}public void setMapProperty(Map mapProperty) {this.mapProperty = mapProperty;}public Properties getProperty() {return property;}public void setProperty(Properties property) {this.property = property;}public void printInfo(){System.out.println("listProperty:");System.out.println(listProperty);System.out.println("setProperty:");System.out.println(setProperty);Set set = mapProperty.entrySet();Iterator it = set.iterator();System.out.println("mapProperty:");while(it.hasNext()){ Map.Entry entry = (Entry) it.next(); System.out.println("Key " +entry.getKey() ); System.out.println("value "+entry.getValue());}System.out.println("props: ");Set set2 = property.entrySet();Iterator it2 = set2.iterator();while(it2.hasNext()){Map.Entry entry= (Entry) it2.next();System.out.println("key "+entry.getKey());System.out.println("value "+entry.getValue());}}}
这个好像没什么讲的~

SetTest测试类:

public class SetTest {public static void main(String[] args) {//集合的装配String path = "list.xml";ApplicationContext  ac =new ClassPathXmlApplicationContext(path);ListBean lb = (ListBean) ac.getBean("listBean");lb.printInfo();}}
效果图:


写到这里set方式注入就写完了~


0 0
原创粉丝点击