程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> Spring筆記(四)--公共屬性的配置,spring筆記--配置

Spring筆記(四)--公共屬性的配置,spring筆記--配置

編輯:JAVA綜合教程

Spring筆記(四)--公共屬性的配置,spring筆記--配置


一、公共屬性的注入配置:

四個bean:

 1 package com.dwr.spring.bean;
 2 
 3 public class Bean1 {
 4     private Bean2 bean2;
 5     private Bean3 bean3;
 6     private Bean4 bean4;
 7 
 8     public Bean2 getBean2() {
 9         return bean2;
10     }
11 
12     public void setBean2(Bean2 bean2) {
13         this.bean2 = bean2;
14     }
15 
16     public Bean3 getBean3() {
17         return bean3;
18     }
19 
20     public void setBean3(Bean3 bean3) {
21         this.bean3 = bean3;
22     }
23 
24     public Bean4 getBean4() {
25         return bean4;
26     }
27 
28     public void setBean4(Bean4 bean4) {
29         this.bean4 = bean4;
30     }
31 }
 1 package com.dwr.spring.bean;
 2 
 3 public class Bean2 {
 4     private int id;
 5     private String name;
 6     private String password;
 7 
 8     public int getId() {
 9         return id;
10     }
11 
12     public void setId(int id) {
13         this.id = id;
14     }
15 
16     public String getName() {
17         return name;
18     }
19 
20     public void setName(String name) {
21         this.name = name;
22     }
23 
24     public String getPassword() {
25         return password;
26     }
27 
28     public void setPassword(String password) {
29         this.password = password;
30     }
31 }
 1 package com.dwr.spring.bean;
 2 
 3 public class Bean3 {
 4     private int id;
 5     private String name;
 6 
 7     public int getId() {
 8         return id;
 9     }
10 
11     public void setId(int id) {
12         this.id = id;
13     }
14 
15     public String getName() {
16         return name;
17     }
18 
19     public void setName(String name) {
20         this.name = name;
21     }
22 }
 1 package com.dwr.spring.bean;
 2 
 3 public class Bean4 {
 4     private int age;
 5 
 6     public int getAge() {
 7         return age;
 8     }
 9 
10     public void setAge(int age) {
11         this.age = age;
12     }
13 }

spring.xml

 1   <bean id="bean1" class="com.dwr.spring.bean.Bean1">
 2         <property name="bean2" ref="bean2"/>
 3         <property name="bean3" ref="bean3"/>
 4         <property name="bean4" ref="bean4"/>
 5     </bean>
 6     <bean id="bean2" class="com.dwr.spring.bean.Bean2">
 7         <property name="id" value="1"/>
 8         <property name="name" value="Jack"/>
 9         <property name="password" value="123456"/>
10     </bean>
11     <bean id="bean3" class="com.dwr.spring.bean.Bean3">
12         <property name="id" value="1"/>
13         <property name="name" value="Jack"/>
14     </bean>
15     <bean id="bean4" class="com.dwr.spring.bean.Bean4">
16         <property name="age" value="20"/>
17     </bean>

測試:

 1 @org.junit.Test
 2     public void testMethod(){
 3         ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
 4         Bean1 bean1 = (Bean1) context.getBean("bean1");
 5 
 6         System.out.println("bean1.getBean2().getId():" + bean1.getBean2().getId());
 7         System.out.println("bean1.getBean2().getName():" + bean1.getBean2().getName());
 8         System.out.println("bean1.getBean2().getPassword():" + bean1.getBean2().getPassword());
 9         System.out.println("bean1.getBean3().getId():" + bean1.getBean3().getId());
10         System.out.println("bean1.getBean3().getName():" + bean1.getBean3().getName());
11         System.out.println("bean1.getBean4().getAge():" + bean1.getBean4().getAge());
12 
13     }

測試結果:

1 bean1.getBean2().getId():1
2 bean1.getBean2().getName():Jack
3 bean1.getBean2().getPassword():123456
4 bean1.getBean3().getId():1
5 bean1.getBean3().getName():Jack
6 bean1.getBean4().getAge():20

bean2和bean3的部分屬性配置信息相同,因此可以將公共部分提出來。xml文件配置如下:

 1   <bean id="beanAbstract" abstract="true">
 2         <property name="id" value="1"/>
 3         <property name="name" value="Jack"/>
 4     </bean>
 5     <bean id="bean1" class="com.dwr.spring.bean.Bean1">
 6         <property name="bean2" ref="bean2"/>
 7         <property name="bean3" ref="bean3"/>
 8         <property name="bean4" ref="bean4"/>
 9     </bean>
10     <bean id="bean2" class="com.dwr.spring.bean.Bean2" parent="beanAbstract">
11         <property name="password" value="123456"/>
12     </bean>
13     <bean id="bean3" class="com.dwr.spring.bean.Bean3" parent="beanAbstract"></bean>
14     <bean id="bean4" class="com.dwr.spring.bean.Bean4">
15         <property name="age" value="20"/>
16     </bean>

beanAbstract是抽象出來的,令abstract="true"後,可以不指定class屬性。

測試結果與上面相同。

若bean2的name不同,則可以在配置bean2是聲明(配置property)。

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved