返回顶部
2 3 4

spring(五)bean的自动装配及纯Java配置spring

5、Bean的自动装配

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

在Spring中有三种装配方式

  1. 在xml中显式配置
  2. 在java中显式配置
  3. 隐式的自动装配bean【重点】

5.1 byName和byType自动装配

省略引用类型ref

    <bean id="cat" class="com.yang.entity.Cat"/>
    <bean id="dog" class="com.yang.entity.Dog"/>

    <!--
        byName:会在容器上下文中查找,和自己对象set方法后面的值相对应的beanid
        byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean
    -->
    <bean id="people" class="com.yang.entity.People" autowire="byName">
        <property name="name" value="yang"/>
    </bean>
    <bean id="people1" class="com.yang.entity.People" autowire="byType">
        <property name="name" value="yang"/>
    </bean>
</beans>

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

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

各有弊端

5.2 使用注解实现自动装配

jdk1.5支持的注解 Spring2.5支持的注解
The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML

使用注解须知:
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

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"
      xmlns:aop="http://www.springframework.org/schema/aop"
      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
       http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd">

   <!--开启注解支持-->
   <context:annotation-config/>
</beans>

5.3 @Autowired与@Resource

@Autowired
直接在属性上使用即可!也可以在set方式上使用
使用Autowired我们可以不用编写set方法了,前提是你这个自动装配的属性在IOC(Spring)容器中存在,且符合名字byType

默认是byType,有相同类型字段时byName

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

image-20210819113622409

@Resource注解,不指定name值,先去判断byName和byType,有一个能注入即成功

小结:@Resource和@Autowired的区别

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


5.4 使用注解开发

在Spring4之后,要使用注解开发,必须保证aop的包导入了

image-20210819114302331

使用注解需要导入context约束,增加注解的支持!

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

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

1. bean注入使用@Componet注解

@Component 组件,放在类上说明这个类被spring管理了,就是一个bean

image-20210819115658066

2. 属性注入使用@Value注解

image-20210819123655599

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

  • dao层 【@Repository】

  • service层 【@Service】

  • controller层 【@Controller】

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

4、自动装配

@Autowired 自动装配通过类型、名字
如果Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value="xxx")
@Nullable 字段标记了这个注解,说明这个字段可以为null
@Resource 自动装配通过名字,类型

5. 作用域
@Scope(“singleton”)单例

6. 小结
XML 与 注解

  • xml更加万能,适用于任何场合!维护简单方便
  • 注解不是自己类使用不了, 维护相对复杂
    XML 与 注解最佳实践
  • xml用来管理bean
  • 注解只负责完成属性的注入
  • 我们在使用过程中,只需要注意一个问题:必须让注解生效,就需要开启注解的支持

5.5 使用java的方式配置Spring

完全脱离xml,实质还是使用注解

JavaConfig时spring的一个子项目,在spring4之后,成为了一个核心功能

环境

实体类:

@Component
public class User {

    private String name;

    public String getName() {
        return name;
    }

    @Value("wen")
    public void setName(String name) {
        this.name = name;
    }

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

配置类:

// 配置类
@Configuration
@ComponentScan("com.wen.pojo")
public class SpringConfig {

    @Bean
    public User getUser() {
        return new User();
    }
}

image-20210819132325440

测试类:

image-20210819132538282

不加@Component就是单例

posted @ 2021-08-19 13:29  硫没有正七价  阅读(62)  评论(0编辑  收藏  举报