2015-10-16 68 views
0

我是Spring框架的新手,我想要做的是在我的spring配置中有一个bean定义,它在任何其他bean中都没有被引用,我也不想用上下文加载它。我想在做bean初始化的时候,弹出它自己加载的叫它init-method如何在春季实例化一个独立的bean?

<bean id="test" class="com.spring.test.Test" init-method="init"/> 

package com.spring.test; 

public class Test { 

    public void init() { 
    System.out.println("Recvd the call Test.print() "); 
    } 

}

我没有得到这个init()打电话,我觉得这个测试的bean应该实现一个接口来告诉弹簧加载这一点。

+0

你用Spring来调用.getBean()方法吗? – ryekayo

+0

这就是我不想做的事情。这个bean应该作为一个例子的回调在春季实例化。 – ANewDeveloper

+0

这可能实际上有助于那种情况:'@ PostConstruct' – ryekayo

回答

0

,你必须使用父“豆”标签default-init-method属性,看我的例子:

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" 
     default-init-method="init" default-destroy-method="destroy"> 

<!--then you can disable init-method using these tag in child bean:--> 

<bean id="test" class="com.spring.test.Test" init-method=""/> 

</beans> 

添加“初始化方法”在bean标签属性是覆盖父方法,像一个继承方法。与销毁方法一样。

祝你好运!