spring 容器生成bean的3中方式

spring容器在创建bena的时候可以有3中方式,在日常开发中可以根据需求选择其中的某一中,但是正常的情况都是第一种(构造函数)

具体实现代码如下:

User.java

 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>

测试代码:

 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 }

输出结果:

四月 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

 

posted on 2014-04-12 20:08  把-大象装进-冰箱  阅读(649)  评论(0编辑  收藏  举报