Spring定义bean的三种方式和自动注入

本文内容纲要:

- 一、定义bean的三种途径:

- 二、Spring的自动注入

- 三、如何进行选择?

一、定义bean的三种途径:

  • 首先编写Student和Teacher两个类
  •  public class Student {  

    private String name;

    private Teacher teacher;

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    public Teacher getTeacher() {

    return teacher;

    }

    public void setTeacher(Teacher teacher) {

    this.teacher = teacher;

    }

    }

    public class Teacher {

    private String name;

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    }

    • 方法一:基于XML的bean定义(需要提供setter方法)

    •  <?xml version="1.0" encoding="UTF-8"?>  

      <beans xmlns="http://www.springframework.org/schema/beans"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

      <bean id="student" class="test.Student">

      <property name="name" value="张三"/>

      <property name="teacher" ref="teacher"/>

      </bean>

      <bean id="teacher" class="test.Teacher">

      <property name="name" value="李四"/>

      </bean>

      </beans>

      [java] view plain copy

      public class Main {

      public static void main(String args[]){

      FileSystemXmlApplicationContext context=new FileSystemXmlApplicationContext("applicationContext.xml的绝对路径");

      Student student= (Student) context.getBean("student");

      Teacher teacher= (Teacher) context.getBean("teacher");

      System.out.println("学生的姓名:"+student.getName()+"。老师是"+student.getTeacher().getName());

      System.out.println("老师的姓名:"+teacher.getName());

      }

      }

      • 方法二:基于注解的bean定义(不需要提供setter方法)

      Spring为此提供了四个注解,这些注解的作用与上面的XML定义bean效果一致,在于将组件交给Spring容器管理。组件的名称默认是类名(首字母变小写),也可以自己修改:

      @Component:当对组件的层次难以定位的时候使用这个注解

      @Controller:表示控制层的组件

      @Service:表示业务逻辑层的组件

      @Repository:表示数据访问层的组件

      使用这些注解的时候还有一个地方需要注意,就是需要在applicationContext.xml中声明contex:component-scan...一项,指明Spring容器扫描组件的包目录。

    •  @Component("teacher")  

      public class Teacher {

      @Value("李四")

      private String name;

      public String getName() {

      return name;

      }

      }

      [java] view plain copy

      @Component("student")

      public class Student {

      @Value("张三")

      private String name;

      @Resource

      private Teacher teacher;

      public String getName() {

      return name;

      }

      public Teacher getTeacher() {

      return teacher;

      }

      }

      [html] view plain copy

      <?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:context="http://www.springframework.org/schema/context"

      xsi:schemaLocation="http://www.springframework.org/schema/beans

      http://www.springframework.org/schema/beans/spring-beans.xsd

      http://www.springframework.org/schema/context

      http://www.springframework.org/schema/context/spring-context.xsd">

      <!--扫描组件的包目录-->

      <context:component-scan base-package="test"/>

      </beans>

      [java] view plain copy

      public class Main {

      public static void main(String args[]){

      FileSystemXmlApplicationContext context=new FileSystemXmlApplicationContext("applicationContext.xml的绝对路径");

      Student student= (Student) context.getBean("student");

      Teacher teacher= (Teacher) context.getBean("teacher");

      System.out.println("学生的姓名:"+student.getName()+"。老师是"+student.getTeacher().getName());

      System.out.println("老师的姓名:"+teacher.getName());

      }

      }

      • 方法三:基于Java类的bean定义(需要提供setter方法)

      •  @Configuration  

        public class BeansConfiguration {

        @Bean

        public Student student(){

        Student student=new Student();

        student.setName("张三");

        student.setTeacher(teacher());

        return student;

        }

        @Bean

        public Teacher teacher(){

        Teacher teacher=new Teacher();

        teacher.setName("李四");

        return teacher;

        }

        }

        [java] view plain copy

        public class Main {

        public static void main(String args[]){

        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(BeansConfiguration.class);

        Student student= (Student) context.getBean("student");

        Teacher teacher= (Teacher) context.getBean("teacher");

        System.out.println("学生的姓名:"+student.getName()+"。老师是"+student.getTeacher().getName());

        System.out.println("老师的姓名:"+teacher.getName());

        }

        }

        二、Spring的自动注入

        • Spring提供了五种自动装配的类型

        no:顾名思义, 显式指明不使用Spring的自动装配功能

        byName:根据属性和组件的名称匹配关系来实现bean的自动装配

        byType:根据属性和组件的类型匹配关系来实现bean的自动装配,有多个适合类型的对象时装配失败

        constructor:与byType类似是根据类型进行自动装配,但是要求待装配的bean有相应的构造函数

        autodetect:利用Spring的自省机制判断使用byType或是constructor装配

        • 基于XML的自动装配

          <bean id="student" class="test.Student" autowire="byName">  

          <property name="name" value="张三"/>

          </bean>

          <bean id="teacher" class="test.Teacher">

          <property name="name" value="李四"/>

          </bean>

        这里我并没有显式为Student对象注入Teacher属性,而是使用autowired="byName"代替,这样一来Spring会帮我们处理这些细节,将名字是teacher的组件注入到Student对象中。

        • 基于注解的自动装配

        其实上面已经应用过了,这里再提一下@Resource和@Autowired的区别。@Resource默认是使用byName进行装配,@Autowired默认使用byType进行装配。

         @Component("teacher")  

        public class Teacher {

        @Value("李四")

        private String name;

        public String getName() {

        return name;

        }

        public void setName(String name) {

        this.name = name;

        }

        }

        @Component("student")

        public class Student {

        @Value("张三")

        private String name;

        @Resource

        private Teacher teacher;

        public String getName() {

        return name;

        }

        public void setName(String name) {

        this.name = name;

        }

        public Teacher getTeacher() {

        return teacher;

        }

        public void setTeacher(Teacher teacher) {

        this.teacher = teacher;

        }

        }

        三、如何进行选择?

          • <property name="driverClassName" value="com.mysql.jdbc.Driver"/>  

            <property name="url" value="jdbc:mysql://localhost:3306/study"/>

            <property name="username" value="账号"/>

            <property name="password" value="密码"/>

            </bean>

            自动装配:一般我使用基于注解的自动装配。同样也是为了减少XML配置文件的“篇幅”。

本文内容总结:一、定义bean的三种途径:,二、Spring的自动注入,三、如何进行选择?,

原文链接:https://www.cnblogs.com/yimian/p/8286558.html

以上是 Spring定义bean的三种方式和自动注入 的全部内容, 来源链接: utcz.com/z/296094.html

回到顶部