`

spring中单例中获取新的bean实例

    博客分类:
  • JAVA
阅读更多

spring中配置的单例实例中,如果需要每次获取新的属性对象,有两种方式。1、采用配置文件中的lookup-method元素   2、实现BeanFactoryAware接口。

public interface AxeInter {
	public void chop();
}

 

public class StoneAxe implements AxeInter {
	@Override
	public void chop() {
		System.out.println("石头斧头");
	}
}

 

<bean id="axe" class="springTest.impl.StoneAxe" scope="prototype">
</bean>

 

 

  1.  采用配置文件中的lookup-method元素

 

public abstract class UserService2 {
	public abstract AxeInter getAxe();
}

 注意:没有get、set方法

 

 

<bean id="userService2" class="springTest.impl.UserService2">
		<lookup-method name="getAxe" bean="axe"/>
	</bean>

 

public class CopyOfSpringTest1 {

	private ApplicationContext context = null;
	

	@Before
	public void before(){
		context = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	
	@Test
	public void testMoreInstanceInSingleInstance(){
		UserService2 userService = context.getBean("userService2", UserService2.class);
		System.out.println(userService);
		System.out.println(userService.getAxe());
		
		UserService2 userService2 = context.getBean("userService2", UserService2.class);
		System.out.println(userService2);
		System.out.println(userService2.getAxe());
	}
}

 

   2.实现BeanFactoryAware接口。

public class UserService implements BeanFactoryAware{

	private AxeInter axe;
	private BeanFactory beanFactory;
	
	public AxeInter getAxe() {
		this.axe = (AxeInter)beanFactory.getBean("axe");
		return axe;
	}

	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		this.beanFactory = beanFactory;
	}
}

 注意:没有axe的set方法

<bean id="userService" class="springTest.impl.UserService">
	</bean>

 

@Test
	public void testMoreInstanceInSingleInstance(){
		UserService userService = context.getBean("userService", UserService.class);
		System.out.println(userService);
		System.out.println(userService.getAxe());
		
		UserService userService2 = context.getBean("userService", UserService.class);
		System.out.println(userService2);
		System.out.println(userService2.getAxe());
	}

 

 

分享到:
评论

相关推荐

    学习SSM源码分析的一次实践,自己实现SSM框架

    默认采用的是通过单例的模式来进行管理我们注入到Ioc中的bean(当然我们也是可以修改成其他的模式,暂且不讨论),在spring中单例模式是采用注册的方式来实现的单例模式,所以我也是采用map注册的方式实现bean的实例...

    spring mvc 自學筆記

    这里提到的“验证单例多用构造方法”可能是指 Spring MVC 中单例 Bean 的生命周期问题。Spring 管理的 Bean 默认是单例模式,即在整个应用中只有一个实例。当 Bean 采用构造方法初始化时,无论多少次请求,构造函数...

    echart后台获取数据实例

    在“echart后台获取数据实例”中,我们将探讨如何从后台获取数据并将其用于 ECharts 图表的展示。 首先,`EntityController.java` 可能是 Spring MVC 或其他类似框架中的控制器,负责处理HTTP请求并提供数据。在...

    深入解析Java的设计模式编程中单例模式的使用

    单例模式是软件设计模式中的基础模式之一,其核心目标是确保类只有一个实例,并提供一个全局访问点。在Java中,实现单例模式通常有两种方式:饿汉式单例和懒汉式单例。 **饿汉式单例**: 饿汉式单例在类加载时就...

    Java笔试题库①.pdf

    Spring 中单例作用范围 - **知识点**: `Spring`中`Singleton`作用范围的概念。 - **选项解析**: - **A**. `Context`: 正确。在`Spring`的上下文中,`Singleton`作用范围意味着每个`ApplicationContext`只有一个...

    程序员必看的书籍Struts1和Struts2的区别

    - **并发安全性**:Struts2通过为每个请求创建新的Action实例来提高并发安全性,避免了Struts1中单例Action可能导致的问题。 - **验证框架**:Struts2的验证框架更加灵活和强大,支持多种验证机制,提高了代码的...

    互联网高频Java后端面试题20道(适合1~3年)V1.0.78.docx

    答案:Spring Cloud Gateway 是 Spring Cloud 的一个组件,作为微服务架构中的 API 网关,负责路由转发、过滤器链、限流、熔断等职责。它简化了服务发现、安全、监控和路由的实现,是构建高性能、可扩展的微服务网关...

Global site tag (gtag.js) - Google Analytics