实践理解 @Configuration 的使用 和作用

本文详细解析了Spring框架中@Configuration注解的使用方法,包括如何替代XML配置文件,@Bean注解的应用,以及如何管理bean的生命周期。同时,介绍了如何在Web应用程序中使用AnnotationConfigApplicationContext配置Spring容器。

从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。

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

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

目录

 一、用@Configuration加载Spring

1.1、@Configuration配置spring并启动spring容器

1.2、@Configuration启动容器+@Bean注册Bean,@Bean下管理bean的生命周期

1.3、@Configuration启动容器+@Component注册Bean

1.4、使用 AnnotationConfigApplicationContext 注册 AppContext 类的两种方法

1.5、配置Web应用程序(web.xml中配置AnnotationConfigApplicationContext) 

1.6、@Configuation总结

二、组合多个配置类 

2.1、在@configuration中引入spring的xml配置文件

 2.2、在@configuration中引入其它注解配置

2.3、@configuration嵌套(嵌套的Configuration必须是静态类)


1.1、@Configuration配置spring并启动spring容器

@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文)

package com.example.test.configuration;

import org.springframework.context.annotation.Configuration;

@Configuration
public class TestConfiguration {

    public TestConfiguration(){
        System.out.println("TestConfiguration 容器启动初始化了。。。");
    }
}

测试:

package com.example.test.configuration;

import org.springframework.context.annotation.Configuration;

@Configuration
public class TestConfiguration {

    public TestConfiguration(){
        System.out.println("TestConfiguration 容器启动初始化了。。。");
    }
}

从运行主方法结果可以看出,spring容器已经启动了:

1.2、@Configuration启动容器+@Bean注册Bean,@Bean下管理bean的生命周期

@Bean标注在方法上(返回某个实例的方法),等价于spring的xml配置文件中的<bean>,作用为:注册bean对象

bean类:

package com.example.test.configuration;

public class TestBean {

    private String userName;
    private String url;
    private String password;

    public void sayHello(){
        System.out.println("TestBean sayHello...");
    }

    public void start(){
        System.out.println("TestBean 初始化。。。");
    }

    public void cleanUp(){
        System.out.println("TestBean 销毁。。。");
    }
}

配置类:

package com.example.test.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class TestConfiguration {

    public TestConfiguration(){
        System.out.println("TestConfiguration 容器启动初始化了。。。");
    }
    //@Bean注解注册bean,同时可以指定初始化和销毁方法
    @Bean(name = "testBean",initMethod = "start",destroyMethod = "cleanUp")
    @Scope("prototype")
    public TestBean testBean(){
        return new TestBean();
    }
}

 测试:

package com.example.test.configuration;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestRun {

    public static void main(String[] args) throws InterruptedException {
        //如果加载 spring-context.xml文件:
        //ApplicationContext context=new ClassPathXmlApplicationContext("spring-context.xml");

        //@Configuration 注解的spring容器加载方式,用AnnotationConfigApplicationContext替换ClassPathApplicationContext
        ApplicationContext context=new AnnotationConfigApplicationContext(TestConfiguration.class);

        //获取Bean
        TestBean tb= (TestBean) context.getBean("testBean");
        tb.sayHello();
        System.out.println(tb);

        TestBean tb2= (TestBean) context.getBean("testBean");
        tb2.sayHello();
        System.out.println(tb2);
    }
}

 结果:

 注:
(1)、@Bean注解在返回实例的方法上,如果未通过@Bean指定bean的名称,则默认与标注的方法名相同;
(2)、@Bean注解默认作用域为单例singleton作用域,可通过@Scope(“prototype”)设置为原型作用域;
(3)、既然@Bean的作用是注册bean对象,那么完全可以使用@Component、@Controller、@Service、@Ripository等注解注册bean,当然需要配置@ComponentScan注解进行自动扫描。

@Bean下管理bean的生命周期
可以使用基于 Java 的配置来管理 bean 的生命周期。@Bean 支持两种属性,即 initMethod 和destroyMethod,这些属性可用于定义生命周期方法。在实例化 bean 或即将销毁它时,容器便可调用生命周期方法。生命周期方法也称为回调方法,因为它将由容器调用。使用 @Bean 注释注册的 bean 也支持 JSR-250 规定的标准 @PostConstruct 和 @PreDestroy 注释。

1.3、@Configuration启动容器+@Component注册Bean

Bean类:

package com.example.test.configuration;

