Java Reference
In-Depth Information
<bean id="cashier1" class=""
init-method="openFile" destroy-method="closeFile">
<property name="name" value="cashier1" />
<property name="path" value="c:/cashier" />
With these two attributes set in the bean declaration, your Cashier class no longer needs
to implement the InitializingBean and DisposableBean interfaces. You can also delete the
afterPropertiesSet() and destroy() methods as well.
Annotating the @PostConstruct and @PreDestroy Annotations
You can annotate the initialization and destruction callback methods with the JSR-250 life cycle
annotations @PostConstruct and @PreDestroy .
Note To use the JSR-250 annotations, you have to include common-annotations.jar (located in the
lib/j2ee directory of the Spring installation) in your classpath. However, if your application is running on
Java SE 6 or Java EE 5, you needn't include this JAR file.
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class Cashier {
public void openFile() throws IOException {
File logFile = new File(path, name + ".txt");
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(logFile, true)));
public void closeFile() throws IOException {
Then you register a CommonAnnotationBeanPostProcessor instance in the IoC container to call the
initialization and destruction callback methods with the life cycle annotations. In this way, you no longer
need to specify the init-method and destroy-method attributes for your bean.
<beans ...>
<bean class="org.springframework.context.annotation.
CommonAnnotationBeanPostProcessor" />
Search WWH ::

Custom Search