Spring的Bean的自动装配

Bean的自动装配

  • 自动装配是Spring满足bean依赖一种方式
  • Spring会在上下文中自动寻找,并自动给bean装配属性

byName自动装配

需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致

<bean id="cat" class="com.lzj.entity.Cat"></bean>
<bean id="dog" class="com.lzj.entity.Dog"></bean>
<!--
	byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid
-->
<bean id="people" class="com.lzj.entity.People" autowire="baName">
	<property name="name" value="lzj"></property>
</bean>

byType自动装配

需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致

<bean id="cat" class="com.lzj.entity.Cat"></bean>
<bean id="dog2" class="com.lzj.entity.Dog"></bean>
<!--
	byName:会自动在容器上下文中查找,和自己对象属性类型相同的bean
-->
<bean id="people" class="com.lzj.entity.People" autowire="byType">
	<property name="name" value="lzj"></property>
</bean>

使用注解实现自动装配

  1. 导入约束(context约束)

    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/>
    
  2. 配置注解的支持: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>
    

@Autowired

直接在属性上使用即可!也可以在set方式上使用

使用Autowired我们可以不用编写Set方法了,前提是这个自动中装配的属性在IOC(Spring)容器中存在,且符合名字byType

如果@Autowired自动装配的环境比较复杂,自动装配无法通过-个注解【@Autowired】 完成的时候、我们可以使用@Qualifier(value=“xx”)去配置@Autowired的使用,指定一个唯一的bean对象注入!

public class People {
    @Autowired
    private Cat cat;
    @Autowired
    @Qualifier(value = "dog222")
    private Dog dog;
    private String name;
}

@Resource注解

  1. 导入相关依赖

    <!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.3.2</version>
    </dependency>
    
  2. 实体类中使用注解

    public class People {
        @Resource
        private Cat cat;
        @Resource(name = "dog22")
        private Dog dog;
        private String name;
    }
    

区别

@Resource和@ Autowired的区别:

  • 都是用来自动装配的,都可以放在属性字段上
  • @ Autowired通过byType的方式实现,若使用byType方式找不到则会使用@Qualifier注解按照byName方式,必须要求这个对象存在
  • @Resource 默认通过byName的方式实现,如果找不到名字,则通过byType实现!如果两个都找不到的情况下,就报错!
  • 执行顺序不同

使用注解进行开发

  1. bean

    1. 导入配置文件,并指定要扫描的包

      <?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.lzj.entity"></context:component-scan>
          <context:annotation-config/>
      
      </beans>
      
    2. 实体类使用注解(@Component)

      //等价于<bean id="user" class="com.lzj.entity.User"/>
      //@Component组件
      @Component
      public class User {
          public String name;
      }
      
  2. 属性如何注入

    public class User {
        //相当于<property name="name" value="lzj"/>
        @Value("lzj")
        public String name;
    }
    
  3. 衍生的注解

    @Component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层!

    • dao [@Repository]

    • service [@Service]

    • controller [ @Controller ]

      这四个注解功能都是一-样的, 都是代表将某个类注册到Spring中,装配Bean

  4. 自动装配置

    • @Autowired :自动装配通过类型。名字

      如果Autowired不能唯一自动装配 上属性,则需要通过**@qualifier**(va1ue=“xxx”)

    • @Nullable 字段标记了这个注解,说明这个字段可以为null;

    • @Resource:自动装配通过名字。类型。

  5. 作用域(@Scope)

    //指定作用域为原型模式
    @Scope("prototype")
    public class User {
        public String name;
    }
    
  6. 小结

    xml与注解:

    • xml更加万能,适用于任何场合!维护简单方便
    • 注解不是自己类使用不了,维护相对复杂!

    xml与注解最佳实践:

    • xml用来管理bean;

    • 注解只负责完成属性的注入;

    • 我们在使用的过程中,只需要注意一个问题:必须让注解生效,就需要开启注解的支持

      <!--    指定要扫描的包,这个包下的注解就会生效-->
          <context:component-scan base-package="com.lzj"></context:component-scan>
          <context:annotation-config/>
      

使用Java的方式配置Spring

实体类

@Component
public class User {
    @Value(value = "lzj")
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}

配置文件

@Configuration
@ComponentScan("com.lzj.entity")//默认会扫描该类所在的包下所有的配置类
@Import(LzjConfig2.class)
public class LzjConfig {
    //注册-个bean ,就相当 于我们之前写的一-个bean 标签
    //这个方法的名字,就相当Fbean标签中的id属性
    //这个方法的返回值,就相当Fbean标签中的cLass属性
    @Bean
    public User getUser(){
        return new User();//就是返回要注入到bean的对象
    }
}

测试类

public class MyTest {
    public static void main(String[] args) {
        //.如果完全使用了配置类方式去做,我们就只能通过AnnotationConfig. 上下文来获取容器,通过配置类的class对象加载!
        ApplicationContext context = new AnnotationConfigApplicationContext(LzjConfig.class);
        User getUser = (User) context.getBean("getUser");
        System.out.println(getUser.getName());
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱学习的大雄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值