import org.springframework.stereotype.Component;
//增加注册bean的注解
@Component
public class TestBean {

    private String userName;
    private String url;
    private String password;

    public void sayHello(){
        System.out.println("TestBean sayHello...");
    }

    public void start(){
        System.out.println("TestBean 初始化。。。");
    }

    public void cleanUp(){
        System.out.println("TestBean 销毁。。。");
    }
}

配置类:

package com.example.test.configuration;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.example.test.configuration")
public class TestConfiguration {

    public TestConfiguration(){
        System.out.println("TestConfiguration 容器启动初始化了。。。");
    }
}

 测试:

package com.example.test.configuration;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.example.test.configuration")
public class TestConfiguration {

    public TestConfiguration(){
        System.out.println("TestConfiguration 容器启动初始化了。。。");
    }
}

sayHello()方法都被正常调用:

1.4、使用 AnnotationConfigApplicationContext 注册 AppContext 类的两种方法

1.4.1、 配置类的注册方式是将其传递给 AnnotationConfigApplicationContext 构造函数

package com.example.test.configuration;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestRun {

    public static void main(String[] args) throws InterruptedException {

        //@Configuration 注解的spring容器加载方式,用AnnotationConfigApplicationContext替换ClassPathApplicationContext
        ApplicationContext context=new AnnotationConfigApplicationContext(TestConfiguration.class);

        TestBean tb = (TestBean) context.getBean("testBean");
        tb.sayHello();
    }
}

1.4.2、 AnnotationConfigApplicationContext 的register 方法传入配置类来注册配置类

package com.example.test.configuration;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestRun {

    public static void main(String[] args){

        AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext();
        ctx.register(TestConfiguration.class);
        ctx.refresh();

        TestBean tb = (TestBean) ctx.getBean("testBean");
        tb.sayHello();
    }
}

1.5、配置Web应用程序(web.xml中配置AnnotationConfigApplicationContext) 

过去,您通常要利用 XmlWebApplicationContext 上下文来配置 Spring Web 应用程序,即在 Web 部署描述符文件 web.xml 中指定外部 XML 上下文文件的路径。XMLWebApplicationContext 是 Web 应用程序使用的默认上下文类。以下代码描述了 web.xml 中指向将由 ContextLoaderListener 监听器类载入的外部 XML 上下文文件的元素。

<web-app>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <servlet>
    <servlet-name>sampleServlet</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    </servlet>
 
...
</web-app>

现在,您要将 web.xml 中的上述代码更改为使用 AnnotationConfigApplicationContext 类。切记,XmlWebApplicationContext 是 Spring 为 Web 应用程序使用的默认上下文实现,因此您永远不必在您的web.xml 文件中显式指定这个上下文类。现在,您将使用基于 Java 的配置,因此在配置 Web 应用程序时,需要在web.xml 文件中指定 AnnotationConfigApplicationContext 类。上述代码将修改如下:

<web-app>
    <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>
            demo.AppContext
        </param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <servlet>
    <servlet-name>sampleServlet</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.
            support.AnnotationConfigWebApplicationContext
        </param-value>
    </init-param>
    </servlet>
 
...
</web-app>

1.6、@Configuation总结

@Configuation等价于<Beans></Beans>

@Bean等价于<Bean></Bean>

@ComponentScan等价于<context:component-scan base-package="com.dxz.demo"/>

 


二、组合多个配置类 

2.1、在@configuration中引入spring的xml配置文件

配置类:

package com.example.test.configuration;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;

/**
 * 引入spring的xml配置文件
 */
@Configuration
@ImportResource("classpath:applicationContext-configuration.xml")
public class WebConfig {
}

Bean:

package com.example.test.configuration;

public class TestBean2 {

    private String userName;
    private String url;
    private String password;

    public void sayHello(){
        System.out.println("TestBean2 sayHello...");
    }

    public void start(){
        System.out.println("TestBean2 初始化。。。");
    }

    public void cleanUp(){
        System.out.println("TestBean2 销毁。。。");
    }
}

classpath:applicationContext-configuration.xml :

<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <bean id="testBean2" class="com.example.test.configuration.TestBean2">

    </bean>
</beans>

测试:

package com.example.test.configuration;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestRun {

    public static void main(String[] args){

        AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext();
        ctx.register(WebConfig.class);
        ctx.refresh();

        TestBean2 tb = (TestBean2) ctx.getBean("testBean2");
        tb.sayHello();
    }
}

 结果:

 2.2、在@configuration中引入其它注解配置

