解读Spring定义Bean的两种方式:和@Bean

前言

Spring中最重要的概念IOC和AOP,实际围绕的就是Bean的生成与使用。

什么叫做Bean呢?我们可以理解成对象,每一个你想交给Spring去托管的对象都可以称之为Bean。

今天通过Spring官方文档来了解下,如何生成bean,如何使用呢?

1.通过XML的方式来生成一个bean

最简单也是最原始的一种方式,通过XML来定义一个bean,我们来看下其过程

1)创建entity,命名为Student

  1. @Data
  2. @AllArgsConstructor
  3. @NoArgsConstructor
  4. public class Student implements Serializable {
  5.  
  6.      private static final long serialVersionUID = 2088281526481179972L;
  7.      private int id;
  8.      private String name;
  9.      private int age;
  10. }

2)在beans.xml中定义Student

  1.      <!– 1.空值的student –>
  2.      <bean id=“studentNoValue” class=“domain.Student”/>
  3.  
  4.      <!– 2.带值的student –>
  5.      <bean id=“student” class=“domain.Student”>
  6.      <property name=“id” value=“11”/>
  7.      <property name=“age” value=“22”/>
  8.      <property name=“name” value=“jack”/>
  9.      </bean>
  10.  
  11.      <!– 3.全参构造:使用成员变量名称对应 –>
  12.      <bean id=“studentConstruct” class=“domain.Student”>
  13.      <constructor-arg name=“age” value=“22”></constructor-arg>
  14.      <constructor-arg name=“id” value=“11”></constructor-arg>
  15.      <constructor-arg name=“name” value=“jack”></constructor-arg>
  16.      </bean>
  17.  
  18.      <!– 4.全参构造:使用成员变量index对应 –>
  19.      <bean id=“studentConstruct2” class=“domain.Student”>
  20.      <constructor-arg index=“0” value=“11”></constructor-arg>
  21.      <constructor-arg index=“1” value=“jack”></constructor-arg>
  22.      <constructor-arg index=“2” value=“22”></constructor-arg>
  23.      </bean>
  24.  
  25.      <!– 5.全参构造:使用成员变量类型对应 –>
  26.      <bean id=“studentConstruct3” class=“domain.Student”>
  27.      <constructor-arg type=“int” value=“11”></constructor-arg>
  28.      <constructor-arg type=“java.lang.String” value=“jack”></constructor-arg>
  29.      <constructor-arg type=“int” value=“22”></constructor-arg>
  30.      </bean>

总结:可以看到,创建bean的方式多种多样,我们可以通过属性来赋值<property>,也可以通过构造参数来赋值<constructor>,关于构造赋值以上展示了三种方式,我们可以根据自己的需求来选择对应的方式。

3)测试bean

  1. public class ApplicationContextTest {
  2.  
  3.      @Test
  4.      public void testXml(){
  5.      ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(“beans.xml”);
  6.      Student studentNoValue = (Student) applicationContext.getBean(“studentNoValue”);
  7.      Student studentFullValue = (Student) applicationContext.getBean(“studentFullValue”);
  8.      System.out.println(studentNoValue);
  9.      System.out.println(studentFullValue);
  10.  
  11.  
  12.      Student studentConstruct1 = (Student) applicationContext.getBean(“studentConstruct”);
  13.      Student studentConstruct2 = (Student) applicationContext.getBean(“studentConstruct2”);
  14.      Student studentConstruct3 = (Student) applicationContext.getBean(“studentConstruct3”);
  15.      System.out.println(studentConstruct1);
  16.      System.out.println(studentConstruct2);
  17.      System.out.println(studentConstruct3);
  18.  
  19.      Book bookChinese = (Book) applicationContext.getBean(“bookChinese”);
  20.      System.out.println(bookChinese);
  21.      }
  22. }
  23. // res:
  24. Student(id=0, name=null, age=0)
  25. Student(id=11, name=jack, age=22)
  26. Student(id=11, name=jack, age=22)
  27. Student(id=11, name=jack, age=22)
  28. Student(id=11, name=jack, age=22)

2.<bean>标签深入了解

我们刚才介绍了最基本的Bean使用方式,大家会发现<bean>标签还有其他的属性,比如name/scope/lazy-init/init-method/…等,这些是做什么用的呢?我们在实际的工作中怎么使用呢?

1)name属性

在介绍name属性之前,我们先来看下ApplicationContext.getBean()的两种方式

* ApplicationContext.getBean(String name)

* ApplicationContext.getBean(Class<T> requiredType)

第一种方式的这个name是什么呢?我们应该如何定义,又该如何使用呢?

  1. // 上文示例中,我们只是指定了Bean的id和class,如下所示
  2. <bean id=“studentNoValue” class=“domain.Student” />
  3. // 具体获取bean的方式如下:
  4. Student studentNoValue = (Student) applicationContext.getBean(“studentNoValue”);
  5.  
  6. // 可以看到,在没有指定bean的name属性的时候,默认使用id来获取bean,当做name使用
  7. // 如果我们不想根据id获取,那就需要主动指定bean的name属性,如下所示:
  8. <bean id=“studentNoValue” class=“domain.Student” name=“stuName”/>
  9. // 这样在获取的时候,就需要使用指定的名称来获取,再根据id来获取的时候就会报错了
  10. Student studentNoValue = (Student) applicationContext.getBean(“stuName”);
  11.      * 根据Class来获取这种方式很好理解,这个不关心你定义的id或者name是什么,使用如下:
  12.  
  13. Student studentNoValue = (Student) applicationContext.getBean(Student.class);

