spring 容器生成bean的3中方式

spring容器在建立bena的時候能夠有3中方式,在平常開發中能夠根據需求選擇其中的某一中,可是正常的狀況都是第一種(構造函數)java

具體實現代碼以下:spring

User.javaapp

 1 /*
 2  * Copyright (C), 2013-2014, xxw
 3  * FileName: User.java
 4  * Author:   xxw
 5  * Date:     2014年4月5日 下午2:01:28
 6  * Description: //模塊目的、功能描述      
 7  * History: //修改記錄
 8  * <author>      <time>      <version>    <desc>
 9  * 修改人姓名             修改時間            版本號                  描述
10  */
11 package com.xxw.pojo;
12 
13 /**
14  * 〈一句話功能簡述〉<br> 
15  * 〈功能詳細描述〉
16  *
17  * @author xxw
18  * @see [相關類/方法](可選)
19  * @since [產品/模塊版本] (可選)
20  */
21 public class User {
22     
23     /**
24      * 用戶姓名
25      */
26     private String name;
27     /**
28      * 用戶年齡
29      */
30     private Integer age;
31     /**
32      * @return the name
33      */
34     public String getName() {
35         return name;
36     }
37     /**
38      * @param name the name to set
39      */
40     public void setName(String name) {
41         this.name = name;
42     }
43     /**
44      * @return the age
45      */
46     public Integer getAge() {
47         return age;
48     }
49     /**
50      * @param age the age to set
51      */
52     public void setAge(Integer age) {
53         this.age = age;
54     }
55     /**
56      * 
57      * 功能描述: <br>
58      * 〈功能詳細描述〉靜態工廠方法
59      *
60      * @return
61      * @see [相關類/方法](可選)
62      * @since [產品/模塊版本](可選)
63      */
64     public static User getIntance(){
65         return new User();
66     }
67 }

工廠類:函數

 1 /*
 2  * Copyright (C), 2013-2014, xxw
 3  * FileName: BeanFactory.java
 4  * Author:   xxw
 5  * Date:     2014年4月12日 下午7:49:55
 6  * Description: //模塊目的、功能描述      
 7  * History: //修改記錄
 8  * <author>      <time>      <version>    <desc>
 9  * 修改人姓名             修改時間            版本號                  描述
10  */
11 package com.xxw.pojo;
12 
13 /**
14  * 〈一句話功能簡述〉<br> 
15  * 〈功能詳細描述〉
16  *
17  * @author xxw
18  * @see [相關類/方法](可選)
19  * @since [產品/模塊版本] (可選)
20  */
21 public class BeanFactory {
22     
23     public static User user = new User();
24     private BeanFactory(){
25     }
26     public User getUserBean(){
27         user.setName("user3");
28         return user;
29     }
30 }

 

xml配置:測試

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:p="http://www.springframework.org/schema/p"
 5        xmlns:c="http://www.springframework.org/schema/c"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans
 7                            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
 8 <!-- 實例化bean的三種方式 -->
 9     <!-- 構造函數 -->
10     <bean name="user1" class="com.xxw.pojo.User" p:name="user1"/>
11     <!-- 靜態方法 -->
12     <bean name="user2" class="com.xxw.pojo.User" factory-method="getIntance" p:name="user2"/>
13     <!-- 工廠類的方法 -->
14     <bean name="beanFactory" class="com.xxw.pojo.BeanFactory"/>
15     <bean name="user3" factory-bean="beanFactory" factory-method="getUserBean"/>
16 </beans>

測試代碼:ui

 1 /*
 2  * Copyright (C), 2013-2014, xxw
 3  * FileName: UserDaoTest.java
 4  * Author:   xxw
 5  * Date:     2014年4月5日 下午1:48:53
 6  * Description: //模塊目的、功能描述      
 7  * History: //修改記錄
 8  * <author>      <time>      <version>    <desc>
 9  * 修改人姓名             修改時間            版本號                  描述
10  */
11 package springDemo;
12 
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.springframework.beans.factory.annotation.Autowired;
16 import org.springframework.test.context.ContextConfiguration;
17 import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
18 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
19 
20 import com.xxw.pojo.User;
21 
22 /**
23  * 〈一句話功能簡述〉<br>
24  * 〈功能詳細描述〉
25  * 
26  * @author xxw
27  * @see [相關類/方法](可選)
28  * @since [產品/模塊版本] (可選)
29  */
30 @RunWith(SpringJUnit4ClassRunner.class)
31 @ContextConfiguration(locations = "classpath:applicationContext.xml")
32 public class UserDaoTest extends AbstractJUnit4SpringContextTests {
33     @Autowired
34     private User user1;
35     @Autowired
36     private User user2;
37     @Autowired
38     private User user3;
39     @Test
40     public void userTest(){
41         System.out.println(user1.getName());
42         System.out.println(user2.getName());
43         System.out.println(user3.getName());
44 
45     }
46 }

輸出結果:this

四月 12, 2014 7:59:03 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
四月 12, 2014 7:59:03 下午 org.springframework.context.support.GenericApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@10a69f0: startup date [Sat Apr 12 19:59:03 CST 2014]; root of context hierarchy
四月 12, 2014 7:59:03 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5ac214: defining beans [user1,user2,beanFactory,user3,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
user1
user2
user3
四月 12, 2014 7:59:03 下午 org.springframework.context.support.GenericApplicationContext doClose
信息: Closing org.springframework.context.support.GenericApplicationContext@10a69f0: startup date [Sat Apr 12 19:59:03 CST 2014]; root of context hierarchy
四月 12, 2014 7:59:03 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory destroySingletons
信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5ac214: defining beans [user1,user2,beanFactory,user3,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
相關文章
相關標籤/搜索