英文:
Prototype Bean Destruction
问题
我有以下的PROTOTYPE类。
- 我的
DemoService
对象会被Spring销毁吗?它会被垃圾回收吗? - 我如何手动销毁
DemoService
,以便进行垃圾回收?
@Service
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class DemoService {
@Autowired
private DaoCall daoCall; //调用数据库。连接将会被连接池关闭。
@Autowired
private KafkaTemplate<String, String> template; //用于向Kafka发送消息
}
我正在将DemoService
原型类注入到单例类GreetingController
中。
@RestController
public class GreetingController {
@Autowired
private DemoService demoService;
@GetMapping("/greeting")
public String greeting()
{
demoService. //调用demoService
return "Hello ";
}
}
英文:
I have the below PROTOTYPE class .
1, Will my DemoService
objects destroyed by spring?And will it be garbage collected?
2. How can i manually destroy the DemoService
so it is Garbage Collected?
@Service
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class DemoService {
@Autowired
private DaoCall daoCall; //call to database.Connection will be closed by the connection pool.
@Autowired
private KafkaTemplate<String, String> template; //used to send message to Kafka
}
I am injecting DemoService prototype class in the singleton class GreetingController
@RestController
public class GreetingController {
@Autowired
private DemoService demoService;
@GetMapping("/greeting")
public String greeting()
{
demoService. //call to demoService
return "Hello ";
}
答案1
得分: 2
如果您以这种方式使用原型bean,它只会在创建单例bean时创建一次,并且只会在Spring上下文关闭时销毁。
您需要使用ApplicationContext
获取对它的引用:
public class GreetingController implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
@GetMapping("/greeting")
public String greeting() {
DemoService demoService = applicationContext.getBean(DemoService.class);
demoService. //调用demoService
return "Hello ";
}
}
然后,当您停止使用它时,它将像其他Java对象一样被垃圾回收。
还有许多其他方法可以获得原型bean的实例,请参阅 https://www.baeldung.com/spring-inject-prototype-bean-into-singleton。
英文:
If you use a prototype bean this way it's only going to be created once when the singleton bean is created and it will only be destroyed when the spring context is shutdown.
You need to get a reference to it using the ApplicationContext
public class GreetingController implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
@GetMapping("/greeting")
public String greeting()
{
DemoService demoService = applicationContext.getBean(DemoService.class);
demoService. //call to demoService
return "Hello ";
}
}
Then it will be garbage collected when you stop using it as any other java object.
Many more ways to get and instance of a prototype bean see https://www.baeldung.com/spring-inject-prototype-bean-into-singleton
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论