spring @Configuration

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

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

@Configuration
@ComponentScan(basePackages = "com.apd.demo")
public class MyConfiguration {

    public MyConfiguration(){
        System.out.println("MyConfiguration初始化。。。");
    }

    /*
    @Bean(name="exampleBean",initMethod = "init",destroyMethod = "destroy")
    @Scope("prototype")
    public ExampleBean exampleBean(){
        return new ExampleBean();
    } */
}

二、初始化spring上下文

@Test
public void testInitContext(){

    //初始化方法一,applicationContext.xml在resource路径下
    //ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

    //初始化方法二
    //String conf = System.getProperty("user.dir")+File.separator+"conf"+File.separator+"applicationContext.xml";              
    //ApplicationContext ac = new FileSystemXmlApplicationContext(conf);

    //初始化方法三,利用@Configuration
    ApplicationContext ac = new AnnotationConfigApplicationContext(MyConfiguration.class)
    
}

三、

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值