美文网首页
Spring(八)Bean装配 基于java容器

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

作者: lqsss | 来源:发表于2018-03-22 16:42 被阅读0次
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());
    }

相关文章

网友评论

      本文标题:Spring(八)Bean装配 基于java容器

      本文链接:https://www.haomeiwen.com/subject/qngaqftx.html