Spring(八)Bean装配 基于java容器

2018-03-22  本文已影响0人  lqsss
image.png

解释

通过@Bean注解将创建的对象注入到ioc容器中

例子

    @Bean(name = "stringStore", initMethod="init", destroyMethod="destroy")
    public Store stringStore() {
        return new StringStore();
    }
public class StringStore implements Store<String> {
    
    public void init() {
        System.out.println("This is init.");
    }
    
    public void destroy() {
        System.out.println("This is destroy.");
    }
    
}

测试

    @Test
    public void test() {
        Store store = super.getBean("stringStore");
        System.out.println(store.getClass().getName());
    }
上一篇 下一篇

猜你喜欢

热点阅读