我试图在Jersey / Spring应用程序中获取属性以注入成员变量,如下所示。当我在localhost:8080 / test / test上调用该服务时,String只是为null。我在容器启动时也没有收到任何错误。一切看起来都像预期的那样在类路径上得到解决。
我下面做错了什么吗?我可以提供更多信息来帮助排除故障吗?
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;
}
}
弹簧-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"
**某些输出**
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
答案 0 :(得分:4)
我还必须添加@Component注释..
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;
}
}