singleton单例,拿的一直是同一个
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="S_dao" class="com.sl.dao.StudentDao"></bean>
<bean id="S_service" class="com.sl.service.StudentService" scope="singleton">
<property name="dao" ref="S_dao"></property>
</bean>
</beans>
public class test {
@Test
public void testOne(){
ApplicationContext appl=new ClassPathXmlApplicationContext("beans.xml");
StudentService service=(StudentService)appl.getBean("S_service");
StudentService service1=(StudentService)appl.getBean("S_service");
System.out.println(service==service1);
}
}
输出是true
prototype:不断new新的
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="S_dao" class="com.sl.dao.StudentDao"></bean>
<bean id="S_service" class="com.sl.service.StudentService" scope="prototype">
<property name="dao" ref="S_dao"></property>
</bean>
</beans>
public class test {
@Test
public void testOne(){
ApplicationContext appl=new ClassPathXmlApplicationContext("beans.xml");
StudentService service=(StudentService)appl.getBean("S_service");
StudentService service1=(StudentService)appl.getBean("S_service");
System.out.println(service==service1);
}
}