5. Spring 装配Bean的三种方式,bean作用域
实例化Bean的三种方式
-
使用构造方法实例化
-
使用静态工厂方法实例化
-
使用实例化工厂方法实例化
代码实现:
UserServiceImpl:
1 public class UserServiceImpl implements IUserService { 2 3 private String name; 4 5 public String getName() { 6 return name; 7 } 8 9 public void setName(String name) { 10 this.name = name; 11 } 12 13 public UserServiceImpl() { 14 System.out.println("UserServiceImpl()。。调用了"); 15 } 16 17 @Override 18 public void add() { 19 System.out.println("添加用户。。。"+name); 20 } 21 }UserServiceFactory1
UserServiceFactory1
1 public class UserServiceFactory1 { 2 public static IUserService createUservice(){ 3 return new UserServiceImpl(); 4 } 5 }
UserServiceFactory2
1 public class UserServiceFactory2 { 2 public IUserService createUservice(){ 3 return new UserServiceImpl(); 4 } 5 }
Beans1.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--装配bean的三种方式,所谓的装配bean就是在xml写一个bean标签--> <!--第一种方式:new 实现类--> <bean id="userService" class="com.gyf.service.UserServiceImpl"> <!--依赖注入数据,调用属性的set方法--> <property name="name" value="alex"></property> </bean> <!--第二种方式:通过静态工厂方法--> <bean id="userService1" class="com.gyf.service.UserServiceFactory1" factory-method="createUservice"></bean> <!--第三种方式:通过实例工厂方法--> <!--创建实例factory2 bean--> <bean id="factory" class="com.gyf.service.UserServiceFactory2"></bean> <bean id="userService2" factory-bean="factory" factory-method="createUservice"></bean> </beans>
main
1 public class Run2 { 2 @Test() 3 public void test2() { 4 5 ApplicationContext context = new ClassPathXmlApplicationContext("beans1.xml"); 6 7 // new对象 8 //IUserService userService = (IUserService) context.getBean("userService"); 9 //userService.add(); 10 11 //静态工厂 12 //IUserService uservice = UserServiceFactory1.createUservice(); 13 //userService.add(); 14 //IUserService userService1 = (IUserService)context.getBean("userService1"); 15 //userService1.add(); 16 17 //实例化工厂 18 //1.创建工厂 19 //UserServiceFactory2 factory2 = new UserServiceFactory2(); 20 //IUserService userService2 = factory2.createUservice(); 21 22 IUserService userService2 = (IUserService)context.getBean("userService2"); 23 userService2.add(); 24 25 } 26 }
bean的作用域
掌握红色两个常用的即可
====================================================================================================
类别 说明
singleton 在Spring IoC容器中仅存在一个Bean实例,Bean以单例方式存在,默认值
prototype 每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时,相当于执行new xxxBean()
request 每次HTTP请求都会创建一个新的Bean,该作用与仅适用与WebApplicationContext环境
session 同一个HTTP Session共享一个Bean,不同Session使用不同Bean,仅适用WebApplicationContext环境
globalSession 一般用与Portlet应用环境,该作用域仅用于WebApplicationContext环境
====================================================================================================
□scop="singleton"单例模式(默认)
beans3.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 <!----> 6 <bean id="userService" class="com.gyf.service.UserServiceImpl" scope="singleton"> 7 <!--依赖注入数据,调用属性的set方法--> 8 <property name="name" value="alex"></property> 9 </bean> 10 </beans>
main
1 public class Run3 { 2 @Test() 3 public void test2() { 4 5 ApplicationContext context = new ClassPathXmlApplicationContext("beans3.xml"); 6 7 // 从容器获取两个bean 8 IUserService userService1 = (IUserService) context.getBean("userService"); 9 IUserService userService2 = (IUserService) context.getBean("userService"); 10 System.out.println(userService1); 11 System.out.println(userService2); 12 13 } 14 }
================================
结果:
com.gyf.service.UserServiceImpl@192d3247
com.gyf.service.UserServiceImpl@192d3247
□scop="prototype"
beans3.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!----> <bean id="userService" class="com.gyf.service.UserServiceImpl" scope="prototype"> <!--依赖注入数据,调用属性的set方法--> <property name="name" value="alex"></property> </bean> </beans>
main
1 public class Run3 { 2 @Test() 3 public void test2() { 4 5 ApplicationContext context = new ClassPathXmlApplicationContext("beans3.xml"); 6 7 // 从容器获取两个bean 8 IUserService userService1 = (IUserService) context.getBean("userService"); 9 IUserService userService2 = (IUserService) context.getBean("userService"); 10 System.out.println(userService1); 11 System.out.println(userService2); 12 13 } 14 }
=============================================
结果:
com.gyf.service.UserServiceImpl@8646db9
com.gyf.service.UserServiceImpl@37374a5e

浙公网安备 33010602011771号