002SpringIOC005完全注解开发
部分内容来自以下博客:
https://www.cnblogs.com/duanxz/p/7493276.html
1 概述
在使用Spring时,有两种方式管理Spring容器中的Bean,一种是通过XML文件进行管理,一种是通过注解进行管理。
为了能完全通过注解进行开发,Spring也提供了用于取代XML配置文件的注解。
2 代替配置文件
2.1 要求
使用Configuration注解可以将一个类作为配置类,替代XML配置文件,配置类需要满足以下要求:
不能是被final修饰的类。
不能是匿名类。
2.2 创建配置类
使用Configuration注解修饰配置类:
1 @Configuration 2 public class DemoConfiguration { 3 public DemoConfiguration() { 4 System.out.println("DemoConfiguration DemoConfiguration() ..."); 5 } 6 }
2.3 加载配置类
不再使用之前通过加载配置文件的方式获取容器,而是通过加载配置类的方式获取容器。
在测试类的方法中获取Spring容器:
1 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DemoConfiguration.class);
这一步相当于之前的代码:
1 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
还可以在配置类中引入其他配置类:
1 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); 2 context.register(DemoConfiguration.class); 3 context.refresh();
3 扫描
3.1 说明
使用ComponentScan注解扫描Bean,类似于在配置文件中使用component-scan扫描器。
ComponentScan注解的属性有:
basePackages:设置扫描类的路径。
basePackageClasses:设置扫描类的类名。
includeFilters:设置包含的过滤器。
excludeFilters:设置排除的过滤器。
3.2 标识
在类上使用注解将类作为一个Bean:
1 @Component 2 public class Demo { 3 public Demo() { 4 System.out.println("Demo Demo() ..."); 5 } 6 public void init() { 7 System.out.println("Demo init() ..."); 8 } 9 public void destroy() { 10 System.out.println("Demo destroy() ..."); 11 } 12 }
3.3 使用
在配置类上使用ComponentScan注解并设置扫描的属性:
1 @Configuration 2 @ComponentScan(basePackages="com.demo.bean") 3 public class DemoConfiguration { 4 public DemoConfiguration() { 5 System.out.println("DemoConfiguration DemoConfiguration() ..."); 6 } 7 }
4 配置Web应用程序
如果要在Web应用程序中使用程序式的配置,需要在web.xml中将contextClass这个属性值设为AnnotationConfigWebApplicationContext类,如果不指定,系统将加载默认的WebApplicationContext类来构建容器。
此外,使用WebApplicationContext类时需要使用contextConfigLocation指定配置文件,当使用AnnotationConfigWebApplicationContext类时,需要指定配置类。
添加并修改:
1 <context-param> 2 <param-name>contextClass</param-name> 3 <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> 4 </context-param> 5 <context-param> 6 <param-name>contextConfigLocation</param-name> 7 <param-value>com.demo.config.DemoConfiguration</param-value> 8 </context-param> 9 <listener> 10 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 11 </listener> 12 <servlet> 13 <servlet-name>springDispatcherServlet</servlet-name> 14 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 15 <init-param> 16 <param-name>contextConfigLocation</param-name> 17 <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> 18 </init-param> 19 <load-on-startup>1</load-on-startup> 20 </servlet> 21 <servlet-mapping> 22 <servlet-name>springDispatcherServlet</servlet-name> 23 <url-pattern>/</url-pattern> 24 </servlet-mapping>
5 引入其他配置
5.1 引入XML配置文件
使用ImportResource注解引入其他XML配置:
1 @Configuration 2 @ImportResource("classpath:spring.xml") 3 public class DemoConfiguration { 4 5 public DemoConfiguration() { 6 System.out.println("DemoConfiguration DemoConfiguration() ..."); 7 } 8 }
5.2 引入配置类
使用注解引入其他配置类:
1 @Configuration 2 @Import(TestConfiguration.class) 3 public class DemoConfiguration { 4 5 public DemoConfiguration() { 6 System.out.println("DemoConfiguration DemoConfiguration() ..."); 7 } 8 }
6 EnableXxx注解
6.1 EnableAsync注解
在类上使用EnableAsync注解可以开启程序中的异步多线程功能,相当于XML配置文件中task名称空间的annotation-driven标签。
6.2 EnableScheduling注解
在类上使用EnableScheduling注解可以开启程序中的任务调度功能,相当于XML配置文件中task名称空间的annotation-driven标签。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!