spring 容器生成bean的3中方式
2014-04-12 20:08
387 查看
spring容器在创建bena的时候可以有3中方式,在日常开发中可以根据需求选择其中的某一中,但是正常的情况都是第一种(构造函数)
具体实现代码如下:
User.java
工厂类:
xml配置:
测试代码:
输出结果:
具体实现代码如下:
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
相关文章推荐
- Spring中默认bean名称的生成策略/方式修改
- JAVAWEB开发之Spring详解之——Spring的入门以及IOC容器装配Bean(xml和注解的方式)、Spring整合web开发、整合Junit4测试
- spring IOC容器实例化Bean的方式与RequestContextListener应用
- Spring中bean的实例化方式与IOC容器总结
- Spring(2)与接口配合使用和获取Bean容器的两种方式(BeanFactory 、ApplicationContext)
- spring容器创建对象的3种方式(bean的实例化)
- 关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种
- JunitTest拿spring容器bean的2种方式[Spring ClassPathXmlApplicationContext和FileSystemXmlApplicationContext]
- 【Spring】【IOC】【Spring容器注入Bean对象的四种方式】【Spring中注入bean对象的注解】
- servlet调用spring容器中的bean,的两种方式一种注解一种xml配置
- spring IOC容器实例化Bean的方式与RequestContextListener应用
- servlet调用spring容器中的bean,的两种方式一种注解一种xml配置
- spring学习教程7-spring容器创建bean对象的方式以及如何处理多个spring配置文件
- 关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种
- 传智播客 Spring中bean的实例化方式与IOC容器总结
- 从Spring容器中获取bean的几种方式
- 第36天(就业班) spring引入、专业术语、spring六大模块、bean创建对象的细节、IOC容器、对象依赖关系、自动装配、注解方式
- Spring容器中init Bean和destroy Bean的方式
- spring容器对bean生命周期的管理三中方式
- Java - Spring IoC容器配置Bean的方式?