您的位置:首页 > 编程语言 > Java开发

spring 容器生成bean的3中方式

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

具体实现代码如下:

User.java

/*
* Copyright (C), 2013-2014, xxw
* FileName: User.java
* Author:   xxw
* Date:     2014年4月5日 下午2:01:28
* Description: //模块目的、功能描述
* History: //修改记录
* <author>      <time>      <version>    <desc>
* 修改人姓名             修改时间            版本号                  描述
*/
package com.xxw.pojo;

/**
* 〈一句话功能简述〉<br>
* 〈功能详细描述〉
*
* @author xxw
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
public class User {

/**
* 用户姓名
*/
private String name;
/**
* 用户年龄
*/
private Integer age;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public Integer getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(Integer age) {
this.age = age;
}
/**
*
* 功能描述: <br>
* 〈功能详细描述〉静态工厂方法
*
* @return
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public static User getIntance(){
return new User();
}
}


工厂类:

/*
* Copyright (C), 2013-2014, xxw
* FileName: BeanFactory.java
* Author:   xxw
* Date:     2014年4月12日 下午7:49:55
* Description: //模块目的、功能描述
* History: //修改记录
* <author>      <time>      <version>    <desc>
* 修改人姓名             修改时间            版本号                  描述
*/
package com.xxw.pojo;

/**
* 〈一句话功能简述〉<br>
* 〈功能详细描述〉
*
* @author xxw
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
public class BeanFactory {

public static User user = new User();
private BeanFactory(){
}
public User getUserBean(){
user.setName("user3");
return user;
}
}


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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!-- 实例化bean的三种方式 -->
<!-- 构造函数 -->
<bean name="user1" class="com.xxw.pojo.User" p:name="user1"/>
<!-- 静态方法 -->
<bean name="user2" class="com.xxw.pojo.User" factory-method="getIntance" p:name="user2"/>
<!-- 工厂类的方法 -->
<bean name="beanFactory" class="com.xxw.pojo.BeanFactory"/>
<bean name="user3" factory-bean="beanFactory" factory-method="getUserBean"/>
</beans>


测试代码:

/*
* Copyright (C), 2013-2014, xxw
* FileName: UserDaoTest.java
* Author:   xxw
* Date:     2014年4月5日 下午1:48:53
* Description: //模块目的、功能描述
* History: //修改记录
* <author>      <time>      <version>    <desc>
* 修改人姓名             修改时间            版本号                  描述
*/
package springDemo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.xxw.pojo.User;

/**
* 〈一句话功能简述〉<br>
* 〈功能详细描述〉
*
* @author xxw
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class UserDaoTest extends AbstractJUnit4SpringContextTests {
@Autowired
private User user1;
@Autowired
private User user2;
@Autowired
private User user3;
@Test
public void userTest(){
System.out.println(user1.getName());
System.out.println(user2.getName());
System.out.println(user3.getName());

}
}


输出结果:

四月 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