我正在尝试使用 bean @Profiles 声明来加载特定的休眠设置,具体取决于我部署到的应用服务器。
我在 web.xml 中注册了一个上下文配置文件初始化程序,还实现了 ApplicationContextInitializer 接口来执行我的应用程序服务器逻辑。
问题是,当我部署我的项目时,我收到以下错误:
没有为依赖项找到类型为 [org.hibernate.SessionFactory] 的匹配 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
对我来说,这看起来好像在 Spring 自动装配我的其余 bean 之前它没有执行我的配置文件检查(ApplicationContextInitilizer 代码)。我的假设正确吗?我该如何纠正?
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>SpringTest</display-name>
<context-param>
<param-name>contextInitializerClasses</param-name>
<param-value>myPackage.Service.ContextProfileInitializer</param-value>
</context-param>
<!-- Dispatcher Servlet to handle HTTP requests -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<!-- Welcome File List -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
调度程序-servlet.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="myPackage" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Transaction management -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<!-- DataSources -->
<beans profile="jboss">
<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:jboss/datasources/dbName" />
</bean>
<!-- Hibernate -->
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="configLocation">
<value>classpath:JBOSS-hibernate-db2.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
</bean>
</beans>
<beans profile="websphere">
<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:websphere/datasources/dbName" />
</bean>
<!-- Hibernate -->
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="configLocation">
<value>classpath:WEBSPHERE-hibernate-db2.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
</bean>
</beans>
</beans>
ContextProfileInitializer.java
public class ContextProfileInitializer implements ApplicationContextInitializer<ConfigurableWebApplicationContext> {
private org.apache.log4j.Logger logger = Logger.getLogger(getClass());
@Override
public void initialize(ConfigurableWebApplicationContext ctx){
if(Rtools.pos("jboss.server", System.getProperties().toString()) > 0){
ctx.getEnvironment().setActiveProfiles("jboss");
logger.info("Profile set to JBOSS");
} else {
ctx.getEnvironment().setActiveProfiles("websphere");
logger.info("Profile set to WEBSPHERE");
}
ctx.refresh();
}
}