which jsr 250 annotations?
Spring also supports JSR-250 based annotations which include @PostConstruct, @PreDestroy and @Resource annotations. Though these annotations are not really required because you already have other alternates, yet let us get a brief idea about them.
To define the setup and teardown for a bean, we simply declare the
You can use @PostConstruct annotation as an alternate of initialization callback and @PreDestroy annotation as an alternate of destruction callback as explained in the below example.
Let us have a working Eclipse IDE in place and take the following steps to create a Spring application −
Here is the content of HelloWorld.java file −
Following is the content of the MainApp.java file. Here, you need to register a shutdown hook registerShutdownHook() method that is declared on the AbstractApplicationContext class. This will ensure a graceful shutdown and call the relevant destroy methods.
Following is the configuration file Beans.xml required for init and destroy methods −
Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message −
You can use @Resource annotation on fields or setter methods and it works the same as in Java EE 5. The @Resource annotation takes a 'name' attribute which will be interpreted as the bean name to be injected. You can say, it follows by-name autowiring semantics as demonstrated in the following example −
If no 'name' is specified explicitly, the default name is derived from the field name or setter method. In case of a field, it takes the field name; in case of a setter method, it takes the bean property name.