1.1 简介
1.1.1 概述
Spring 应用支持多种配置方式,除了 XML 配置外,开发人员更加青睐使用基于注解的配置。基于注解的配置方式,允许开发人员将配置信息移入组件类本身中,在相关的类、方法或字段上声明使用注解。使用注解之后,就不需要在 XML 文件中进行复杂的配置了,Spring 会自动帮我们搞定。Spring 启动时会去扫描注解,根据不同的注解会进行不同的操作。
1.1.2 启用注解
要想使用注解,必须在 applicationContext.xml 中添加一段配置开启注解扫描,需要导入 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"
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">
<!-- 开启注解扫描 base-package 指定扫描的包(包含子包)-->
<context:component-scan base-package="com.softrware.spring"></context:component-scan>
</beans>
1.2 注解介绍
1.2.1 @Component
Spring 2.5 引入了更多的典型注解,如 @Repository、@Component、@Service 和 @Controller。@Component 注解是将对象的创建权交给 Sring 容器,即把普通 pojo 实例化到 Spring 容器中,相当于配置文件中的<bean id="" class=""/>
。@Repository,@Service 和 @Controller 注解相较于 @Component 注解则有特殊的语义,分别对应了持久层、服务层和表现层。因此,对于服务层,如果在 @Component 和 @Service 注解之间进行选择,那么 @Service 注解无疑是更好的选择。同样,在持久层中,@Repository 注解已经支持作为自动异常转换的标记。
/**
* Created with IntelliJ IDEA.
*
* @author Demo_Null
* @date 2020/8/20
* @description @Component
*/
@Component
public class Run {
public void run() {
System.out.println("run...");
}
}
1.2.2 @Required
@Required 注解应用于 Bean 属性的 setter 方法上,使用该注解的 Bean 的属性必须在 Bean 的定义中或自动装配中必须赋予明确的属性值。使用该注解的 Bean 的属性没有赋予明确的属性值,那么容器就会抛出异常。这就是通过快速失败的机制来避免空指针。
1.2.3 @Autowired
这是 Spring 提供的一个注解,默认是按照类型装配(by-type),要求容器中一定要有这个类型的对象,如果没有将会报错,抛出异常。也可以通过设置可以 @Autowired(required = false),来告诉容器,如果没有,可以不注入。当容器中有多个相同类型的对象,属性的名称作为组件的 id 去容器中查找,可以通过 @Qualifier(“name”),来指定装配哪个对象。
/**
* Created with IntelliJ IDEA.
*
* @author Demo_Null
* @date 2020/8/20
* @description 示例
*/
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:application.xml")
public class Demo {
@Autowired
private Run runImpl;
private Run runSet;
@Autowired
public void setRunSet(Run runSet) {
this.runSet = runSet;
}
@Test
public void main() {
System.out.println(runImpl);
System.out.println(runSet);
}
}
1.2.4 @Resources
@Resources注解是属于 JSP- 250 的一个注解,他可以使用 by-name 和 by-type 来进行自动装配。当设置了 by-name 和 by-type 即 @Resource(name = "name", type = "Entity.class")
,根据设置的条件到 IOC 中注入唯一的对象;当只设置了 by-name,则按照 by-name 装配,如果没有那么抛出异常。当只设置了 by-type,那么按照类型装配,如果 IOC 容器当中存在多个,或不存在,抛出异常;如果 by-name 和 by-type 都没有指定,那么先按 by-name 查找,如果 by-name 没查找到,那么就进行 by-type 查找,如果都没查找到则抛出异常。
1.2.5 @Primary
因为通过类型的自动装配可能有多个类型相同的类,那么在通常需要使用 @Primary 指定了一个优先的 Bean 来进行控制。当多个候选者 Bean 中存在一个确切的指定了 @Primary 的 Bean 时,就会自动装载这个 Bean,否则可能会抛出异常。
/**
* Created with IntelliJ IDEA.
*
* @author Demo_Null
* @date 2020/8/20
* @description 接口
*/
public interface Run {
void run();
}
/**
* Created with IntelliJ IDEA.
*
* @author Demo_Null
* @date 2020/8/20
* @description 实现类_1
*/
@Component
@Primary
public class RunImpl_01 implements Run {
public void run() {
System.out.println("一号实现类");
}
}
/**
* Created with IntelliJ IDEA.
*
* @author Demo_Null
* @date 2020/8/20
* @description 实现类_2
*/
@Component
public class RunImpl_02 implements Run {
public void run() {
System.out.println("二号实现类");
}
}
/**
* Created with IntelliJ IDEA.
*
* @author Demo_Null
* @date 2020/8/20
* @description 测试类
*/
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:application.xml")
public class Demo {
@Autowired
private Run runImpl;
@Test
public void main() {
runImpl.run();
}
}