标签:utf-8 xmlns action 作用域 效果 ted instance sch 能力
一、Bean的种类
1、普通bean:<bean id="" class="A"> ,spring直接创建A实例,并返回。
2、FactoryBean:是一个特殊的bean,具有工厂生成对象的能力,只能生成特定的对象。bean必须使用 FactoryBean接口,此接口提供方法 getObject() 用于获得特定bean。
<bean id="" class="FB"> 先创建FB实例,使用调用getObject()方法,并返回方法的返回值
FB fb = new FB();
return fb.getObject();
BeanFactory 和 FactoryBean 对比?
BeanFactory:工厂,用于生成任意bean。
FactoryBean:特殊bean,用于生成另一个特定的bean。例如:ProxyFactoryBean ,此工厂bean用于生产代理。
<bean id="" class="....ProxyFactoryBean"> 获得代理对象实例,AOP使用。
二、作用域
作用域:用于确定Spring创建Bean的实例个数。
取值:
singleton 单例,是默认值。
prototype 多例,每执行一次getBean将获得一个实例。例如:struts整合spring,配置action多例。
配置信息:
<bean id="" class="" scope="">
例如:
<bean id="userServiceId" class="com.zju.scope.UserServiceImpl" scope="prototype"></bean>
例1:singleton 单例
UserService.java
package com.zk.myspring; public interface UserService { public void addUser(); }
UserServiceImpl.java
package com.zk.myspring; public class UserServiceImpl implements UserService{ @Override public void addUser() { // TODO Auto-generated method stub System.out.println("UserService"); } }
ApplicationContext.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"> <!-- 默认scope为singleton --> <bean id="userserviceId" class="com.zk.myspring.UserServiceImpl" scope="singleton"></bean> </beans>
TestDemo.java
package com.zk.myspring; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestDemo { @Test public void demo1(){ String xmlpath="ApplicationContext.xml"; ApplicationContext ac=new ClassPathXmlApplicationContext(xmlpath); UserService us=ac.getBean("userserviceId", UserServiceImpl.class); UserService us2=ac.getBean("userserviceId", UserServiceImpl.class); //us.addUser(); //us2.addUser(); System.out.println(us); System.out.println(us2); System.out.println(us==us2); } }
运行效果图:
例2:prototype 多例
UserService.java
package com.zk.myspring; public interface UserService { public void addUser(); }
UserServiceImpl.java
package com.zk.myspring; public class UserServiceImpl implements UserService{ @Override public void addUser() { // TODO Auto-generated method stub System.out.println("UserService"); } }
TestDemo.java
package com.zk.myspring; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestDemo { @Test public void demo1(){ String xmlpath="ApplicationContext.xml"; ApplicationContext ac=new ClassPathXmlApplicationContext(xmlpath); UserService us=ac.getBean("userserviceId", UserServiceImpl.class); UserService us2=ac.getBean("userserviceId", UserServiceImpl.class); //us.addUser(); //us2.addUser(); System.out.println(us); System.out.println(us2); System.out.println(us==us2); } }
ApplicationContext.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="userserviceId" class="com.zk.myspring.UserServiceImpl" scope="prototype"></bean> </beans>
运行效果图:
标签:utf-8 xmlns action 作用域 效果 ted instance sch 能力
原文地址:https://www.cnblogs.com/longlyseul/p/9977390.html