Spring详细内容-Spring 中的自动装配 En(3)

红太狼 2023-09-24 19:11 131阅读 0赞

1. Autowiring in Spring

  • Autowiring feature of spring framework enables you to inject the object dependency implicitly. It internally uses setteror constructor 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

  1. <bean id="a" class="org.sssit.A" autowire="byName"></bean>

This class contains a constructor and method only.

  1. public class B {
  2. B(){
  3. System.out.println("b is created");}
  4. void print(){
  5. System.out.println("hello b");}
  6. }

This class contains reference of B class and constructor and method.

  1. public class A {
  2. B b;
  3. A(){
  4. System.out.println("a is created");}
  5. public B getB() {
  6. return b;
  7. }
  8. public void setB(B b) {
  9. this.b = b;
  10. }
  11. void print(){
  12. System.out.println("hello a");}
  13. void display(){
  14. print();
  15. b.print();
  16. }
  17. }
  18. <?xml version="1.0" encoding="UTF-8"?>
  19. <beans
  20. xmlns="http://www.springframework.org/schema/beans"
  21. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22. xmlns:p="http://www.springframework.org/schema/p"
  23. xsi:schemaLocation="http://www.springframework.org/schema/beans
  24. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  25. <bean id="a" class="com.amy.A" autowire="byName"></bean>
  26. <bean id="b" class="com.amy.B"></bean>
  27. </beans>

This class gets the bean from the applicationContext.xml file and calls the display method.

  1. public class Test {
  2. public static void main(String[] args) {
  3. ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");
  4. A a= (A) context.getBean("a");
  5. a.display();
  6. }
  7. }
  8. public class Test {
  9. public static void main(String[] args) {
  10. ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");
  11. A a= context.getBean("a",A.class);
  12. a.display();
  13. }
  14. }

two methods are same

  1. public class Test {
  2. public static void main(String[] args) {
  3. Resource resource = new ClassPathResource("applicationContext.xml");
  4. BeanFactory factory = new XmlBeanFactory(resource);
  5. A a= (A) factory.getBean("a");
  6. a.display();
  7. }
  8. }
  9. a is created
  10. b is created
  11. hello a
  12. 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

  1. <bean id="a" class="com.amy.A" autowire="byName"></bean>
  2. <bean id="b" class="com.amy.B"></bean>

Incorrect

  1. <bean id="a" class="com.amy.A" autowire="byName"></bean>
  2. <bean id="b1" class="com.amy.B"></bean>
  3. a is created
  4. b is created
  5. hello a
  6. Exception in thread "main" java.lang.NullPointerException
  7. at com.amy.A.display(A.java:21)
  8. 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 and throw exception.



3) constructor autowiring mode

  • In case of constructor autowiring mode, spring container injects the dependency by highest parameterized constructor.
  • If you have 3 constructorsin a class, zero-arg, one-arg and two-arg then injection will be performed by calling 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.NullPointerException

    1. at com.amy.A.display(A.java:21)
    2. 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.

  1. public class A {
  2. public static A getA(){
  3. //factory method
  4. return new A();
  5. }
  6. }

2) Factory Method Types

There can be three types of factory method:

  1. static factory method that returns instance of its own class
  2. static factory method that returns instance of another class
  3. non-static factory method that returns instance of another class

(1) static factory method that returns instance of its own class

  1. <bean id="a" class="com.amy.A" factory-method="getA"></bean>
  2. public class A {
  3. private static final A obj = new A();
  4. private A(){
  5. System.out.println("private constructor");
  6. }
  7. public static A getA(){
  8. System.out.println("factory method ");
  9. return obj;
  10. }
  11. public void msg(){
  12. System.out.println("hello user");
  13. }
  14. }
  15. <?xml version="1.0" encoding="UTF-8"?>
  16. <beans
  17. xmlns="http://www.springframework.org/schema/beans"
  18. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19. xmlns:p="http://www.springframework.org/schema/p"
  20. xsi:schemaLocation="http://www.springframework.org/schema/beans
  21. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  22. <bean id="a" class="com.amy.A" factory-method="getA"></bean>
  23. </beans>
  24. private constructor
  25. factory method
  26. hello user

(2) static factory method that returns instance of another class

  1. <bean id="b" class="com.amy.A" factory-method="getB"></bean>
  2. public interface Printable {
  3. void print();
  4. }
  5. public class A implements Printable{
  6. @Override
  7. public void print(){
  8. System.out.println("hello a");
  9. }
  10. }
  11. public class B implements Printable{
  12. @Override
  13. public void print(){
  14. System.out.println("hello b");}
  15. }
  16. public class PrintableFactory {
  17. public static Printable getPrintable(){
  18. return new A();
  19. }
  20. }
  21. <?xml version="1.0" encoding="UTF-8"?>
  22. <beans
  23. xmlns="http://www.springframework.org/schema/beans"
  24. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  25. xmlns:p="http://www.springframework.org/schema/p"
  26. xsi:schemaLocation="http://www.springframework.org/schema/beans
  27. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  28. <bean id="pf" class="com.amy.PrintableFactory" factory-method="getPrintable"></bean>
  29. </beans>
  30. public class Test {
  31. public static void main(String[] args) {
  32. ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");
  33. Printable p= (A) context.getBean("pf");
  34. p.print();
  35. }
  36. }
  37. hello a

(3)non-static factory method that returns instance of another class

  1. <bean id="a" class="com.amy.A"></bean>
  2. <bean id="b" class="com.amy.A" factory-method="getB" factory-bean="a"></bean>
  3. public interface Printable {
  4. void print();
  5. }
  6. public class A implements Printable{
  7. @Override
  8. public void print(){
  9. System.out.println("hello a");
  10. }
  11. }
  12. public class B implements Printable{
  13. @Override
  14. public void print(){
  15. System.out.println("hello b");}
  16. }
  17. public class Test {
  18. public static void main(String[] args) {
  19. ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");
  20. Printable p= (A) context.getBean("pf");
  21. p.print();
  22. }
  23. }

There are difference between static factory method and non-static factory method

  1. public class PrintableFactory {
  2. //public static Printable getPrintable(){/
  3. public Printable getPrintable(){
  4. //non-static factory method
  5. return new A();
  6. }
  7. }
  8. <?xml version="1.0" encoding="UTF-8"?>
  9. <beans
  10. xmlns="http://www.springframework.org/schema/beans"
  11. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  12. xmlns:p="http://www.springframework.org/schema/p"
  13. xsi:schemaLocation="http://www.springframework.org/schema/beans
  14. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  15. //static
  16. //<bean id="pf" class="com.amy.PrintableFactory" factory-//method="getPrintable"></bean>
  17. //</beans>
  18. //non-static
  19. <bean id="pf" class="com.amy.PrintableFactory" ></bean>
  20. <bean id="p" class="com.amy.PrintableFactory" factory-method="getPrintable" factory-bean="pf"></bean>
  21. </beans>

发表评论

表情:
评论列表 (有 0 条评论,131人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring自动装配

    自动装配 Spring利用依赖注入(DI),完成对IOC容器中中各个组件的依赖关系赋值 1)、@Autowired:自动注入 默认优先按照类型去容器中找对应