Академический Документы
Профессиональный Документы
Культура Документы
BY
P U S H A N B H AT TA C H A RYA
Pushan Bhattacharya
Dependency Injection
Pushan Bhattacharya
Dependency Injection
5
public interface Animal {
void eat();
}
public class Tiger implements Animal {
@Override
public void eat() {
System.out.println(Do not disturb);
}
}
wildLife.xml
<bean id = animal class = Tiger />
Pushan Bhattacharya
So what is Spring?
6
IoC container
Lightweight framework
Fully modularized (High decoupling)
Considered an alternative / replacement for the
Easily testable
Pushan Bhattacharya
What is a bean?
8
Class
Pushan Bhattacharya
Bean
DATA ACCESS
INTEGRATION
T
E
S
T
SPRING
AOP
Core
WEB LAYER
Spring ORM
Spring WEB
Spring DAO
Services
Beans
Context
Pushan Bhattacharya
SPRING WEB
MVC
FRAMEWORK
Expression
Language
Bean / POJO /
Application Class
2
Bean Configuration
Metadata
Input
1
Registered
Spring IoC
Container
Output
Completely Configured
Application
Ready to Use
Pushan Bhattacharya
ApplicationContext
org.springframework.beans.factory.BeanFactory
org.springframework.context.ApplicationContext
ApplicationContext aC = new
ClassPathXmlApplicationContext(wildLi
fe.xml);
Pushan Bhattacharya
Injection Methods
13
Setter Injection
Constructor Injection
Interface Injection
wildLife.xml
Setter Injection
14
Constructor Injection
15
}
<bean id="myCollege" class="com.pushan.study.spring.beans.College">
<constructor-arg type=int value=500/>
<constructor-arg type=java.lang.String value=123Abc/>
</bean>
Pushan Bhattacharya
Bean Scope
Description
singleton
prototype
request
session
global-session
thread
custom
Customized scope
We will look into
singleton and
prototype scopes
only
Pushan Bhattacharya
Pushan Bhattacharya
public class A {
private String msg1;
private String msg2;
public class B {
private String msg1;
private String msg2;
private String msg3;
Pushan Bhattacharya
public class A {
private String msg;
private Person owner;
public Location (String c){ private Location address;
this.city = c;
//
getters and setters ...
}
}
//
...
<bean id="person"
class="Person" />
Pushan Bhattacharya
<bean id="location"
class="Location">
<constructor-arg
value="Kolkata"
type="java.lang.String" />
</bean>
Circular Dependency
25
Bean A
public class A {
private B b;
public A (B b) {
this.b = b;
}
}
Bean B
Pushan Bhattacharya
public class B {
private A a;
public B (A a) {
this.a = a;
}
}
Inner Bean
26
public class A {
private Location address;
// getters and setters
}
public class Location {
private String city, zip;
Pushan Bhattacharya
Injecting Collection
27
Tag Name
Inner Tag
Name
<list>
<value>
java.util.List<E>
Allows duplicate
entries
<map>
<entry>
java.util.Map<K, V>
Key-Value pair of
any object type
<set>
<value>
java.util.Set<E>
<props>
<prop>
java.util.Properties
Key-Value pair of
type String
Pushan Bhattacharya
Java Collection
Type
Specification
Pushan Bhattacharya
Bean
Test class
AnimalCollection aC =(AnimalCollection)
ctx.getBean("animalCollection");
System.out.println(animalList : + aC.getAnimalList());
System.out.println(animalSet: + aC.getAnimalSet());
System.out.println(animalMap: + aC.getAnimalMap());
System.out.println(animalProp: + aC.getAnimalProp());
}
}
Output
Pushan Bhattacharya
Bean Properties
30
Tag Name
id
Description
Example
Unique Id
name
Unique Name
class
scope
Constructor injection
property
Setter injection
autowire
lazy-init
constructor-arg
init-method
destroy-method
Pushan Bhattacharya
org.springframework.beans.factory.DisposableBean
void destroy () throws Exception;
<bean id=myBean"
class=MyBean" init-method="init"/>
<bean id=myBean"
class=MyBean" destroy-method=des"/>
Pushan Bhattacharya
Beans Auto-Wiring
32
<constructor-arg/> tags
It decreases the amount of xml configuration
Mode
no
Description
Removed
in Spring
3.0
Example
Default
byName
byType
constructor
autodetect
Pushan Bhattacharya
public class A {
private String msg;
private Person person;
private Location location;
// getters and setters ...
}
// ...
}
<bean id="person"
class="Person" />
Pushan Bhattacharya
<bean id="location"
class="Location">
<constructor-arg value="Kolkata"
type="java.lang.String" />
</bean>
public class A {
private String msg;
private Person owner;
private Location address;
// getters and setters ...
}
// ...
}
Pushan Bhattacharya
<bean id="location"
class="Location">
<constructor-arg value="Kolkata"
type="java.lang.String" />
</bean>
public class A {
private String msg;
private Person owner;
private Location address;
public A (String m, Person o, Location a){
// ...
<bean id="person"
class="Person" />
Pushan Bhattacharya
<bean id="location"
class="Location">
<constructor-arg value="Kolkata"
type="java.lang.String" />
</bean>
given below.
Disadvantage
Description
Auto wiring is applicable only for beans and not for simple
properties like primitives, String etc.
Pushan Bhattacharya
Auto wiring will always try to wire all the beans through setter
or constructor. It cannot be used for partial wiring.
Pushan Bhattacharya
<context:annotation-config/>
<!-<bean ... />
<bean ... />
.
.
.
<bean ... />
-->
</beans>
Pushan Bhattacharya
@Required
39
Values by name.
@Required
public void setName(String name){
this.name = name;
}
@Required
public void setAge(int age){
this.age = age;
}
//
}
getters ...
Strict checking.
Pushan Bhattacharya
@Autowired
40
getters ...
Pushan Bhattacharya
Wiring by type.
No setter
needed for
autowiring
on field
required attribute
...
student
auto wiring
becomes
optional
@Qualifier
41
getters ...
Pushan Bhattacharya
@Resource(name=boy1)
private Boy student;
//
}
//
}
}
Pushan Bhattacharya
...
Pushan Bhattacharya
...
@Configuration
@Import(TigerConfig.class)
public class LionConfig{
@Bean
public Lion lion(){
return new Lion();
}
}
Pushan Bhattacharya
@Configuration
public class TigerConfig{
@Bean(name=cat)
@Scope(prototype)
public Tiger tiger(){
return new Tiger();
}
}
<bean id=cat
class=Tiger
scope=prototype />
Try Yourself
46
Pushan Bhattacharya
Helping Resources
47
(http://www.springsource.org/documentation)
Tutorials Point tutorial
(http://www.tutorialspoint.com/spring)
Mkyong tutorial
(http://www.mkyong.com/tutorials/springtutorials)
DZone (http://java.dzone.com/articles/case-springinner-beans)
Pushan Bhattacharya
- Pushan Bhattacharya
Pushan Bhattacharya