配置类:

package com.example.test.configuration;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;

/**
 * 引入spring的xml配置文件
 */
@Configuration
@ImportResource("classpath:applicationContext-configuration.xml")//配置文件
@Import(TestConfiguration.class)//配置类
public class WebConfig {
}

测试:

package com.example.test.configuration;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestRun {

    public static void main(String[] args){
        ApplicationContext ctx=new AnnotationConfigApplicationContext(WebConfig.class);

        TestBean tb = (TestBean) ctx.getBean("testBean");
        tb.sayHello();

        TestBean2 tb2 = (TestBean2) ctx.getBean("testBean2");
        tb2.sayHello();
    }
}

结果:

2.3、@configuration嵌套(嵌套的Configuration必须是静态类)

通过配置类嵌套的配置类,达到组合多个配置类的目的。但注意内部类必须是静态类。

配置类:

package com.example.test.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.example.test.configuration")  //TestBean 增加了@Component注解
public class TestConfiguration {

    public TestConfiguration(){
        System.out.println("TestConfiguration 容器启动初始化了。。。");
    }

    static class TestConfiguration2{
        @Bean
        TestBean2 testBean2(){
            return new TestBean2();
        }
    }
}

测试类:

package com.example.test.configuration;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestRun {

    public static void main(String[] args){
        ApplicationContext ctx=new AnnotationConfigApplicationContext(TestConfiguration.class);

        TestBean tb = (TestBean) ctx.getBean("testBean");
        tb.sayHello();

        TestBean2 tb2 = (TestBean2) ctx.getBean("testBean2");
        tb2.sayHello();
    }
}

结果:

参考文章:https://www.breakyizhan.com/java/14623.html#11 

