我有一种方法,它包含一些业务逻辑,例如使用一些特定于域的服务器创建协议等。我需要在我的应用程序启动时运行一次该方法。
我怎样才能达到我的以下要求?
这是我已经做过的。我为业务类和 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
意志是,
@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);
}
}
我的Business class
意志是
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;
}
}
}
这是正确的方法吗?或任何其他完美的方式来做到这一点?
好的答案绝对值得赞赏。