Spring Boot注解使用指南

时间:2020-06-15 20:44:23   收藏:0   阅读:51

@Configuration

@Configuration包含了@Component,所以被其注解的类自身也会被纳入到bean容器中,但是纳入的是经过cglib增强的子类(代理类)。

@Configuration标记的类必须符合下面的要求:

@Configuration
public class Test {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Map<String, Test> map = ac.getBeansOfType(Test.class);
        System.out.println();
        System.out.println(map);
        System.out.println(map.get("test").getStr() == ac.getBean("getStr")); // true, Test类型在容器中的实例其实是被cglib增强的代理子类,所以行为上和原类是不一样的。
    }
    @Bean
    public String getStr(){
        return "hello";
    }
}

@Primary

用于修改优先权的注解。当发现多个同样类型的Bean时,请优先使用其进行注入,常常和@Component结合使用。

@Qualifier

和@Autowired结合使用,@Autowired是通过类型进行自动装配,这个注解则是通过名字进行装配。

@Lazy

和@Component一起使用,该bean会被惰性初始化。如果和@Configuration一起使用,那么该类所有被@Bean修饰的方法产生的bean都会被惰性初始化。

@Value

注入bean的属性值,支持Spring EL(#{} or ${})。

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!