Sping中的@Configuration注解

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

使用要求:

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

一、配置类的使用

Bean类:

public class TestBean {

    private String username;
    private String url;
    private String password;

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

    public String toString() {
        return "username:" + this.username + ",url:" + this.url + ",password:" + this.password;
    }

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

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

配置类:

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,类型为返回值,id默认是方法名
    @Bean
    public TestBean testBean() {
        return new TestBean();
    }
}

测试类:

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

public class TestMain {
    public static void main(String[] args) {

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

        // 如果加载spring-context.xml文件:
        // ApplicationContext context = new
        // ClassPathXmlApplicationContext("spring-context.xml");
        
        //获取bean
        TestBean tb = (TestBean) context.getBean("testBean");
        tb.sayHello();
    }
}

结果在控制台输出:TestBean sayHello… 说明TestBean已经加载到spring容器当中了。

二、组合多个配置类

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

@Configuration
@ImportResource("classpath:applicationContext-configuration.xml")
// 引入applicationContext-configuration.xml配置文件
public class TestConfig {

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

@Configuration
@Import(TestConfiguration.class)
// 进入TestConfiguration.class配置类
public class TestConfig {

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

@Configuration
@ComponentScan(basePackages = "com.czx")
public class TestConfiguration {
    public TestConfiguration() {
        System.out.println("TestConfiguration容器启动初始化。。。");
    }
    
    @Configuration
    static class DatabaseConfig {
        @Bean
        DataSource dataSource() {
            return new DataSource();
        }
    }
}

总结:

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

使用 @Configuation注解的同时也经常会使用@ComponentScan注解@Bean注解,如果想了解请自行跳转。ω

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值