7.12.1 Basic concepts @Bean and @Configuration

Java配置支持@Configuration标记类@Bean标记方法

@Bean注解用来声明这个方法实例化,配置并初始化一个新的对象被Ioc容器包含。这些和XML配置的<beans>元素很相似,@Bean与<bean/>办演的角色相同。

你可以在任何被Spring @Component的类下用@Bean标记一个方法。然而,@Bean最常用在@Configuration bean下

用@Configuration注解一个类声明他的主要目的是bean定义的源。标记@Configuration的类允许内置的bean依赖仅调用@Bean方法

@Configuration
public class Appconfig{
    @Bean
    public MyService myService(){
        return new MyServiceImpl();
    }
}

上面的配置等价于<beans>XML配置

<beans>
    <bean id="myService" class="com.acme.services.MyServiceImple"/>
</beans>

Full @Configuration vs 'lite' @Beans mode?

@Configuration 下使用@Bean在调用方法时返回的总是单例bean 在@Component和普通的po类中定定义的@bean方法 ,调用此方法返回的不是单例的bean

results matching ""

    No results matching ""