<think>首先,用户的问题是:“我想理解在Spring Boot中@Configuration注解的作用 请问Spring Boot中@Configuration注解的作用是什么”。这直接询问了@Configuration注解在Spring Boot中的作用。 根据系统级指令: - 所有行内数学表达式必须使用$...$格式,但这里没有数学表达式,所以不需要。 - 独立公式必须使用$$...$$格式并单独成段,但同样,没有数学内容。 - LaTeX语法正确:不适用。 - 使用中文回答:必须用中文。 - 生成相关问题:在回答末尾,我需要生成相关问题。 - 回答中引用的段落末尾自然地添加引用标识:例如[^1]。参考引用提供了: - 引用[1]: @Configuration注解在Spring Boot中的作用及原因。它标识一个类是配置类,用于定义应用程序的配置Bean的创建。Spring Boot启动时会扫描这些类。 - 引用[2]: @Configuration注解的作用:在调用方法创建实例时,会去Spring的单例bean工厂获取单例bean的实例。 用户可见指令: - 回答结构清晰,帮助用户逐步解决问题。 - 保证回答真实可靠。 我的回答应该基于这些引用,但也要补充一般知识,因为我是AI,应该提供准确信息。@Configuration是Spring框架的一部分,用于定义Bean配置。 关键点: - @Configuration注解表示一个类是配置类。 - 在配置类中,使用@Bean注解的方法返回的对象会被Spring容器管理为Bean。 - 在Spring Boot中,它用于自动配置组件扫描。 - 当Spring Boot启动时,它会扫描所有带有@Configuration注解的类,并加载它们定义的Bean。 - 与不使用@Configuration相比,使用它时,Spring会处理Bean的依赖单例管理,确保方法调用时从容器中获取Bean,而不是每次都创建新实例(如引用[2]所述)。 结构化回答: 1. **介绍作用**:解释@Configuration注解的基本作用。 2. **详细说明**:描述它在Spring Boot中的具体应用。 3. **原因**:为什么使用它(如自动配置)。 4. **示例**:提供一个简单的代码示例来演示。 5. **总结**:简要总结。 6. **生成相关问题**:在末尾添加相关问题。 生成相关问题: - 基于主题,如@Configuration与其他注解的区别、最佳实践等。 - 例如: 1. @Configuration@Component有什么区别? 2. 如何在Spring Boot中自定义配置类? 3. @Configuration注解的性能影响是什么? 确保引用自然添加:在回答中,如果提到引用内容,在末尾添加[^1]或[^2]。 最终回答格式: - 用中文。 - 清晰结构。 - 末尾有“相关问题”部分。</think>### Spring Boot中@Configuration注解的作用详解 在Spring Boot中,`@Configuration`注解是一个核心注解,用于标识一个类是配置类。它的主要作用是定义应用程序的配置信息Bean的创建逻辑,帮助Spring容器管理Bean的生命周期依赖关系。下面我将逐步解释其作用、原因实际应用,确保回答清晰可靠。 #### 1. **基本作用:定义配置类Bean** - `@Configuration`注解标记的类被称为配置类。在这个类中,你可以使用`@Bean`注解的方法来定义Spring容器管理的Bean。 - 当Spring Boot应用程序启动时,它会自动扫描所有带有`@Configuration`注解的类(通常通过`@SpringBootApplication`注解触发),并将这些类加载到Spring IoC(Inversion of Control)容器中。容器会执行配置类中的方法,创建并管理Bean实例。 - 例如,在配置类中定义一个Bean的方法: ```java @Configuration public class AppConfig { @Bean public MyService myService() { return new MyServiceImpl(); // 返回的实例会被Spring容器管理为Bean } } ``` 这里,`myService()`方法返回的对象会被Spring容器注册为一个Bean,可以在应用程序中注入使用。 #### 2. **关键特性:处理Bean依赖单例管理** - **依赖注入**:在配置类中,如果一个Bean方法依赖另一个Bean,Spring容器会自动处理依赖关系。例如,调用`myService()`时,如果它需要另一个Bean(如`MyRepository`),容器会从单例工厂获取该Bean,而不是每次创建新实例。这确保了Bean的复用性一致性[^2]。 - **单例模式**:默认情况下,`@Bean`方法创建的Bean是单例的(即整个应用程序中只有一个实例)。Spring容器会缓存这些Bean,避免重复创建,提高性能。 - 与普通类相比,不使用`@Configuration`时,方法调用可能直接创建新对象,而不会由Spring管理依赖。例如: - 使用`@Configuration`:调用`myService()`时,Spring会注入所需的依赖(如从容器获取`MyRepository`)。 - 不使用`@Configuration`:如果只是一个普通类,方法调用可能每次都创建新实例,导致依赖管理混乱。 #### 3. **在Spring Boot中的具体应用** - **自动配置的基础**:Spring Boot的自动配置机制(如通过`spring-boot-autoconfigure`模块)大量使用`@Configuration`类。例如,内置的`DataSourceAutoConfiguration`类定义了数据库连接的Bean,当应用程序检测到相关依赖时自动生效。 - **组件扫描的补充**:`@Configuration`类通常与`@ComponentScan`结合使用。`@SpringBootApplication`注解隐式包含`@ComponentScan`,它会扫描指定包下的所有组件配置类。 - **自定义配置**:开发者可以创建自己的配置类来覆盖默认Bean或添加特定逻辑。例如,定义自定义的数据源或安全设置。 - 原因:使用`@Configuration`能提升代码的模块化可维护性。它允许将配置集中管理,而不是分散在XML文件中,符合Spring Boot的“约定优于配置”原则[^1]。 #### 4. **简单示例** 以下是一个完整的Spring Boot示例,展示`@Configuration`的实际使用: ```java // 主应用类 @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } // 配置类 @Configuration public class AppConfig { @Bean public String greetingMessage() { return "Hello, Spring Boot!"; } } // 在其他类中注入Bean @RestController public class GreetingController { @Autowired private String greetingMessage; // 注入配置类中定义的Bean @GetMapping("/greet") public String greet() { return greetingMessage; } } ``` 在这个例子中: - `AppConfig`类被标记为`@Configuration`,其中`greetingMessage()`方法定义了一个字符串Bean。 - Spring Boot启动时扫描并加载`AppConfig`,创建`greetingMessage` Bean。 - `GreetingController`通过`@Autowired`注入该Bean,并用于API响应。 #### 5. **总结** `@Configuration`注解在Spring Boot中的作用是:**标识配置类,用于集中定义Bean应用程序配置,确保Spring容器能自动管理Bean的创建、依赖注入单例生命周期**。它简化了配置过程,提高了代码的可测试性灵活性,是Spring Boot自动配置的核心机制之一[^1][^2]。 如果您有更多具体场景或疑问,欢迎进一步讨论! ### 相关问题 1. `@Configuration`注解`@Component`注解有什么区别? 2. 如何在Spring Boot中自定义配置类来覆盖默认Bean? 3. `@Configuration`注解在大型项目中的最佳实践是什么?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值