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.

@Resource
public void setXXX

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

@Resource(name="beanid")
public void setXXX

– 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

@PostConstruct
public void myInitialize()

@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()

@PreDestroy
public void myDestroy()
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s