1

我目前正在尝试将我的服务器设置为在 tomcat 上使用 jersey、spring 和 hibernate。目前,该项目使用 maven 构建并部署在 tomcat 上。当我在调试模式下运行并调用一些 GET 方法时,会调用 com.xyzrequest 包中正确的 @GET 方法,但是此类中的自动装配服务类为空,我不知道为什么。我怀疑这是一个配置问题,但我对此有点陌生,不确定我哪里出错了。

服务器启动时我在控制台中得到这个

....
Sep 13, 2013 11:18:56 PM com.sun.jersey.spi.spring.container.servlet.SpringServlet getContext
INFO: Using default applicationContext
Sep 13, 2013 11:18:56 PM com.sun.jersey.spi.spring.container.SpringComponentProviderFactory registerSpringBeans
INFO: Registering Spring bean, testRequestService, of type com.x.y.z.TestRequestService as a root resource class
....

所以看起来请求bean正在正确初始化

应用程序上下文.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-3.2.xsd    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/springjee-3.2.xsd    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<context:component-scan
    base-package="com.x.y.z.request, com.x.y.z.service, com.x.y.z.dao" />

<context:annotation-config/>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://localhost:5432/db" />
    <property name="username" value="user" />
    <property name="password" value="password" />
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="loadTimeWeaver">
        <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
     </property>
     <property name="persistenceUnitName" value="serverPersistenceUnit" />
     <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
    </property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

web.xml(我将 * 写为 '*' 因为它正在注释掉文本块)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<display-name>MyWebApp</display-name>

<description>
My Web App
</description>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<servlet>
    <servlet-name>Jersey Spring Web Application</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.x.y.z.request</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Spring Web Application</servlet-name>
    <url-pattern>/webresources/'*'</url-pattern>
</servlet-mapping>

<!-- JAX RS config -->
<filter>
    <filter-name>Jersey Filter</filter-name>
    <filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
    <init-param>
        <param-name>com.sun.jersey.config.feature.Trace</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.config.feature.Redirect</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.config.feature.ImplicitViewables</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.x.y.z.request</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.config.feature.FilterForwardOn404</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>Jersey Filter</filter-name>
    <url-pattern>/'*'</url-pattern>
</filter-mapping>
</web-app>

持久性.xml

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence    http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="serverPersistenceUnit">
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    </properties>
</persistence-unit>

测试请求服务.java

@Component
@Path("/test")
public class TestRequestService 
{
    @Autowired
    private IDelegate delegate;

    @GET
    @Path("{id}") 
    @Produces(MediaType.APPLICATION_JSON)
    public List<IObject> get(@PathParam("id") int id)
    {   
         return delegate.get(id);
    } 
}

IBusinessDelegate.java

@Service
public class Delegate extends AbstractDelegate<IObject> implements IDelegate
{
    @Autowired
    private IDAO<IObject> dao;

    @Override
    public List<IObject> get(int id)
    {
        dao.get(id);
    }
}

DAO.java

@Repository
public class DAO extends AbstractDAO<IObject> implements IDAO<IObject>
{
    @Override
    public List<IObject> get(int id)
    {
        //some query here
    }
}

AbstractDAO.java

public abstract class AbstractDAO<T> implements IDAO<T>
{
    @PersistenceContext
    protected EntityManager entityManager;

    public abstract List<T> get(int id);
}
4

2 回答 2

2

问题的最可能原因是 Jersey 未配置为使用 spring 应用程序上下文来获取对象实例。因此 Jersey 正在创建 TestRequestSerivce 的实例,这意味着 Jersey 将忽略 @Autowired 因为它不理解它。

您是否将 Jersey servlet 配置为使用 spring 来获取控制器实例。请参阅 http://www.mkyong.com/webservices/jax-rs/jersey-spring-integration-example特别注意 jersey 的 servlet 应该知道 spring。

<servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>
            com.sun.jersey.spi.spring.container.servlet.SpringServlet
        </servlet-class>
        <init-param>
            <param-name>
                                 com.sun.jersey.config.property.packages
                        </param-name>
            <param-value>com.mkyong.rest</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
于 2013-09-14T01:28:28.687 回答
0

事实证明,这仅仅是因为我使用错误的 URL 进行测试。您可以在我的 web.xml 中看到我的 servlet-mapping 的 URL 模式是 /webresources/*。如果我通过在浏览器中输入 localhost:8080/my-webservice/test/1 进行测试,它将在“/test”请求 bean 中命中我的断点,但 spring 没有自动装配字段(可能是因为它是错误的 URL 模式对于这个 jersey/spring servlet)。当我开始输入 localhost:8080/my-webservice/webresources/test/1 时,一切都开始正常工作。这是一个非常愚蠢的错误,但我只是假设如果球衣收到请求,URL 是正确的,也许有人可以解释为什么错误的 URL 会发生这种情况。

于 2013-09-14T13:52:03.617 回答