Spring 自动装配bean注解

什么是bean自动装配?

  1. 自动装配是Spring满足bean依赖的一种方式
  2. Sping会在配置文件上下文中自动寻找,并给bean转配属性

装配方式(Autowired)

  1. XML中显示配置
  2. 在java中显示配置
  3. 隐式的自动装配

autowire属性

属性对应值如下
属性值

byName:会自动在容器中查找和而自己对象set方法后面的值对应的beanid(如果使用自动创建的set方法,set后面的第一个会默认变小写)

1
2
在这里插入图片描述

byType:会自动在容器中查找和而自己对象属性相同的beanid;!!要保证类型全局唯一==bean的class唯一!!

4

<bean name="people" class="com.liu.pojo.Person" autowire="byType">
          <property name="name" value="lili"/>
 </bean>

注解@Autowired(使用此属性可以忽略set方法)

  1. 首先要使用该注解必须更新配置文件
    添加的语句是:
    xmlns:context=“http://www.springframework.org/schema/context” http://www.springframework.org/schema/context
    https://www.springframework.org/schema/context/spring-context.xsd
    context:annotation-config/
<?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
       https://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">
   <context:annotation-config/>
</beans>

测试类:

import com.liu.pojo.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
    
    
    @Test
    public void usuallyTest(){
    
    
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Person people = context.getBean("people", Person.class);
        System.out.println(people);
    }
}

结果如图:
在这里插入图片描述

  1. 自动装配,一般放在属性字段上
法1:单独使用@Autowirted

@Autowired默认按照byType装配对象


xml配置文件

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
   <context:annotation-config/>
   <bean name="animal" class="com.liu.pojo.Animal"/>
   <bean name="cat" class="com.liu.pojo.Cat"/>
   <bean name="dog" class="com.liu.pojo.Dog"/>
   <bean id="people" class="com.liu.pojo.Person">
      <property name="name" value="lili" />
   </bean>
</beans>
  	package com.liu.pojo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.lang.Nullable;

public class Person {
    
    
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;
    public Cat getCat() {
    
    
        return cat;
    }
    public void setCat(@Nullable Cat cat) {
    
    
        this.cat = cat;
    }

    public Dog getDog() {
    
    
        return dog;
    }
    public void setDog(@Nullable Dog dog) {
    
    
        this.dog = dog;
    }
    public String getName() {
    
    
        return name;
    }
    public void setName(String name) {
    
    
        this.name = name;
    }
    @Override
    public String toString() {
    
    
        return name + "拥有" + cat.getName() +
                dog.getName();
    }
}

法2:@Autowired配合@Qualifier(value = “XXX”)使用,指定对象《==》byName
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
   <context:annotation-config/>
   <bean name="animal" class="com.liu.pojo.Animal"/>
   <bean id="cat1" class="com.liu.pojo.Cat"/>
   <bean id="dog1" class="com.liu.pojo.Dog"/>
   <bean id="people" class="com.liu.pojo.Person">
      <property name="name" value="lili" />
   </bean>
</beans>
  @Autowired
    @Qualifier(value = "cat1")
    private Cat cat;
    @Autowired
    @Qualifier(value = "dog1")
    private Dog dog;
    private String name;
扩展:法3( @Resource 或者@Resource(name = “dog”))使用与@Autowirted类似

注意: @Resource 通过的是属性name

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
   <context:annotation-config/>
   <bean name="animal" class="com.liu.pojo.Animal"/>
   <bean name="cat" class="com.liu.pojo.Cat"/>
   <bean name="dog" class="com.liu.pojo.Dog"/>
   <bean id="people" class="com.liu.pojo.Person">
      <property name="name" value="lili" />
   </bean>
</beans>
    @Resource(name = "cat")
    private Cat cat;
    @Resource(name = "dog")
    private Dog dog;
    private String name;

注解( @Resource,@Autowired)小结:

  • 两者都可自动装配,都可以写在属性上
  • @Autowired默认通过bytpe方式实现,而且必须要求这个对象存在
  • @Resource默认通过byname方式实现,如果找不到名字则通过bytype实现。如果两种方式都找不到,就报错

@component注解

使用准备

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
   <!--包扫描,在这个包下的注解都会生效-->
   <context:component-scan base-package="com.liu.pojo"/>
   <context:annotation-config/>
</beans>

使用

@Component(作用在类上)相当于 <bean name="" class=""/>
@value(作用在属性上)相当于  <property name="" value=""/>
package com.liu.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Student {
    
    
    @Value("李四")
    private String sName;
    @Value("19")
    private String sId;

    public String getsName() {
    
    
        return sName;
    }

    public void setsName(String sName) {
    
    
        this.sName = sName;
    }

    public String getsId() {
    
    
        return sId;
    }

    public void setsId(String sId) {
    
    
        this.sId = sId;
    }

    @Override
    public String toString() {
    
    
        return "Student{" +
                "sName='" + sName + '\'' +
                ", sId='" + sId + '\'' +
                '}';
    }
}

XML与注解小结

  • xml更万能,适用于任何场景,维护简单
  • 注解不是自己的类是用不了,维护相对复杂
  • 若要让注解生效,必须开启注解支持

使用java方式配置Spring

使用到的注解:@Configuration @Bean

  1. 创建一个java文件(BeanConfig.java)用于配置
@Configuration  (作用在类上)相当于<beans/>
@Bean(作用在方法上)相当于  <bean name="" class=""/>
@import(.class) 导入其他配置文件
import com.liu.pojo.Teacher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeanConfig {
    
    

    @Bean
    public Teacher getTeacher() {
    
    
        return new Teacher();
    }
}
  1. 使用 @Bean注册对象
    Teacher .java
package com.liu.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
public class Teacher {
    
    
    @Value("张三")
    private String tName;
    @Value("0001")
    private String tId;

    public String gettName() {
    
    
        return tName;
    }

    public void settName(String tName) {
    
    
        this.tName = tName;
    }

    public String gettId() {
    
    
        return tId;
    }

    public void settId(String tId) {
    
    
        this.tId = tId;
    }

    @Override
    public String toString() {
    
    
        return "Teacher{" +
                "tName='" + tName + '\'' +
                ", tId='" + tId + '\'' +
                '}';
    }
}
  1. 使用@Component注册对象,beanid默认类名第一个字母小写
    Teacher .java
package com.liu.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Teacher {
    
    
    @Value("张三")
    private String tName;
    @Value("0001")
    private String tId;

    public String gettName() {
    
    
        return tName;
    }

    public void settName(String tName) {
    
    
        this.tName = tName;
    }

    public String gettId() {
    
    
        return tId;
    }

    public void settId(String tId) {
    
    
        this.tId = tId;
    }

    @Override
    public String toString() {
    
    
        return "Teacher{" +
                "tName='" + tName + '\'' +
                ", tId='" + tId + '\'' +
                '}';
    }
}

  1. 测试:
import com.liu.appConfig.BeanConfig;
import com.liu.pojo.Teacher;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SpringTest {
    
    
    @Test
    public void ComponentTest(){
    
    
        ApplicationContext context = new AnnotationConfigApplicationContext(BeanConfig.class);
        //通过@Bean注册
        //Teacher getTeacher = context.getBean("getTeacher", Teacher.class);
        //通过@ComponentScan()包注册,让类中的注解生效@Component
        Teacher user = context.getBean("teacher", Teacher.class);
        System.out.println(user);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_44774287/article/details/123923247