0

I am trying to use the bean @Profiles declarations to load specific hibernate settings depending on the app server I deploy to.

I have registered a context profile initializer in my web.xml and also implemented the ApplicationContextInitializer interface to perform my app server logic.

The problem is that when I deploy my project I'm getting the following error:

No matching bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

To me, this looks like it is not performing my Profile check (ApplicationContextInitilizer code) before Spring is autowiring the rest of my beans. Is my assumption correct? How do I correct this?

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>

Dispatcher-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();
    }
}
skaffman
  • 398,947
  • 96
  • 818
  • 769
schn0573
  • 244
  • 4
  • 10

0 Answers0