Spring是"约定优于配置"的原则,XML文件配置的方式也是异常繁琐,影响开发的效率,所以在Spring3之后就支持大量使用注解的方式开发,使用注解代替xml配置可简化开发,提高效率。
什么是注解?注解其实就相当于一个标签,比方说我们在公司中有很多部门,你是后勤部门的就做后勤相关工作,你是开发部门的就去做项目开发,注解就是类似功能。给类添加@Controller注解,那这个类就打上了控制层的标签,就去做控制层相关工作,给你加上@Test就打上测试标签,就去做测试相关工作。所以注解相对于XML的配置,它更加的直观,同时也更加便捷。
Spring默认不使用注解装配 Bean,需要在配置文件中开启注解扫描路径。
在XML文件中添加 context命名空间和约束的配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
beans>
<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">
beans>
注解包的扫描
<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="需要扫描的路径"/>
<context:component-scan base-package="com.yu.springtest">
<context:exclude-filter type="annotation" expression=""/>
context:component-scan>
<context:component-scan base-package="com.yu.springtest" use-default-filters="false">
<context:include-filter type="annotation" expression=""/>
context:component-scan>
beans>
实例化Bean对象的注解
使用注解注入Bean
@Autowired(required=false):用于属性、setter 方法、非 setter 方法及构造函数,表示注入一个对象,默认按照类型进行装配
@Primary:配合Autowired使用,当有多个实现类时,在类上加此注解会告诉IoC容器优先注入此类。但是当两个实现类都加上Primary注解时,还是会出错,它只能解决首要性问题,而非选择性问题。
@Qualifier(value=“名”):结合Autowired(默认为按类注入)使用,它会变成根据名字依赖注入,可以消除Primary注解的问题
@Resource(name=“”):相当于@Autowired+@Qualifier同时使用,默认按名字进行装配(jdk中没有,需要导 javax.annotation-api 或 jakarta.annotation-api依赖)
<dependency>
<groupId>jakarta.annotationgroupId>
<artifactId>jakarta.annotation-apiartifactId>
<version>2.1.1version>
dependency>
<dependency>
<groupId>javax.annotationgroupId>
<artifactId>javax.annotation-apiartifactId>
<version>1.3.2version>
dependency>
如果指定 name 属性,则按实例名称进行装配
如果指定 type 属性,则按 Bean 类型进行装配
如果都不指定,则先按 Bean 实例名称装配,如果不能匹配,则再按照 Bean 类型进行装配
Autowired和Resource对比
1、都是用来进行自动装配的,主要用于属性上
2、Autowired默认是按ByType方式实现的,不配置required时要求对象必须存在
3、Resource默认通过ByName,如果没有名字则按照ByType实现,否则就报错
4、Autowired注解用在属性上、setter方法上、构造方法上、构造方法参数上。
5、Resource注解用在属性上、setter方法上。
@Value:给普通的属性设置值,无法注入对象
作用域
初始方法和销毁方法(需要导javax.annotation包或依赖)
开启注解的支持,并且开启包的扫描
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--注解包的扫描-->
<context:component-scan base-package="扫描的路径"/>
</beans>
定义一个基础的类
使用@Component注解表明是一个普通的组件,使用 @Value(“大黄”)注入普通的属性,@Autowired在构造方法、属性、set方法上注入一个依赖的对象
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class Dog {
@Value("大黄")
private String name;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
//表示为一个基础组件
@Component
public class Person {
/**
* value注解用来注入普通的值
*/
@Value("张三")
private String name;
@Value("22")
private int age;
/**
* 养了条狗,通过注解 Autowired 注入对象
*/
@Autowired
private Dog dog;
}
定义 service 获取
使用 @service 注解标注此类是一个service类
// 表示是service层的一个组件
@Service
public interface AnnotationTestService {
Person getPerson();
}
@Service
public class AnnotationTestServiceImpl implements AnnotationTestService {
/**
* 通过 @Resource 注入IOC容器管理的类
*/
@Resource
private Person person;
@Override
public Person getPerson() {
return person;
}
}
定义一个controller类
使用 @Controller 标注此类是一个Controller, @Autowired注入一个 service

在测试类中获取这个Controller进行测试
