Spring学习笔记(10)----公共属性的注入配置
时间:2010-06-02 来源:mcuflower
假设我们定义了四个bean类,其代码分别如下:
Java代码- package com.szy.spring.bean;
- public class Bean1 {
- private Bean2 bean2;
- private Bean3 bean3;
- private Bean4 bean4;
- public Bean2 getBean2()
- {
- return bean2;
- }
- public void setBean2(Bean2 bean2)
- {
- this.bean2 = bean2;
- }
- public Bean3 getBean3()
- {
- return bean3;
- }
- public void setBean3(Bean3 bean3)
- {
- this.bean3 = bean3;
- }
- public Bean4 getBean4()
- {
- return bean4;
- }
- public void setBean4(Bean4 bean4)
- {
- this.bean4 = bean4;
- }
- }
Java代码
- package com.szy.spring.bean;
- public class Bean2
- {
- private int id;
- private String name;
- private String password;
- public int getId()
- {
- return id;
- }
- public void setId(int id)
- {
- this.id = id;
- }
- public String getName()
- {
- return name;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public String getPassword()
- {
- return password;
- }
- public void setPassword(String password)
- {
- this.password = password;
- }
- }
Java代码
- package com.szy.spring.bean;
- public class Bean3
- {
- private int id;
- private String name;
- public int getId()
- {
- return id;
- }
- public void setId(int id)
- {
- this.id = id;
- }
- public String getName()
- {
- return name;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- }
Java代码
- package com.szy.spring.bean;
- public class Bean4
- {
- private int age;
- public int getAge()
- {
- return age;
- }
- public void setAge(int age)
- {
- this.age = age;
- }
- }
按照正常的思路,我们下面就要给每个类进行属性的注入,配置文件如下设置:
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"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <bean id="bean1" class="com.szy.spring.bean.Bean1">
- <property name="bean2" ref="bean2"/>
- <property name="bean3">
- <ref bean="bean3"/>
- </property>
- <property name="bean4" ref="bean4"/>
- </bean>
- <bean id="bean2" class="com.szy.spring.bean.Bean2">
- <property name="id" value="100"/>
- <property name="name">
- <value>kuka</value>
- </property>
- <property name="password" value="123"/>
- </bean>
- <bean id="bean3" class="com.szy.spring.bean.Bean3">
- <property name="id" value="100"/>
- <property name="name" value="kuka"/>
- </bean>
- <bean id="bean4" class="com.szy.spring.bean.Bean4">
- <property name="age" value="22"/>
- </bean>
- </beans>
我们进行测试:
Java代码- @Test
- public void testMethod() throws Exception
- {
- ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
- Bean1 bean1 = (Bean1)ctx.getBean("bean1");
- System.out.println("bean1.bean2.id=" + bean1.getBean2().getId());
- System.out.println("bean1.bean2.name=" + bean1.getBean2().getName());
- System.out.println("bean1.bean2.password=" + bean1.getBean2().getPassword());
- System.out.println("bean1.bean3.id=" + bean1.getBean3().getId());
- System.out.println("bean1.bean3.name=" + bean1.getBean3().getName());
- System.out.println("bean1.bean4.age=" + bean1.getBean4().getAge());
- }
正常输出我们所预期的信息,但是我们观察发现bean2和bean3的部分属性的配置信息是相同的,这仅是两个bean,如果是多个bean的话我们要修改就好修改多处,因此我们可以把这些公共的部分提出出来,进行抽象。这个在Spring中是支持的。我们在建立一个配置文件,命名为:applicationCommon.xml,其内容如下配置
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"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <bean id="beanAbstract" abstract="true">
- <property name="id" value="100"/>
- <property name="name" value="kuka"/>
- </bean>
- <bean id="bean2" class="com.szy.spring.bean.Bean2" parent="beanAbstract">
- <property name="password" value="123"/>
- </bean>
- <bean id="bean3" class="com.szy.spring.bean.Bean3" parent="beanAbstract"/>
- </beans>
beanAbstract就是我们抽象出来的,设置abstract="true"属性后就不需要指定class属性。
我们把原来配置文件里的关于bean2和bean3节点注释掉。
下面进行测试,在这里要注意由于我们使用了两个配置文件,因此我们在读取是要写两个配置文件名。我们查看ClassPathXmlApplicationContext源文件发现其有个构造函数参数是string数组,因此我们可以把这个配置文件名放在数组里面。此外我们还有另外一种实现方法,两个配置文件一个叫applicationContext.xml,另一个applicationCommon.xml,公共部分是applicationC*.xml,下面我们就可以这样进行测试:
Java代码- @Test
- public void testMethod() throws Exception
- {
- ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationC*.xml");
- Bean1 bean1 = (Bean1)ctx.getBean("bean1");
- System.out.println("bean1.bean2.id=" + bean1.getBean2().getId());
- System.out.println("bean1.bean2.name=" + bean1.getBean2().getName());
- System.out.println("bean1.bean2.password=" + bean1.getBean2().getPassword());
- System.out.println("bean1.bean3.id=" + bean1.getBean3().getId());
- System.out.println("bean1.bean3.name=" + bean1.getBean3().getName());
- System.out.println("bean1.bean4.age=" + bean1.getBean4().getAge());
- }
如果我们bean2的name属性的值不是kuka,那么我们只需在applicationCommon.xml文件的bean2节点下再添加property属性即可
Xml代码- <property name="name" value="coolszy"/>
- 10_公共属性的注入配置.rar (2.6 MB)