Java 当应用程序启动时,在spring中初始化一次方法的最佳方法是什么?

Java 当应用程序启动时,在spring中初始化一次方法的最佳方法是什么?,java,spring,spring-mvc,controller,Java,Spring,Spring Mvc,Controller,我有一个方法,它包含一些业务逻辑,比如,使用一些特定于域的服务器创建协议等等。我需要在应用程序启动时运行该方法一次 我如何达到以下要求 这是我已经做过的。我在applicationContext.xml中为业务类和控制器创建了bean,并将业务类bean注入控制器中 我的applicationContext.xml将是 <bean id="configInfo" class="com.pointel.Agent_Chat.ServerInfo.ConfigServerInfo">

我有一个方法,它包含一些业务逻辑,比如,使用一些特定于域的服务器创建协议等等。我需要在应用程序启动时运行该方法一次

我如何达到以下要求

这是我已经做过的。我在applicationContext.xml中为业务类和控制器创建了bean,并将业务类bean注入控制器中

我的
applicationContext.xml
将是

<bean id="configInfo" class="com.pointel.Agent_Chat.ServerInfo.ConfigServerInfo">
    <property name="configurationServer_name" value="${CS_NAME}"></property>
    <property name="configurationServer_host" value="${CS_IPADDRESS}"></property>
</bean>

<bean id="config_con" class="com.pointel.Agent_Chat.ServerConnection.ConfigServerConnection" >
    <property name="info" ref="configInfo"></property>
</bean>

<bean id="init_Server" class="com.pointel.Agent_Chat.Initialiser.Initialize_Servers">
    <property name="connection" ref="config_con"></property>
</bean>

<bean id="initCon" class="com.pointel.Agent_Chat.controllers.InitServerController">
    <property name="init_Server" ref="init_Server"></property>
</bean>
@Controller
public class InitServerController {

    public static int count = 0;
    private Initialize_Servers init_Server;

    public InitServerController(){
        count++;
        System.out.println("Init Controller "+count+" time");
    }

    public void setInit_Server(Initialize_Servers init_Server) {
        this.init_Server = init_Server;
        initializeServers();
    }



    private void initializeServers() {
        ServerStatus.config_status = init_Server.initialize_Config_Server();
        System.out.println("config_status : "+ServerStatus.config_status);
    }

}
我的
商务舱将是

public class Initialize_Servers {

    private ConfigServerConnection connection;
    private InteractionServerConnection interConnection;
    private UCSConnection ucsConnection;

    public Initialize_Servers(){
        System.out.println("Initialize_Servers class instance was created !");
    }

    public ConfigServerConnection getConnection() {
        return connection;
    }

    public void setConnection(ConfigServerConnection connection) {
        this.connection = connection;
    }

    public boolean initialize_Config_Server(){
        try{
            ConfServerProtocol protocol = connection.getConnection();
            if(protocol != null){
                System.out.println("Config server Protocol is not null");
                return true;

            }
        }catch(Exception e){

            return false;
        }
    }

}
这条路对吗?还是其他完美的方法


非常感谢您的回答。

您可以使用bean xml元素上的init method属性将xml文件中的bean设置为具有init方法,如下所示:

<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-2.5.xsd">

    <bean id="myBean" class="com.myjunk.MyBean" 
        init-method="myInit" >
    </bean>
</beans>

这将在初始化MyBean后调用MyBean上的myInit()方法。这是在属性连接在一起之后发生的。如果你使用注释连接东西,也可以使用注释,但我不知道它们是怎么回事。该方法不能采用任何参数


另一个选项是实现,它提供了在连接过程中获取应用程序上下文的挂钩。同样,在对象上设置属性后会发生这种情况。

是否选中了
ApplicationContextAware