@Configuration 使用

@Configuration注解用于定义配置类,替代XML配置,通过@Bean方法注册Bean,适用于构建Spring容器。配置类不可为final或匿名,嵌套配置需为静态类。支持启动容器、注册Bean,可结合@Component或@EnableXXX注解使用。

@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被

AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。

注意:@Configuration注解的配置类有如下要求:

  • @Configuration不可以是final类型;
  • @Configuration不可以是匿名类;
  • 嵌套的configuration必须是静态类。

常用:

  • 用@Configuration加载spring

    • @Configuration配置spring并启动spring容器
    • @Configuration启动容器+@Bean注册Bean
    • @Configuration启动容器+@Component注册Bean
    • 使用 AnnotationConfigApplicationContext 注册 AppContext 类的两种方法
    • 配置Web应用程序(web.xml中配置AnnotationConfigApplicationContext)
  • 组合多个配置类

    • 在@configuration中引入spring的xml配置文件
    • 在@configuration中引入其它注解配置
    • @configuration嵌套(嵌套的Configuration必须是静态类)
    • @EnableXXX注解
    • @Profile逻辑组配置
    • 使用外部变量

简单举例:

@Configuration
public class MQConfig {
    public static final String MIAOSHA_QUEUE = "miaosha.queue";
    public static final String QUEUE = "queue";

    @Bean
    public Queue queue(){
        return new Queue(QUEUE,true);
    }
`@Configuration` 是 Spring 框架中的核心注解,用于标识一个类作为配置类,该类中可定义 Bean。以下是其使用流程: ### 1. 添加依赖 若使用 Maven 项目,需在 `pom.xml` 中添加 Spring 相关依赖: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.7.5</version> </dependency> </dependencies> ``` ### 2. 创建配置类 创建一个类并使用 `@Configuration` 注解标注,在该类中使用 `@Bean` 注解定义 Bean: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean public MyService myService() { return new MyService(); } } ``` 上述代码中,`AppConfig` 类被 `@Configuration` 注解标注,成为配置类,`myService` 方法被 `@Bean` 注解标注,Spring 会将 `MyService` 类的实例作为 Bean 注册到 Spring 容器中。 ### 3. 定义 Bean 类 定义 `MyService` 类: ```java public class MyService { public void doSomething() { System.out.println("Doing something..."); } } ``` ### 4. 加载配置类 在 Spring 应用中加载配置类: ```java import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { // 创建 AnnotationConfigApplicationContext 并加载配置类 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); // 从容器中获取 Bean MyService myService = context.getBean(MyService.class); // 调用 Bean 的方法 myService.doSomething(); // 关闭容器 context.close(); } } ``` 上述代码中,使用 `AnnotationConfigApplicationContext` 加载配置类 `AppConfig`,并从容器中获取 `MyService` Bean 实例,调用其方法,最后关闭容器。 ### 5. 依赖注入 在其他类中可以通过依赖注入使用这些 Bean: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class MyClient { private final MyService myService; @Autowired public MyClient(MyService myService) { this.myService = myService; } public void performAction() { myService.doSomething(); } } ``` 上述代码中,`MyClient` 类通过构造函数注入 `MyService` Bean 实例,并在 `performAction` 方法中调用其方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值