@Autowired没有在泽西资源上工作

时间:2013-04-16 13:54:49

标签: spring jersey

workflowService为空。 bean配置是正确的,因为手动注入在应用程序的其他部分中工作正常。

这是我的资源:

@Path("/workflowProcess")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public class WorkflowProcessResource {

    @Autowired
    WorkflowService workflowService;

    @Autowired
    WorkflowProcessService workflowProcessService;

    @GET
    @Path ("/getWorkflowProcesses/{uuid}")
    public Collection<WorkflowProcessEntity> getWorkflows (@PathParam("uuid") String uuid) {
        WorkflowEntity workflowEntity = workflowService.findByUUID(uuid);

        return workflowEntity.getWorkflowProcesses();
    }
}

我在http://www.mkyong.com/webservices/jax-rs/jersey-spring-integration-example/等网站上的Google上发现,看起来ContextLoaderListener是关键。但我已经将其添加到应用程序上下文中。

import com.sun.jersey.spi.container.servlet.ServletContainer;
import com.sun.jersey.spi.spring.container.servlet.SpringServlet;
import org.atmosphere.cpr.AtmosphereFramework;
import org.atmosphere.cpr.AtmosphereServlet;
import org.atmosphere.handler.ReflectorServletProcessor;
import org.glassfish.grizzly.servlet.ServletRegistration;
import org.glassfish.grizzly.servlet.WebappContext;
import org.glassfish.grizzly.websockets.WebSocketAddOn;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.http.server.NetworkListener;

import java.io.IOException;
import java.util.logging.Logger;

public class Main {

    protected static final Logger logger = Logger.getLogger(Main.class.getName());

    public static void main(String[] args) throws IOException {
        logger.info("Starting server...");
        final HttpServer server = HttpServer.createSimpleServer(".", 8181);

        WebappContext ctx = new WebappContext("Socket", "/");

        //enable annotation configuration
        ctx.addContextInitParameter("contextClass", "org.springframework.web.context.support.AnnotationConfigWebApplicationContext");
        ctx.addContextInitParameter("contextConfigLocation", "com.production");

        //allow spring to do all of it's stuff
        ctx.addListener("org.springframework.web.context.ContextLoaderListener");

        //add jersey servlet support
        ServletRegistration jerseyServletRegistration = ctx.addServlet("JerseyServlet", new SpringServlet());
        jerseyServletRegistration.setInitParameter("com.sun.jersey.config.property.packages", "com.production.resource");
        jerseyServletRegistration.setInitParameter("com.sun.jersey.spi.container.ContainerResponseFilters", "com.production.resource.ResponseCorsFilter");
        jerseyServletRegistration.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
        jerseyServletRegistration.setLoadOnStartup(1);
        jerseyServletRegistration.addMapping("/api/*");

2 个答案:

答案 0 :(得分:5)

我认为,您需要@InjectParam代替@Autowired

答案 1 :(得分:1)

@InjectParam工作正常,而不是@Autowired,略有改变

@InjectParam不能应用于构造函数本身,因此必须应用于构造函数的参数。

<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("#Contact").hide(); $("#show").click(function(){ $("#Contact").show(2000); }); }); </script> </head> <body> <div id="Contact"> <input type="text"><br/> <input type="text"><br/> <input type="text"><br/> <input type="text"> </div> <button id="show">Show</button> </body> </html>