我最初有一个xml文件,其格式如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
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.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="FirstBean">...</bean>
<bean id="SecondBean">...</bean>
<bean id="ThirdBean">...</bean>
</beans>
对我们来说效果很好。但今天我们意识到我们只想在某些环境(生产和测试)中实例化一些bean - 我们需要配置文件才能实现。我们通过嵌套一些元素并添加配置文件来完成此操作,如下所示:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
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.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Beans to run commonly amongst both profiles -->
<beans>
<bean id="FirstBean">...</bean>
</beans>
<!-- Beans to run in prod -->
<beans profile="production">
<bean id="SecondBean">...</bean>
</beans>
<!-- Beans to run in test -->
<beans profile="test">
<bean id="ThirdBean">...</bean>
</beans>
</beans>
然后......为了让bean加载正确的bean - 在应用程序的初始启动时 - 我执行以下操作(目前硬编码用于测试目的):
System.setProperty("spring.profiles.active", "test");
我也尝试过:
System.setProperty("spring.profiles.active", "production");
但现在我继续在所有三个bean上获得“没有这样的bean定义”。
我希望我在这里提供了足够的信息。我真的很困惑 - 已经关注了各种在线资源。
答案 0 :(得分:0)
您似乎需要在应用程序启动时更早地设置活动的Spring配置文件。有几种不同的方法可以实现这一目标。
快速解决方案
出于测试目的,您可以向web.xml添加context-param:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Spring Web Application</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/spring/*</url-pattern>
</servlet-mapping>
<!-- Set active Spring profile -->
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>prod</param-value>
</context-param>
</web-app>
程序化解决方案
您可以通过将实现ApplicationContextInitializer的自定义类传递给DispatcherServlet,在启动时以编程方式设置活动的Spring配置文件。在您的自定义类中添加一个方法,该方法使用为应用程序上下文设置活动Spring配置文件的代码覆盖ApplicationContextInitializer的initialize方法。
在web.xml中,将实现ApplicationContextInitializer的自定义类作为init-param传递给DispatcherServlet:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Spring Web Application</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- Register custom class that implements ApplicationContextInitilizer to set active Spring profile on startup -->
<param-name>contextInitializerClasses</param-name>
<param-value>org.eclipse.che.examples.MyApplicationContextInitializer</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/spring/*</url-pattern>
</servlet-mapping>
</web-app>
然后定义实现ApplicationContextInitializer的自定义类:
package org.eclipse.che.examples;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
public class MyApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
// set active Spring profile
applicationContext.getEnvironment().setActiveProfiles("prod");
}
}