部分内容来自以下博客:
https://www.cnblogs.com/duanxz/p/7493276.html
1 概述
在使用Spring时,有两种方式管理Spring容器中的Bean,一种是通过XML文件进行管理,一种是通过注解进行管理。
为了能完全通过注解进行开发,Spring也提供了用于取代XML配置文件的注解。
2 代替配置文件
2.1 要求
使用Configuration注解可以将一个类作为配置类,替代XML配置文件,配置类需要满足以下要求:
不能是被final修饰的类。
不能是匿名类。
2.2 创建配置类
使用Configuration注解修饰配置类:
@Configuration
public class DemoConfiguration {
public DemoConfiguration() {
System.out.println("DemoConfiguration DemoConfiguration() ...");
}
}
2.3 加载配置类
不再使用之前通过加载配置文件的方式获取容器,而是通过加载配置类的方式获取容器。
在测试类的方法中获取Spring容器:
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DemoConfiguration.class);
这一步相当于之前的代码:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
还可以在配置类中引入其他配置类:
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(DemoConfiguration.class);
context.refresh();
3 扫描
3.1 说明
使用ComponentScan注解扫描Bean,类似于在配置文件中使用component-scan扫描器。
ComponentScan注解的属性有:
basePackages:设置扫描类的路径。
basePackageClasses:设置扫描类的类名。
includeFilters:设置包含的过滤器。
excludeFilters:设置排除的过滤器。
3.2 标识
在类上使用注解将类作为一个Bean:
@Component
public class Demo {
public Demo() {
System.out.println("Demo Demo() ...");
}
public void init() {
System.out.println("Demo init() ...");
}
public void destroy() {
System.out.println("Demo destroy() ...");
}
}
3.3 使用
在配置类上使用ComponentScan注解并设置扫描的属性:
@Configuration
@ComponentScan(basePackages="com.demo.bean")
public class DemoConfiguration {
public DemoConfiguration() {
System.out.println("DemoConfiguration DemoConfiguration() ...");
}
}
4 配置Web应用程序
如果要在Web应用程序中使用程序式的配置,需要在web.xml中将contextClass这个属性值设为AnnotationConfigWebApplicationContext类,如果不指定,系统将加载默认的WebApplicationContext类来构建容器。
此外,使用WebApplicationContext类时需要使用contextConfigLocation指定配置文件,当使用AnnotationConfigWebApplicationContext类时,需要指定配置类。
添加并修改:
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.demo.config.DemoConfiguration</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
5 引入其他配置
5.1 引入XML配置文件
使用ImportResource注解引入其他XML配置:
@Configuration
@ImportResource("classpath:spring.xml")
public class DemoConfiguration {
public DemoConfiguration() {
System.out.println("DemoConfiguration DemoConfiguration() ...");
}
}
5.2 引入配置类
使用注解引入其他配置类:
@Configuration
@Import(TestConfiguration.class)
public class DemoConfiguration {
public DemoConfiguration() {
System.out.println("DemoConfiguration DemoConfiguration() ...");
}
}
6 EnableXxx注解
6.1 EnableAsync注解
在类上使用EnableAsync注解可以开启程序中的异步多线程功能,相当于XML配置文件中task名称空间的annotation-driven标签。
6.2 EnableScheduling注解
在类上使用EnableScheduling注解可以开启程序中的任务调度功能,相当于XML配置文件中task名称空间的annotation-driven标签。