December 20 2014

Spring Setter Injection

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.

Example

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

Category: Spring | LEAVE A COMMENT
December 20 2014

Constructor Injection In Spring

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.

Continue reading

Category: Spring | LEAVE A COMMENT
December 18 2014

Spring @PostConstruct and @PreDestroy

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.

Example

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

Continue reading

Category: Spring | LEAVE A COMMENT