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

你可能感兴趣的:(spring,AOP,session,prototype,Class,encoding)