Question

I'm trying to get a property in my Jersey/Spring application to get injected into a member variable as follows below. When I call the service at localhost:8080/test/test, the String just comes up as null. I'm not getting any errors from the container when it starts up either. Everything looks like it's being resolved on the classpath as expected.

Am I doing anything wrong below? Is there any more information I could provide to help troubleshoot this?

Tester.java:

package com.foo;

@Service
@Path("/test")
public class Tester implements Serializable {

    private static final long serialVersionUID = 7192266167739106825L;

    @Value("${testInjection}")
    private String throttleTime;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("/test")
    public String test() {
        return "injected value: " + throttleTime;
    }
}

spring-config.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:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<context:annotation-config />
<context:component-scan base-package="com.foo"/>

<!-- Property Loader Configuration -->
<context:property-placeholder location="classpath:Test.properties"/>
<tx:annotation-driven/>

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>Test</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Test</name>
<url>http://maven.apache.org</url>

<properties>
    <spring.version>4.0.2.RELEASE</spring.version>
    <hibernate.version>4.3.4.Final</hibernate.version>
    <jersey.version>2.7</jersey.version>
</properties>

<dependencies>
    <!-- Spring stuff -->
    <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-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- Hibernate stuff -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <!-- Jersey stuff -->
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <!-- if Servlet API older than 3.0, use "jersey-container-servlet-core"  -->
        <artifactId>jersey-container-servlet</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-spring3</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
        <version>2.7</version>
    </dependency>

    <!-- Testing stuff -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.0.13</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.26</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Test.properties:

testInjection="itworks"

** Some output**

INFO: Refreshing Root WebApplicationContext: startup date [Thu Mar 27 13:04:37 EDT 2014]; root of context hierarchy
Mar 27, 2014 1:04:37 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-config.xml]
Mar 27, 2014 1:04:37 PM org.springframework.context.support.PropertySourcesPlaceholderConfigurer loadProperties
INFO: Loading properties file from class path resource [Test.properties]
Mar 27, 2014 1:04:37 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
Mar 27, 2014 1:04:37 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
Was it helpful?

Solution

I had to add the @Component annotation as well..

package com.foo;

@Component
@Service
@Path("/test")
public class Tester implements Serializable {

    private static final long serialVersionUID = 7192266167739106825L;

    @Value("${testInjection}")
    private String throttleTime;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("/test")
    public String test() {
        return "injected value: " + throttleTime;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top