(知识目录)
一、前言
这篇文章主要介绍spring中有关注解开发的知识
二、内容
1.注解开发定义bean
步骤
1.使用@component注解定义bean的名字
2.在核心配置文件使用context:component-scan标签扫描组件
3.验证bean是否创建
可以看到运行结果中出现了BookDaoImpl类中的重写方法的内容,说明使用@Component注解定义bean成功。
等价写法
spring提供@Component注解的三个衍生等价注解
@Repository @Service @Controller分别对应dao层,service层和controller层。
注意事项:
当注解中没有写bean的名字时,只能通过类型获取bean,此时要保证该类型的bean唯一
2.纯注解开发
步骤
1.编写配置类
@Configuration
@ComponentScan("demo3.service")
public class SpringConfig {
}
使用@Configuration注解代替配置文件中的默认内容,使用@ComponentScan注解代替context:component-scan标签。 2.加载配置类并获取bean
public class App_2 {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); // 加载配置类,使用的是AnnotationConfigApplicationContext类
BookService service = context.getBean(BookService.class); // 获取bean
service.bookSerSay();
}
}
运行结果
3.如果要扫描多个包,@ComponentScan的值采用字符串数组的形式
3.注解定义bean的作用范围和生命周期函数
作用范围
使用@Scope注解声明bean的作用范围
生命周期函数
包括自定义初始化函数和销毁函数 定义初始化函数,使用@PostConstructor注解;定义销毁函数 使用@PreDestroy注解。
@PostConstruct
public void init() {
System.out.println("init");
}
@PreDestroy
public void destroy() {
System.out.println("destroy");
}
注意: 在Java9之后这两个注解已经被淘汰了,要使用这两个注解,首先在pom.xml中加入一下依赖:
<!-- java注解相关依赖-->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
下面来看主类和运行结果
4.依赖注入
自动装配和读取properties文件
自动装配 | 读取properties文件 |
---|---|
引用类型 | 简单类型 |
@Autowired注解(按类型) | @Value注解 |
当有多个相同类型的bean时,报错,此时再使用@Qualifier注解指定其中一个 | 读取properties文件时使用${},注意此时仍然可能读取到系统属性 |
读取properties文件内容,首先要在配置类中,使用@PropertySource注解告知spring有哪些文件
5.第三方bean管理(以DruidDataSource为例)
步骤
1.在pom.xml中导入Druid坐标
<!-- druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.0</version>
</dependency>
2.在配置类中写要获取的对象类型的方法(在方法上加@Bean注解)
@Configuration
@ComponentScan({"demo3.service","demo3.controller","demo3.dao"})
public class SpringConfig {
@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("mysql:jdbc://localhost:3306/daily?useSSL=false");
dataSource.setUsername("root");
dataSource.setPassword("@123456");
return dataSource;
}
}
3.获取bean
public class App_5 {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
DataSource dataSource = context.getBean(DruidDataSource.class); //通过方法的返回值类型获取这个bean
System.out.println(dataSource);
}
}
4.查看运行结果
可以看到第三方bean对象已成功创建
注意事项
1.一般情况下不会把JDBC配置信息放在spring配置类中,而是新建一个JdbcConfig类
2.在SpringConfig中使用@Import注解导入这个类
运行结果和上面的运行结果是一致的,如果不是使用@Import注解,需要在JdbcConfig类上加@Configuration注解,在SpringConfig类上的@ComponentScan注解上加上这个config包。
3.@Import注解只能使用一次,如果要导入多个配置类,要使用数组的格式
6.第三方bean依赖注入
简单类型 | 引用类型 |
---|---|
@Value注解+成员变量 | 方法形参 |
如果没有形参这个类型的bean,就会报错。
7.xml配置与注解配置对比
三、结语
这篇文章主要讲了:
spring中如何使用@Component注解开发,以及@Component注解的三个衍生注解; 不使用配置文件,而使用配置类的注解开发; bean的作用范围@Scope和声明周期函数@PostConstructor和@PreDestroy; 依赖注入中使用到@Autowired和@Value注解分别注入引用类型和简单类型; 如何使用第三方bean和注入依赖,并对比了xml和注解方式的异同。