Spring详细内容-Spring 中的自动装配 En(3)
1. Autowiring in Spring
- Autowiring feature of spring framework enables you to
inject the object dependency
implicitly
. It internally usessetter
orconstructor injection
. - Autowiring
can't be used to inject primitive and string values
. It works with reference only.
1)Advantage of Autowiring
It requires the less code because we don’t need to write the code to inject the dependency explicitly.
2)Disadvantage of Autowiring
- No control of programmer.
- It can’t be used for primitive and string values.
3)Autowiring Modes
No. | Mode | Description |
---|---|---|
1) | no | It is the default autowiring mode . It means no autowiring bydefault. |
2) | byName | The byName mode injects the object dependency according to name of the bean. In such case, property name and bean name must be same. It internally calls setter method . |
3) | byType | The byType mode injects the object dependency according to type . So property name and bean name can be different. It internally calls setter method . |
4) | constructor | The constructor mode injects the dependency by calling the constructor of the class. It calls the constructor having large number of parameters. |
Example of Autowiring
Use autowire attribute of bean element to apply the autowire modes
<bean id="a" class="org.sssit.A" autowire="byName"></bean>
This class contains a constructor and method only.
public class B {
B(){
System.out.println("b is created");}
void print(){
System.out.println("hello b");}
}
This class contains reference of B class and constructor and method.
public class A {
B b;
A(){
System.out.println("a is created");}
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
void print(){
System.out.println("hello a");}
void display(){
print();
b.print();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="a" class="com.amy.A" autowire="byName"></bean>
<bean id="b" class="com.amy.B"></bean>
</beans>
This class gets the bean from the applicationContext.xml file and calls the display method.
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");
A a= (A) context.getBean("a");
a.display();
}
}
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");
A a= context.getBean("a",A.class);
a.display();
}
}
two methods are same
public class Test {
public static void main(String[] args) {
Resource resource = new ClassPathResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
A a= (A) factory.getBean("a");
a.display();
}
}
a is created
b is created
hello a
hello b
1) byName autowiring mode
- In case of byName autowiring mode, bean id and reference name must be same.
- It internally uses setter injection.
Correct
<bean id="a" class="com.amy.A" autowire="byName"></bean>
<bean id="b" class="com.amy.B"></bean>
Incorrect
<bean id="a" class="com.amy.A" autowire="byName"></bean>
<bean id="b1" class="com.amy.B"></bean>
a is created
b is created
hello a
Exception in thread "main" java.lang.NullPointerException
at com.amy.A.display(A.java:21)
at com.amy.Test.main(Test.java:20)
2)byType autowiring mode
- In case of byType autowiring mode,
bean id and reference name may be different
. But there must be only one bean of a type.
it works fine because you have created an instance of B type.
- It doesn’t matter that you have different bean name than reference name.
But, if you have multiple bean of one type, it will
not work
andthrow exception
.
3) constructor autowiring mode
- In case of constructor autowiring mode, spring container injects the dependency by
highest parameterized constructor
. If you
have 3 constructors
in a class,zero-arg
,one-arg
andtwo-arg
then injection will be performed bycalling the two-arg constructor
.
4) no autowiring mode
In case of no autowiring mode, spring container doesn’t inject the dependency by autowiring.
a is created
b is created
hello a
Exception in thread “main” java.lang.NullPointerExceptionat com.amy.A.display(A.java:21)
at com.amy.Test.main(Test.java:20)
2.Dependency Injection with Factory Method in Spring
- Spring framework provides facility to
inject bean using factory method
. - To do so, we can use
two attributes of bean element
.
1) Factory method
A method that returns instance of a class is called factory method
.
public class A {
public static A getA(){
//factory method
return new A();
}
}
2) Factory Method Types
There can be three types of factory method:
static factory method
that returnsinstance of its own class
static factory method
that returnsinstance of another class
non-static factory method
that returnsinstance of another class
(1)
static factory method
that returnsinstance of its own class
<bean id="a" class="com.amy.A" factory-method="getA"></bean>
public class A {
private static final A obj = new A();
private A(){
System.out.println("private constructor");
}
public static A getA(){
System.out.println("factory method ");
return obj;
}
public void msg(){
System.out.println("hello user");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="a" class="com.amy.A" factory-method="getA"></bean>
</beans>
private constructor
factory method
hello user
(2)
static factory method
that returnsinstance of another class
<bean id="b" class="com.amy.A" factory-method="getB"></bean>
public interface Printable {
void print();
}
public class A implements Printable{
@Override
public void print(){
System.out.println("hello a");
}
}
public class B implements Printable{
@Override
public void print(){
System.out.println("hello b");}
}
public class PrintableFactory {
public static Printable getPrintable(){
return new A();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="pf" class="com.amy.PrintableFactory" factory-method="getPrintable"></bean>
</beans>
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");
Printable p= (A) context.getBean("pf");
p.print();
}
}
hello a
(3)
non-static factory method
that returnsinstance of another class
<bean id="a" class="com.amy.A"></bean>
<bean id="b" class="com.amy.A" factory-method="getB" factory-bean="a"></bean>
public interface Printable {
void print();
}
public class A implements Printable{
@Override
public void print(){
System.out.println("hello a");
}
}
public class B implements Printable{
@Override
public void print(){
System.out.println("hello b");}
}
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");
Printable p= (A) context.getBean("pf");
p.print();
}
}
There are difference between static factory method and non-static factory method
public class PrintableFactory {
//public static Printable getPrintable(){/
public Printable getPrintable(){
//non-static factory method
return new A();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
//static
//<bean id="pf" class="com.amy.PrintableFactory" factory-//method="getPrintable"></bean>
//</beans>
//non-static
<bean id="pf" class="com.amy.PrintableFactory" ></bean>
<bean id="p" class="com.amy.PrintableFactory" factory-method="getPrintable" factory-bean="pf"></bean>
</beans>
还没有评论,来说两句吧...