spring3种创建对象的方式

<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!--第一种,最常见 -->
 <bean id="personService" class="com.clannan.service.impl.PersonServiceBean"></bean>

<!--第二种,工厂静态方法 -->
 <bean id="personService1" class="com.clannan.service.impl.PersonServiceBeanFactory" factory-method="createPersonServiceBean"></bean>

<!--第三种,工厂方法 -->
 <bean id="personServicebeanFactory" class="com.clannan.service.impl.PersonServiceBeanFactory"></bean>
 <bean id="personService2" factory-bean="personServicebeanFactory" factory-method="createnewPersonServiceBean"></bean>
 
</beans>

 

有一个奇怪的情况,就是这样配置之后,只要实例化 ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");

就会调用personServicebeanFactory里面的静态和动态创建personService的方法。这是为什么呢?

难道是spring会通过这两个方法创建两个实例,形成了单例的模式?也许是这样吧。

------------------------------------------------------------------------------------------------

11:47补充

spring确实会在初始化时候就创建一个对象形成单例模式,如果不设置bean的scope属性的话。

scope属性有:prototype,每次都会新建,request,session,globle session

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值