博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring 对象作用域
阅读量:4050 次
发布时间:2019-05-25

本文共 1197 字,大约阅读时间需要 3 分钟。

– Start


默认情况下, Spring 将创建的对象放在它的容器中缓存,当我们需要某个对象时,Spring 将缓存的对象返回给我们,并不会创建新对象,如果你想让 Spring 每次返回一个新对象该怎么办呢?看看下面的例子吧。

package shangbo.spring.core.example7;public class OutPutService {	public void outPut() {		System.out.println("Hello World");	}}
package shangbo.spring.core.example7;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {	public static void main(String[] args) {		// 实例化 Spring IoC 容器		ApplicationContext context = new ClassPathXmlApplicationContext("example.xml", OutPutService.class);		// 从容器中获得 OutPutService 对象		OutPutService printer = context.getBean(OutPutService.class);		// 使用对象		printer.outPut();	}}

如果你使用 Java 配置元数据,使用 @Scope 注解即可。

package shangbo.spring.core.example8;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Scope;@Configurationpublic class AppConfig {	@Bean	@Scope(value = "prototype")	public OutPutService outPutService() {		return new OutPutService();	}}

– 声 明:转载请注明出处
– Last Updated on 2017-06-17
– Written by ShangBo on 2017-05-21
– End

你可能感兴趣的文章
开发过程中的沟通问题
查看>>
“众”字透出的哲学
查看>>
恋爱爱情婚姻家庭与炒股票
查看>>
答非所问的古今中外名人小笑话幽默
查看>>
周易、命理、风水、姓名与命运交流周易研究心得:姓名学
查看>>
解决asp.net中tabstrip不能点击的问题
查看>>
PB中使用blob进行文件读取的性能问题
查看>>
DataWindow.net中如何实现鼠标划过时变颜色
查看>>
Datawindow.net中设置字符串的显示,超过长度部分显示为。。。
查看>>
PowerBuilder中使用带返回的powerobjectparm
查看>>
从oracle表中随机取记录,产生随机数和随机字符串
查看>>
功夫熊猫,中国式的哲学和西方式的搞笑
查看>>
Oracle SYS口令深入解析
查看>>
XP中IIS“http500”错误的终极解决方法
查看>>
李开复眼中的兰迪教授:引领你的一生
查看>>
早起的虫儿被鸟吃?
查看>>
Love Your Life》—— 热爱生活
查看>>
一个高速交警的忠告
查看>>
新车装饰的中国特色
查看>>
没看过这么NB的自驾游,笑的我眼泪都出来了
查看>>