2)scope属性

可以看到,在使用scope属性的时候,提示有两种输入值,分别是singleton/prototype

这个就代表了Spring-Bean的两种创建模式,单例模式和原型模式

* Spring默认使用单例模式来创建Bean,通过ApplicationContext所获得的bean都是同一个bean(在beanName相同的情况下),我们可以来验证下

  1. Student studentNoValue = (Student) applicationContext.getBean(“stuName”);
  2. Student studentNoValue2 = (Student) applicationContext.getBean(“stuName”);
  3.  
  4. System.out.println(studentNoValue == studentNoValue2);// true

可以看到的是结果输入为true,从工厂类中两次获取的stuName是同一个对象。

* 下面来验证下原型模式

原型模式:每次获取的bean都为一个新的对象

  1. // 修改beans.xml中studentNoValue的scope为prototype
  2. <bean id=“studentNoValue” class=“domain.Student” name=“stuName” scope=“prototype”/>
  3.  
  4. // 然后执行上面的测试代码
  5. Student studentNoValue = (Student) applicationContext.getBean(“stuName”);
  6. Student studentNoValue2 = (Student) applicationContext.getBean(“stuName”);
  7.  
  8. System.out.println(studentNoValue == studentNoValue2);// false

可以看到,输出结果为false,原型模式下从工厂类两次获取的stuName不是同一个对象。

3)init-method和destroy-method方法

见名知意,init-method应该是初始化方法的意思,destroy-method应该是销毁方法的意思。那怎么使用呢?

  1. // 在Student.java中添加init()方法和destroy()方法
  2. public void init(){
  3.      System.out.println(“student init…”);
  4. }
  5.  
  6. public void destroy(){
  7.      System.out.println(“student destroy…”);
  8. }
  9.  
  10. // 在beans.xml中studentNoValue的bean上添加 init-method和destroy-method
  11. <bean id=“studentNoValue” class=“domain.Student” name=“stuName” initmethod=“init” destroymethod=“destroy”/>
  12.  
  13. // 测试方法如下:
  14. ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(“beans.xml”);
  15. Student studentNoValue = (Student) applicationContext.getBean(“stuName”);
  16.  
  17. applicationContext.close();
  18.  
  19. // 执行结果:
  20. student init
  21. student destroy

总结:在获取bean的时候init()方法被执行,在容器被销毁的时候,执行了destroy()方法

根据这个,我们可以在初始化bean和销毁bean的时候做点什么,比如关闭连接,保存记录之类的操作。

延伸:那么初始化init()方法在构造方法之前调用,还是之后调用呢?读者可以自行验证下

总结:还有一些其他属性,笔者就不再一一验证了,下面说一下通过JavaConfig的方法来实现bean的定义。

3.JavaConfig方式的bean定义

JavaConfig是Spring4.x推荐的配置方式,可以完全替代XML的方式定义。

1)如何定义一个Bean

  1. // 创建一个类,命名为SpringConfiguration
  2. @Configuration
  3. public class SpringConfiguration {
  4.      @Bean
  5.      public Student student(){
  6.      return new Student(11,“jack”,22);
  7.      }
  8. }
  9.  
  10. // 使用bean
  11. AnnotationConfigApplicationContext applicationContext
  12.          = new AnnotationConfigApplicationContext(SpringConfiguration.class);
  13. Student student = (Student) applicationContext.getBean(“student”)
  14. System.out.println(student);
  15.  
  16. // res:
  17. Student(id=11, name=jack, age=22)

相对于XML的使用方式而言,JavaConfig的使用方式基本是同步的

* @Configuration等同于<beans></beans>

* @Bean等同于<bean></bean>

* 通过AnnotationConfigApplicationContext来加载JavaConfig

* 方法名student()就等同于<bean>中的id,默认方法名就是beanName

2)@Bean的其他参数

* name属性等同于<bean>的name

* initMethod属性等同于<bean>的init-method

* destroyMethod属性等同于<bean>的destroy-method

* scope这个比较奇怪,不属于@Bean的参数,这是一个单独的注解,使用方式如下

  1. @Bean(name = “stu”,autowire = Autowire.BY_TYPE)
  2. @Scope(value = “singleton”)
  3. public Student student(){
  4.      return new Student(11,“jack”,22);
  5. }

总结

以上全文,我们通过两种方式来定义一个Bean,默认推荐JavaConfig。

还有一些其他属性,笔者就不一一列举了。

这些仅为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

参考:

Spring Framework Reference Documentation

代码地址:github – kldwz/springstudy

标签

发表评论