我正在学习Spring,并尝试创建简单的SOA项目,我有一个简单的测试类:
@Path("/hello")
public class HelloWorldResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayhello() { return "hello"};
}但是我想要JAX-RS的接口和一些自动注入的实现:
@Path("/hello")
public interface HelloWorldResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
String sayhello();
}
public class HelloWorldImpl implements HelloWorldResource {
@Override
public String sayhello() {
return "hello";
}
}我知道在Java中我可以用一个注解(例如@Stateless)就能做到这一点,但是我怎么才能用Spring 4+做同样的事情呢?
发布于 2019-07-26 23:12:04
这篇文章做了一些假设:
/hello路由到HelloWorldResource。如果这对您没有任何意义,那么您将需要考虑将Jax-RS资源注册到您的servlet容器。
org.springframework.web.context.ContextLoaderListener来设置了一个Spring上下文。您需要标记您的HelloWorldImpl,以便在spring之前被发现。要做到这一点,最简单的方法是在spring context.xml中启用注释处理(通过将其作为contextConfigLocation传递给ContextLoaderListener来加载)。
然后,用@Controller标记HelloWorldImpl并将其声明为context.xml中的bean就足够了。
https://stackoverflow.com/questions/31839511
复制相似问题