jersey和spring集成,不能注入service的问题解决方法

来源:互联网 发布:java equals 编辑:程序博客网 时间:2024/05/16 05:27

package cn.mykoo.ws.resources;

import java.io.File;
import java.io.FileInputStream;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.mykoo.resource.Resource;
import cn.mykoo.resource.ResourceService;

@Path("/download/{id}")
@Component
@Scope("request")
public class DownloadResource {
private ResourceService resourceService;
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
     public byte[] download(@PathParam("id") long id)throws Exception{
Resource resource=(Resource)resourceService.getObject(Resource.class, id);
String path=ServletActionContext.getServletContext().getRealPath(resource.getFilePath());
     FileInputStream fis=new FileInputStream(new File(path));
     byte [] b=new byte[fis.available()];
     fis.read(b);
     return b;
     }
public ResourceService getResourceService() {
return resourceService;
}
public void setResourceService(ResourceService resourceService) {
this.resourceService = resourceService;
}

}


<?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-2.5.xsd">
     <context:component-scan base-package="cn.mykoo.ws.resources"/>
     
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
             <property name="sessionFactory">
                 <ref bean="sessionFactory" />
             </property>
</bean>

<bean id="baseTxProxy" lazy-init="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
             <property name="transactionManager">
                 <ref bean="transactionManager" />
             </property>
<property name="transactionAttributes">
                     <props>
                         <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                         <prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
                          <prop key="add*">PROPAGATION_REQUIRED</prop>
                          <prop key="update*">PROPAGATION_REQUIRED</prop>
                         <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
                         <prop key="remove*">PROPAGATION_REQUIRED,-Exception</prop>
                         <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
                         <prop key="increase*">PROPAGATION_REQUIRED,-Exception</prop>
                           <prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
                     </props>
</property>
</bean>

</beans>


<?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">
     <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>/WEB-INF/classes/applicationContext-*.xml</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</servlet-name>
      <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
      <load-on-startup>2</load-on-startup>
      </servlet>
      <servlet-mapping>
      <servlet-name>jersey-spring</servlet-name>
      <url-pattern>/rest/*</url-pattern>
      </servlet-mapping>
     
    <servlet>
    <display-name>JAX-RS REST Servlet</display-name>
    <servlet-name>JAX-RS REST Servlet</servlet-name>
    <servlet-class>
  com.sun.jersey.spi.container.servlet.ServletContainer
  </servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>cn.mykoo.ws.resources</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>JAX-RS REST Servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
     <filter>
         <filter-name>encodingFilter</filter-name>
         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
         <init-param>
             <param-name>encoding</param-name>
             <param-value>UTF-8</param-value>
         </init-param>
     </filter>
     <filter-mapping>
         <filter-name>encodingFilter</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>
     
     <filter>
         <filter-name>AdminsFilter</filter-name>
         <filter-class>cn.mykoo.utils.AdminsFilter</filter-class>
     </filter>
     <filter-mapping>
         <filter-name>AdminsFilter</filter-name>
         <url-pattern>/admins/*</url-pattern>
     </filter-mapping>

     <filter>
         <filter-name>struts</filter-name>
         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
     </filter>
     <filter-mapping>
         <filter-name>struts</filter-name>
         <url-pattern>*.action</url-pattern>
     </filter-mapping>
     
     <servlet>
         <servlet-name>CheckCode</servlet-name>
         <servlet-class>cn.mykoo.utils.CheckCode</servlet-class>
     </servlet>
     <servlet-mapping>
         <servlet-name>CheckCode</servlet-name>
         <url-pattern>/CheckCode</url-pattern>
     </servlet-mapping>

     <session-config>
         <session-timeout>30</session-timeout>
     </session-config>
     
     <filter>   
     <filter-name>hibernateFilter</filter-name>   
     <filter-class>   
      org.springframework.orm.hibernate3.support.OpenSessionInViewFilter   
      </filter-class>  
     </filter>   
   <filter-mapping>   
       <filter-name>hibernateFilter</filter-name>   
      <url-pattern>/*</url-pattern>   
  </filter-mapping> 
     
     
     <welcome-file-list>
         <welcome-file>index.jsp</welcome-file>
     </welcome-file-list>
</web-app>

 

把<servlet>
<display-name>JAX-RS REST Servlet</display-name>
<servlet-name>JAX-RS REST Servlet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>cn.mykoo.ws.resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS REST Servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
去掉就可以了