Spring MVC 3.1使用Profiles进行特定于环境的Hibernate设置

时间:2012-04-04 20:33:30

标签: hibernate spring-mvc

我正在尝试使用bean @Profiles声明来加载特定的hibernate设置,具体取决于我部署到的app服务器。

我在web.xml中注册了一个上下文配置文件初始化程序,并且还实现了ApplicationContextInitializer接口来执行我的app服务器逻辑。

问题在于,当我部署项目时,我收到以下错误:

  

找不到类型为[org.hibernate.SessionFactory]的匹配bean   依赖:预计至少有1个bean有资格成为autowire   这种依赖的候选人。依赖注释:   {@ org.springframework.beans.factory.annotation.Autowired(所需=真)}

对我来说,在Spring自动连接剩余的bean之前,它看起来没有执行我的Profile检查(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();
    }
}

0 个答案:

没有答案