有勇气的牛排博客

Spring (二) Bean自动装配、注解开发

有勇气的牛排 846 Java 2022-06-29 23:49:59

哈喽,大家好,我是有勇气的牛排(全网同名)🐮🐮🐮

有问题的小伙伴欢迎在文末评论,点赞、收藏是对我最大的支持!!!。

文章目录

7 Bean的自动装配@Autowired

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

在Spring中有三种装配方式

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

7.1 ByName自动装配

<!-- 自动装配 autowire --> <bean id="cat" class="com.steak.pojo.Cat"/> <bean id="dog" class="com.steak.pojo.Dog"/> <!-- byname: 会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid --> <bean id="people" class="com.steak.pojo.People" autowire="byName"> <property name="name" value="有勇气的牛排"/> </bean>

7.2 ByType自动装配

类型,全局唯一

<!-- 自动装配 autowire --> <!-- 自动装配 autowire --> <bean id="cat" class="com.steak.pojo.Cat"/> <bean id="dog" class="com.steak.pojo.Dog"/> <!-- byType:会自动在容器上下文中查找,和自己对象属性类型相同的beanid --> <bean id="people" class="com.steak.pojo.People" autowire="byType"> <property name="name" value="有勇气的牛排"/> </bean>

小结:

  • byname:需要保证所有的bean的id唯一,并且这个bean需要和自动注入的属性set方法的值一致。
  • bytype:需要保证所有bean的class一致,并且这个bean需要和自动注入的属性的类型一致。

7.3 使用注解实现自动装配

jdk1.5支持的注解,Spring2.5就支持注解了

  1. 导入命名空间context
  2. 配置注解的支持

beans_context.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 id="cat" class="com.steak.pojo.Cat"/> <bean id="dog" class="com.steak.pojo.Dog"/> <bean id="people" class="com.steak.pojo.People"/> </beans>

People.java

public class People { @Autowired private Cat cat; @Autowired private Dog dog; private String name; ... }

@Autowired:

  • 可以在属性上使用,也可以在set方法上使用
  • 使用@Autowired我们可以不用编写Set方法,前提这个自动装配属性在IOC(Spring)容器,且符合名字ByName

https://www.bilibili.com/video/BV1WE411d7Dv?p=13&spm_id_from=pageDriver&vd_source=86d96d974a484abdae182476b20a8ede

@Nullable

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

public class People {
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;

    public People(@Nullable String name){
        this.name = name;
    }
}

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

public class People { @Autowired @Qualifier(value="cat") // 指定实现装配的值 private Cat cat; @Autowired @Qualifier(value="dog") // 指定实现装配的值 private Dog dog; private String name; ... }

@Resource

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

8 使用注解开发

注解说明:

  • @Autowired:自动装配(通过类型、名字),如果不能唯一自动装配上属性,则需要通过@Qualifier(value=“xxx”)
  • @Nullable:标记为这个的字段可以为null
  • @Resource:自动装配(通过名字、类型)
  • @Component:放在类上,说明这个类被Spring管理了,就是bean

Spring4之后,要使用注解开发,必须导入aop包

<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.2.20.RELEASE</version> </dependency>

8.1 bean

ApplicationContext.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/> </beans>

1、属性注入

/** * 属性注入 * @Component * 等价于 <bean id="user" class="com.steak.pojo.User"/> * */ @Component public class User { /** *@Value("灰太狼") * 相当于 * <bean id="user" class="com.steak.pojo.User"> * <property name="name" value="灰太狼"/> * </bean> * */ @Value("灰太狼") public String name; }

2、衍生注解

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

  • dao— 【@Repository】
  • service — 【@Service】
  • controller — 【@Controller】

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

3、自动装配

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

4、作用域

@Scope("singleton") // 作用域: singleton单例,prototype原型模式 public class User { /** *@Value("灰太狼") * 相当于 * <bean id="user" class="com.steak.pojo.User"> * <property name="name" value="灰太狼"/> * </bean> * */ @Value("灰太狼") public String name; }

小结:
xml与注解

  • xml更强,使用任何场合,维护简单方便
  • 注解,不是自己的类使用不了,维护相对复杂

最佳实践:

  • xml用来管理bean
  • 注解负责完成属性注入

9 使用Java的方式配置Spring

不再使用Spring配置xml,全部交给java

JavaConfig是Spring的一个子项目,在Spring4之后,成为核心项目 appconfig

配置类

package com.steak.config; import com.steak.pojo.User; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @Configuration: 代表这是一个配置类 * * */ @Configuration public class MyConfig { @Bean public User getUser(){ return new User(); } }

测试类

import com.steak.config.MyConfig; import com.steak.pojo.User; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class MyTest { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class); User getUser = (User) context.getBean("getUser"); System.out.println(getUser.getName()); } }

留言

专栏
文章
加入群聊