Spring , @Resource, @PostConstruct and @PreDestroy

Spring supports JSR-250 which provides @Resource, @PostConstruct and @PreDestroy annotations.

@Resource

The @Resource annotation is used in the bean class and is attached to the setter methods of the properties.  It is used to inject a specific bean resource.

[code language=”java”]
@Resource
public void setXXX
[/code]

– will look for a bean with the same name as the property XXX

[code language=”java”]
@Resource(name="beanid")
public void setXXX
[/code]

– will look for a bean with the name “beanid

@PostConstruct

The @PostConstruct annotation is used in the bean class and is attached to any init method that you want to run immediately after the bean has been constructed

[code language=”java”]
@PostConstruct
public void myInitialize()
[/code]

@PreDestroy

The @PreDestroy annotation is used in the bean class and is attached to any destroy method that you want to run immediately before the bean itself is destroyed.

This will be executed just before the context is closed by calling context.close()

[code language=”java”]
@PreDestroy
public void myDestroy()
[/code]

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.