Spring @Configuration @Import
Spring @Configuration
@Configuration 用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被 @Bean 注解的方法,这些方法将会被 AnnotationConfigApplicationContext 或 AnnotationConfigWebApplicationContext 类进行扫描,并用于构建 bean 定义,初始化 Spring 容器。
Spring 3.0开始,@Configuration 用于定义配置类,定义的配置类可以替换xml文件,一般和 @Bean 注解联合使用。
@Configuration 注解主要标注在某个类上,相当于xml配置文件中的<beans>
@Bean注解主要标注在某个方法上,相当于xml配置文件中的<bean>
@Configuration
public class DemoConfiguration {
@Bean
public DemoBean demoBean() {
return new DemoBean();
}
}
相当于
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd" default-lazy-init="false"> <bean id="demoBean" class="com.aircity.demo.entity.demoBean"></bean> </beans>
@Import
@Import用来导入 @Configuration注解的配置类、注入普通类、导入 ImportSelector 的实现类或导入 ImportBeanDefinitionRegistrar 的实现类。
@Import 注解,顾名思义,导入,即把类加入Spring IOC容器。
假设有两个要加入IOC容器的类TestA、TestB
@Data
public class TestA {
private Integer id = 1;
private String name = "TestA";
public void print(){
System.out.println(this.toString());
}
}
@Data
public class TestB {
private Integer id = 2;
private String name = "TestB";
public void print(){
System.out.println(this.toString());
}
}
在任何一个配置类上加@Import({TestA.class, TestB.class})……
@Import({TestA.class, TestB.class}) @Configuration public class ImportConfig {}
然后就行了……
随便写点代码测试一下:
@RestController
@RequestMapping("/import")
public class TestImportController {
@Autowired
private TestA testA;
@Autowired
private TestB testB;
@RequestMapping("/test1")
public void test1(){
testA.print();
testB.print();
}
}
REF
https://blog.csdn.net/weixin_44360895/article/details/112758122
分类:
[14] Java/JSP
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY