今天研究了一下scope的作用域。默认是单例模式,即scope="singleton"。另外scope还有prototype、request、session、global session作用域。scope="prototype"多例。再配置bean的作用域时,它的头文件形式如下:
如何使用spring的作用域:
<bean id="role" class="spring.chapter2.maryGame.Role" scope="singleton"/>
这里的 scope 就是用来配置 spring bean 的作用域,它标识 bean 的作用域。
1、
package com.longteng.lesson2.dao; public interface IOrderDao { public String bookOrder(String name); }
2、
package com.longteng.lesson2.dao.impl; import com.longteng.lesson2.dao.IOrderDao; import org.springframework.stereotype.Repository; @Repository public class OrderDao implements IOrderDao { @Override public String bookOrder(String name) { return name+"预订了订单"; } }
3、
package com.longteng.lesson2.service; import com.longteng.lesson2.dao.IOrderDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; @Service @Scope("prototype") public class OrderService { IOrderDao iOrderDao; @Autowired public void setiOrderDao(IOrderDao iOrderDao) { this.iOrderDao = iOrderDao; } public String getOrderInfo(String name){ return iOrderDao.bookOrder(name); } @PostConstruct public void initTest(){ System.out.println("初始化调用方法"); } @PreDestroy public void tearDownTest(){ System.out.println("回收调用方法"); } }
4、
package com.longteng.lesson2.config; import com.longteng.lesson2.service.OrderService; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan(basePackages = {"com.longteng.lesson2.dao","com.longteng.lesson2.service"}) public class OrderCfg { @Bean public OrderService getOrderService(){ return new OrderService(); } }
5、
package com.longteng.lesson2.dao; import com.longteng.lesson2.config.OrderCfg; import com.longteng.lesson2.service.OrderService; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class OrderDaoTest { ApplicationContext context; @Before public void initApplication(){ context = new AnnotationConfigApplicationContext(OrderCfg.class); } @Test public void test(){ OrderService orderService = context.getBean("orderService", OrderService.class); OrderService orderService1 = context.getBean("orderService", OrderService.class); System.out.println(orderService==orderService1); } }
6、
15:01:44.959 [main] DEBUG org.springframework.context.annotation.CommonAnnotationBeanPostProcessor - Invoking init method on bean 'orderService': public void com.longteng.lesson2.service.OrderService.initTest() 初始化调用方法 15:01:44.959 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'orderService' false Process finished with exit code 0
---------------------------------------------方法2通过在xml 里面配置scope关键字
1、
package com.longteng.lesson2.dao;
public interface IOrderDao {
public String bookOrder(String name);
}
2、
package com.longteng.lesson2.dao.impl;
import com.longteng.lesson2.dao.IOrderDao;
import org.springframework.stereotype.Repository;
public class OrderDao implements IOrderDao {
@Override
public String bookOrder(String name) {
return name+"预订了订单";
}
}
3、
package com.longteng.lesson2.service; import com.longteng.lesson2.dao.IOrderDao; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class OrderService { IOrderDao iOrderDao; public void setiOrderDao(IOrderDao iOrderDao) { this.iOrderDao = iOrderDao; } public String getOrderInfo(String name){ return iOrderDao.bookOrder(name); } @PostConstruct public void initTest(){ System.out.println("初始化调用方法"); } @PreDestroy public void tearDownTest(){ System.out.println("回收调用方法"); } }
4、
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.longteng.lesson2.dao.impl,com.longteng.lesson2.service"/> <bean id="orderDao" class="com.longteng.lesson2.dao.impl.OrderDao"></bean> <bean id="orderService" class="com.longteng.lesson2.service.OrderService" scope="prototype"> <property name="iOrderDao" ref="orderDao"></property> </bean> </beans>
5、
15:05:47.201 [main] DEBUG org.springframework.context.annotation.CommonAnnotationBeanPostProcessor - Invoking init method on bean 'orderService': public void com.longteng.lesson2.service.OrderService.initTest() 初始化调用方法 15:05:47.201 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'orderService' 15:05:47.201 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'orderService' 15:05:47.201 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'orderDao' 15:05:47.202 [main] DEBUG org.springframework.context.annotation.CommonAnnotationBeanPostProcessor - Invoking init method on bean 'orderService': public void com.longteng.lesson2.service.OrderService.initTest() 初始化调用方法 15:05:47.202 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'orderService' false Process finished with exit code 0