@Configuration注解、@Bean注解以及配置自动扫描、bean作用域

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tuoni123/article/details/79977459
/*
@Configuration标注在类上,相当于把该类作为spring.xml配置文件中的<beans>,作用为:配置spring容器(应用上下文)
@Bean 可理解为用spring.xml里面的<bean>标签
注:
(1)、@Bean注解在返回实例的方法上,如果未通过@Bean指定bean的名称,则默认与标注的方法名相同;
(2)、@Bean注解默认作用域为单例singleton作用域,可通过@Scope(“prototype”)设置为原型作用域;
(3)、既然@Bean的作用是注册bean对象,那么完全可以使用@Component、@Controller、@Service、@Ripository等注解注册bean,当然需要配置@ComponentScan注解进行自动扫描。
(4)、@Bean注解注册bean,同时可以指定初始化和销毁方法  @Bean(name="testNean",initMethod="start",destroyMethod="cleanUp")
ApplicationContext  体系架构:https://blog.csdn.net/h12kjgj/article/details/53725507
AnnotationConfigApplicationContext(用AnnotationConfigApplicationContext替换ClassPathXmlApplicationContext) 详解:https://blog.csdn.net/tuoni123/article/details/79976105
*/
@Configuration
public class SpringConfig {

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

    @Bean
    public WpTerms wpTerms(){//自定义
        return new WpTerms();
    }

    //
    @Bean(name = "wpTermmeta")
    public WpTermmeta wpTermmeta() {//自定义
        return new WpTermmeta("allen","28");
    }

    public static void main(String[] args) {
        ApplicationContext annotationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
        ApplicationContext annotationContext1 = new AnnotationConfigApplicationContext("com.wordpress.master.bean");
        //以前加载spring-context.xml文件的写法
        //ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
        WpTermmeta c = annotationContext.getBean("wpTermmeta", WpTermmeta.class);
        WpTermmeta c1 = annotationContext1.getBean("wpTermmeta", WpTermmeta.class);
        Assert.assertEquals("allen",c.getMetaKey()); //测试
        Assert.assertEquals("allen",c1.getMetaKey()); //测试
    }
}
package com.wordpress.master.bean;


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

//添加注册bean的注解
@Component
public class TestBean {

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

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
        //获取bean
        TestBean tb = (TestBean) context.getBean("testBean");
        tb.sayHello();
    }

}
package com.wordpress.master.bean;

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

@Configuration

//添加自动扫描注解,basePackages为TestBean包路径
@ComponentScan(basePackages = "com.wordpress.master.bean")
public class TestConfiguration {
    public TestConfiguration(){
        System.out.println("spring容器启动初始化。。。");
    }

    //取消@Bean注解注册bean的方式
    //@Bean
    //@Scope("prototype")
    //public TestBean testBean() {
    //  return new TestBean();
    //}
}




阅读更多

没有更多推荐了,返回首页