Spring:调用其他bean时会触发bean的init方法

时间:2016-10-21 09:22:04

标签: spring

我目前正在尝试使用bean回调方法,并注意到如果我为一个bean定义回调方法,这些方法也会在其他bean上调用。

我有一个名为x.HelloWorld(没有回调)和y.HelloWorld的类(具有init和destroy回调方法)。

HelloWorld.java:

public class HelloWorld {
    private String message;

    public void setMessage(String message) {
        this.message = message;
    }

    public void getMessage() {
        System.out.println("Your Message : " + message);
    }
}

HelloWorld2.java:

public class HelloWorld {
    private String message;

    public void setMessage(String message) {
        this.message = message;
    }

    public void getMessage() {
        System.out.println("Your Message : " + message);
    }

    public void init() {
        System.out.println("Bean is going through init.");
    }

    public void destroy() {
        System.out.println("Bean will destroy now.");
    }
}

x.MainApp.java:

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {

        AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        obj.getMessage();
    }
}

y.MainApp.java:

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {

        AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

        HelloWorld obj = (HelloWorld) context.getBean("helloWorld2");
        obj.getMessage();
        context.registerShutdownHook();
    }
} 

beans.xml中:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <bean id="helloWorld" class="x.HelloWorld">
        <property name="message" value="Hello World (1)" />
    </bean>
    <bean id="helloWorld2" class="y.HelloWorld"
        init-method="init" destroy-method="destroy">
        <property name="message" value="Hello World (2)" />
    </bean> 
</beans>

结果: 正在运行y.MainApp(按预期工作):

Bean is going through init. 
Your Message : Hello World (2)
Bean will destroy now.

正在运行x.MainAppx.HelloWorld的初始回调由y.HelloWorld触发。对此感到困惑..非常感谢任何帮助......)

Bean is going through init.
Your Message : Hello World (1)

1 个答案:

答案 0 :(得分:0)

请修改你的init方法:

<input type='text' id='firstname' data-pattern="/^[a-zA-Z]+$/"  data-message="For your name please use alphabets only" required="required" /><br />

您将看到所有bean在启动时都已初始化,并且您看到的消息实际上来自使用init方法的类。

如果您不想要这种行为,可以使用lazy init:

public void init() {
    System.out.println("Bean with message '"+ message +"' is going through init.");
}