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

 

 

 

posted @   emanlee  阅读(256)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· 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
点击右上角即可分享
微信分享提示