Sie sind auf Seite 1von 6

Generally, Springs has default support for Singleton pattern.

If you want more t


han one instances of the bean, then, configure the bean tag in xml doc with the
scope attribute value as "prototype".
Note: BeanFactory is the Spring container in which all beans are configured and
can be accessed.
The default constr of the bean is called only with getBean() method is invoked o
n the bean factory object and not while loading the xml doc.
Four types of Injection in Springs
----------------------------------
->Setter Injection
->Dependency Inj
->Constructor Inj
->Method Inj

NOTE:
->All primitives and String objects - <value>
->others - <ref>
->List or array - <list>
->Set - <set>
->Map - <map>
->
->
Setter Injection
----------------
Rule->Include a <property> tag in xml doc and define a setter for the property i
n the pojo.
// interface IHello.java
public interface IHello {
String sayHello();
}
//impl of First class
public class HelloImpl implements IHello{
private String greeterMesg="Hi";
public HelloImpl(){
System.out.println("\nIn Hello constr....");
}
public String sayHello() {
System.out.println("\nIn say Hello method....");
return greeterMesg;
}
public void setGreeterMesg(String greeterMesg){
this.greeterMesg=greeterMesg;
}
}
//beanconfig.xml
<bean id="hello" class="com.myapp.pojo.HelloImpl">
<!-- setter injection -->
<property name="greeterMesg" value="Welcome to Springs"/>
</bean>
//Client appln
public class HelloApp {
public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory(new
ClassPathResource("com/myapp/config/beanconfig.xml"));
IHello hello=(IHello)factory.getBean("hello");
System.out.println("\nIn Client, mesg from bean ...."+hello.sayHello());
}
}
********************************************************************************
*******************
Dependency Injection
--------------------
Client calls a method of a class(say First) which in turn calls a method of anot
her class(say Second)
//interface for Second class
public interface ISecond {
String callSecond();
}
//impl of Second class
public class Second implements ISecond {
Second(){
System.out.println("In Second constr......");
}
public String callSecond(){
System.out.println("In callSecond() method.....");
return "Hello from Second";
}
}
//interface for First class
public interface IFirst {
String callFirst();
}
//impl of First class
public class First implements IFirst{
ISecond second;
First(){
System.out.println("In First constr......");
}
public void setSecond(Second second) {
this.second = second;
}
public String callFirst(){
System.out.println("In callFirst method....");
return second.callSecond();
}
}
// beanconfig.xml
<bean id="fir" class="com.myapp.pojo.First">
<property name="second" ref="sec"/>
</bean>
<bean id="sec" class="com.myapp.pojo.Second"/>
// Client appln
public class DependInjApp {
public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory(new
ClassPathResource("com/myapp/config/beanconfig.xml"));
IFirst f=(IFirst)factory.getBean("fir");
System.out.println("\nIn Client, mesg from bean ...."+f.callFirst());
}
}
//Output
In First constr......
In Second constr......
In callFirst method....
In callSecond() method.....
In Client, mesg from bean ....Hello from Second
********************************************************************************
*******************
Constructor Injection
----------------------
Passing arguments to a parameterized constructor
//interface IParamConstr.java
public interface IParamConstr {
String getDetails();
}
//impl of ParamConstr class
public class ParamConstr implements IParamConstr{
private String name;
private String city;
private int age;
public ParamConstr(String name, String city){
System.out.println("in param constr....");
this.name=name;
this.city=city;
}
public String getDetails(){
System.out.println("in getDetails() method.....");
return name+" : "+city+" : "+age;
}
public void setAge(int age) {
System.out.println("in setter for age.....");
this.age = age;
}
}
//beanconfig.xml
<!--parameterized constr -->
<bean id="pc" class="com.myapp.pojo.ParamConstr">
<!-- Constructor Injection -->
<constructor-arg value="Anu"/>
<constructor-arg value="Hyd"/>
<!-- Setter Injection -->
<property name="age" value="24"/>
</bean>
//Client appln
public class ParamConstrApp {
public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory(new
ClassPathResource("com/myapp/config/beanconfig.xml"));
IParamConstr p=(IParamConstr)factory.getBean("pc");
System.out.println("\nIn Client, mesg from bean ...."+p.getDetails());
}
}
********************************************************************************
*******************
When to use constr inj and when to use setter inj
constr inj -> to set default values, especially for mandatory properties (for
immutability also)
setter inj -> to be able to set values for the optional properties explicitel
y, easy to implement
Note: setter inj are inherited by default
Note: Spring recommends setter inj

Usage of Depending Inj can be observed even outside springs, for eg. in jsp ther
e is an abstract class called 'PageContext' and the 'Tag' interface provides a s
etter injection called setPageContext(PageContext pc) that would be invoked by t
he JSP container.
Container calling a method implicitely
--------------------------------------
// part of the HelloImpl.java
public void openResources(){
//for eg, could include logic for initializing resources
System.out.println("In open resources method .....");
}
public void closeResources(){
//for eg, could include logic for destroying resources
System.out.println("In close resources method .....");
}
//xml file
<bean id="hello" class="com.myapp.pojo.HelloImpl" init-method="autoMethod"
destroy-method="closeResources">
<property name="greeterMesg" value="Welcome to Springs"/>
</bean>
//HelloApp.java, the client appln
public class HelloApp {
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory(new
ClassPathResource("com/myapp/config/beanconfig.xml"));
IHello hello=(IHello)factory.getBean("hello");
System.out.println("\nIn Client, mesg from bean ...."+hello.sayHello());
}
}
//Output
In Hello constr....
In open resources method .....
In say Hello method....
In Client, mesg from bean ....Welcome to Springs

NOTE: such implicitely invoked methods must have only void as return type
NOTE: the destroy method would be called whenever the container decides to destr
oy the instance.

********************************************************************************
*****************
If we want something to be done immed after setter inj, then the pojo class need
s to implement the InitializingBean interface and override afterPropertiesSet()
method.
Similarly, if we want to override destroy() method, we need to implement Disposa
bleBean interface
//part of HelloImpl class
public class HelloImpl implements IHello, InitializingBean {
....
public void afterPropertiesSet() throws Exception {
System.out.println("in after properties set method......");
}
public void destroy() throws Exception {
System.out.println("in destroy method....");
}
}
//no other changes in xml or client required
//Output
In Hello constr....
in set greeter mesg method.....
in after properties set method......
In open resources method .....
In say Hello method....
In Client, mesg from bean ....Welcome to Springs

NOTE: the destroy method would be called whenever the container decides to destr
oy the instance.
********************************************************************************
******************
LIFE CYLE OF A BEAN
-------------------
Instantiation(dependency) -> Injections(Constr,Setter etc.) -> BPP(bean post pro
cessor)
First it constructs a hierarchy (dependency tree) for the beans based on the con
tent of the xml doc
Next it invokes the constructors and setter inj
BPP involves two steps, invokes methods corresponding to init_method and destroy
_method attributes in xml doc
Then performs others methods invoked on the bean before destroying the instance.

JSR250 (few annotations provided are)


@postconstructor
//methods to be invoked after calling constr
@predestroy
//methods to be invoked before destroying instance
@resource
//injecting resources for eg, data source like begin tx, commit tx, roll
back tx

Das könnte Ihnen auch gefallen