Java Tutorial/Spring/Singleton

Материал из Java эксперт
Перейти к: навигация, поиск

BeanFactory PreInstantiate Singletons Integration

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
   <bean id="instantiationCounterClass"
      class="InstantiationCounterClass"/>
</beans>





Prototype Singleton

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
   <bean id="primeIterator"
      class="PrimeIterator" singleton="false">
      <property name="start" value="1000"/>
   </bean>
</beans>





Singleton Scope And Prototype Scope

File: context.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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/util
                http://www.springframework.org/schema/util/spring-util.xsd">
    <bean id="singleMe" class="java.lang.String" scope="singleton">
        <constructor-arg type="java.lang.String" value="Singleton"/>
    </bean>
    <bean id="prototypeMe" class="java.lang.String" scope="prototype">
        <constructor-arg type="java.lang.String" value="Prototype"/>
    </bean>

</beans>





Spring Prototype

File: context.xml



<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="prototypeClient"
          class="SpringPrototypeClient">
        <property name="message1" ref="message"/>
        <property name="message2" ref="message"/>
    </bean>
    <bean id="message" class="EmailMessage" singleton="false"/>
</beans>