Announcement Announcement Module
Collapse
No announcement yet.
spring-jersey integration. Injected objects are null Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring-jersey integration. Injected objects are null

    Hello all,

    I've been trying to get Jersey and Spring to work nicely together for a few days now but despite my efforts I've had no luck; hopefully someone can help me out.

    web.xml
    Code:
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:META-INF/spring-master.xml</param-value>
        </context-param>
        <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>/WEB-INF/log4j.properties</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
        </listener>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <listener>
            <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
        </listener>
    spring-master.xml My 'entities' are annotated: @Service("BookBo"),etc.
    Code:
        <context:property-placeholder location="classpath*:META-INF/database.properties"/>
        <import resource="classpath*:/META-INF/dataSource.xml"/>
        <import resource="classpath*:/META-INF/hibernate.xml"/>
        <context:component-scan base-package="com.bpcm.entities"/>
    dataSource.xml
    Code:
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    	</bean>
    	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="${jdbc.driverClassName}" />
    		<property name="url" value="${jdbc.url}" />
    		<property name="username" value="${jdbc.username}" />
    		<property name="password" value="${jdbc.password}" />
    	</bean>
    hibernate.xml
    Code:
      <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <!-- <prop key="hibernate.show_sql">true</prop>-->
                    <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
                    <prop key="hibernate.hbm2ddl.auto">create</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                </props>
            </property>
            <property name="annotatedClasses">
                <list>
                    <value>com.bpcm.entities.book.domain.Specialist</value>
                    <value>com.bpcm.entities.page.domain.Aptitude</value>
                </list>
            </property>
        </bean>
    From what I understand there are issues with dependencies when using spring-jersey, so I've excluded spring components.
    pom.xml
    Code:
        <properties>
            <jersey-version>1.8</jersey-version>
            <spring-version>3.0.5.RELEASE</spring-version>
         </properties>
        <dependencies> 
            <dependency>
                <groupId>com.sun.jersey</groupId>
                <artifactId>jersey-server</artifactId>
                <version>${jersey-version}</version>
            </dependency>       
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring-version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring-version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring-version}</version>
            </dependency>
            <dependency>
                <groupId>com.sun.jersey.contribs</groupId>
                <artifactId>jersey-spring</artifactId>
                <version>${jersey-version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-core</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-beans</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-context</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-web</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aop</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>${spring-version}</version>
            </dependency>
    If I deploy the project as an application using:
    Code:
    ApplicationContext appContext = new ClassPathXmlApplicationContext("META-INF/spring-master.xml");
    BookBo BookBo = (BookBo) appContext.getBean("BookBo");
    everything is injected properly, but if I deploy it as a webapp I get null Autowired objects. I'm not entirely sure why there is a discrepancy. I assume its working properly when ran as an app because I'm explicitly specifying the bean.

    On a side note the webbapp had previously worked using Spring as the rest service, but my company wanted to switch to Jersey. Besides changing the rest controller and the dependencies the project has remained the same.

    I'm sorry about the long post, I would really appreciate any help/advice,
    bpcm

  • #2
    I figured out the issue. In spring-master I had: <context:component-scan base-package="com.bpcm.entities"/> which does not include the jersey controller class. I changed it to <context:component-scan base-package="com.bpcm"/>

    I believe that since I had used Spring as my restful service previously, it scanned for injection references automatically even without the proper component-scan config.
    Jersey did/could not take care of the references on its own; I neglected to change the scope of the component scan accordingly.

    Comment


    • #3
      Hi bpcm,

      I have a requirement where a system is required to expose its services via restful ws and there needs to exist an authentication around those services. So what I was trying was to have a controller(org.springframework.web.servlet.handler .SimpleUrlHandlerMapping) set-up around the url pattern for my restful services and add an interceptor to it for authentication. I have got everything set up, but when I call my resource via it's url ; the resource gets called but the controller to which it is mapped never gets called. So I am unable to intercept the request.
      A

      Comment

      Working...
      X