Spring Setter Injection
The Spring IoC container also supports setter injection, which is the preferred method of dependency injection in Spring. Setter injection uses the set* methods in a class file to garner property names that are configurable in the spring XML config.
Class Address contains three properties, getters and setters and print method to print all the properties. Class Entity contains three properties id(int),name(String) ,address(Address),getters and setter,print method to print all the values of Entity and Address. Continue reading
Constructor Injection In Spring
Dependency injection (DI) is a process whereby objects define their dependencies, that is, the other objects they work with.Dependency Injection exists in two major variants
Constructor-based dependency injection
Constructor-based DI is accomplished when the container invokes a class constructor with a number of arguments, each representing a dependency on other class.
Spring @PostConstruct and @PreDestroy
The @PostConstruct and @PreDestroy annotation are part of common-annotations.jar. Init and destroy can be implemented using xml and annotations.
Class PostCnstrctPreDstryExample contains three properties(getters and setters),print() method to print the properties, intialize(@PostConstruct) and destroy(@PreDestroy) methods. The @PostConstruct method will be called after the message property set and @PreDestroy method will be called after the context.close();