2017-10-10 79 views
1

我想用Spring 4与Jersey 1.8一起使用。但我无法使用Spring在Rest类中注入依赖项。用Spring配置Rest(Jersey)

每次我尝试并调用自动装配的依赖项时,我都会得到NULL。任何人都可以提出为什么我的依赖没有被注入?

的web.xml

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

    <servlet> 
     <servlet-name>spring</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>spring</servlet-name> 
     <url-pattern>*.html</url-pattern> 
    </servlet-mapping> 



    <servlet> 
     <servlet-name>Rest</servlet-name> 
     <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring-servlet.xml</param-value> 
     </init-param> 
     <init-param> 
      <param-name>com.sun.jersey.config.property.packages</param-name> 
      <param-value>org.portal.services</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Rest</servlet-name> 
     <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

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


    <context:component-scan base-package="org.portal.services"></context:component-scan> 
    <context:annotation-config /> 

    <bean 
     class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> 
     <property name="location"> 
      <value>WEB-INF/dataSource.properties</value> 
     </property> 
    </bean> 
    <import resource="hibernate.cfg.xml" /> 
    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/"></property> 
     <property name="suffix" value=".jsp"></property> 
    </bean> 

    <bean id="addCountryService" class="org.portal.services.AddCountryService"> 
     <property name="addCountryProcessor" ref="addCountryProcessor"></property> 
    </bean> 

    <bean id="addCountryProcessor" class="org.portal.processors.AddCountryProcessor"> 
     <property name="hibernateTemplate" ref="hibernateTemplate" /> 
    </bean> 


</beans> 

休息Coltroller

package org.portal.services; 

import javax.ws.rs.Consumes; 
import javax.ws.rs.POST; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 
import org.portal.dto.Country; 
import org.portal.processors.AddCountryProcessor; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component; 
import com.thoughtworks.xstream.XStream; 
import com.thoughtworks.xstream.io.xml.DomDriver; 

@Component 
@Path("/Country") 
public class AddCountryService { 

    @Autowired 
    private AddCountryProcessor addCountryProcessor; 

    @POST 
    @Path("/addCountry") 
    @Produces(MediaType.APPLICATION_XML) 
    @Consumes(MediaType.APPLICATION_XML) 
    public String addCountry(String newCountry) { 

     XStream xStream = new XStream(new DomDriver()); 
     xStream.alias("country", Country.class); 
     xStream.alias("id", String.class); 
     xStream.alias("name", String.class); 
     xStream.alias("region", Integer.class); 

     Country country = (Country)xStream.fromXML(newCountry); 
     System.out.println("processor is "+addCountryProcessor); 

     if(country != null){ 
      addCountryProcessor.addCountry(country); 
     } 
     return newCountry; 
    } 

    public void setAddCountryProcessor(AddCountryProcessor addCountryProcessor) { 
     this.addCountryProcessor = addCountryProcessor; 
    } 

    public AddCountryProcessor getAddCountryProcessor() { 
     return addCountryProcessor; 
    } 

} 

发布请求后为 “http://localhost:7070/HumanResourcePortal/rest/Country/addCountry”,流量即将“addCountry( )“的方法,但它然后打印addCountryProcessor为空

+0

如果在实例化AddCountryProcessor类或hibernate时出错,您可以在开始战争或部署时检查日志吗? –

+0

控制台没有错误实例化AddCountryProcessor类 – shivam

+0

可能重复[this](https://stackoverflow.com/q/19745187/8101556)问题,你应该可以用[this]解决(https:// stackoverflow。 com/a/19751981/8101556)的答案。希望这可以帮助。如果您不想更改servlet,请使用“@ com.sun.jersey.spi.inject.Inject”而不是“Autowired”来尝试。 –

回答

0

有两个选项可以在这里找到:

  • 更换@Autowired@com.sun.jersey.spi.inject.Inject。这个@Inject注释将从组态的DI框架注入。

  • 使用com.sun.jersey.spi.spring.container.servlet.SpringServlet通过弹簧集成来部署您的资源。对于Spring集成,你需要包括以下依赖性:

    <dependency> 
        <groupId>com.sun.jersey.contribs</groupId> 
        <artifactId>jersey-spring</artifactId> 
        <version>1.8</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> 
        </exclusions> 
    </dependency> 
    

Spring的依赖都在这里排除把你宣布一个,而不是jersey-spring使用的一个。

+0

需要一个更多的帮助................占位符的不工作...从日志我可以看到属性文件正在加载,但占位符不工作在我的休眠文件 – shivam

+0

可以你在另一个问题上发布更多细节? –