java - Spring @PreDestroy and @PostConstruct is not working when register bean using BeanFactoryPostProcessor programmatically -
i'm trying register bean programmatically using beanfactorypostprocessor
, example code follow:
@configuration public class appconfig implements beanfactorypostprocessor { @override public void postprocessbeanfactory(configurablelistablebeanfactory beanfactory) throws beansexception { mybean mybean = new mybean("mybean-0") { @override public void init() { system.out.println("init bean"); } @override public void destroy() { system.out.println("destroy bean"); } }; beanfactory.registersingleton("bean-0", mybean); } }
definition of mybean
:
public class mybean { private string name; public mybean(string name) { this.name = name; } public string getname() { return name; } public void setname(string name) { this.name = name; } @postconstruct public void init() { system.out.println("init bean : " + name); } @predestroy public void destroy() { system.out.println("destroy bean : " + name); } }
all di stuff works fine init
, destroy
method annotation not work, tried interface initializingbean
, disposablebean
neither help. can understand reason init
not working because created manually new
, , can call init
in constructor job, questions are:
- after registered bean, why
destroy
method not invoked when spring application context shuts down? - am using wrong way this? , how make
init
,destroy
method work expected when want register beans programmatically?
btw, i'm using spring 4.2.5, in advance.
Comments
Post a Comment