Spring自动布线在RESTeasy服务中失败
我有一个简单的服务无法自动连接bean。 尽管通过上下文获得相同的bean是成功的。 因此,在存储库中创建和注册bean是可行的,但自动连接没有。 更改服务MyRepository->YourRepository中字段的类时,会抛出一个错误,说明这样的bean不存在,因此自动连接机制正在工作 你知道可能会遗漏什么吗Spring自动布线在RESTeasy服务中失败,spring,jboss,resteasy,Spring,Jboss,Resteasy,我有一个简单的服务无法自动连接bean。 尽管通过上下文获得相同的bean是成功的。 因此,在存储库中创建和注册bean是可行的,但自动连接没有。 更改服务MyRepository->YourRepository中字段的类时,会抛出一个错误,说明这样的bean不存在,因此自动连接机制正在工作 你知道可能会遗漏什么吗 @Component @Path("/") public class RestService { @Autowired private MyRepository
@Component
@Path("/")
public class RestService {
@Autowired
private MyRepository myRepository; // is not autowired and is null
@GET
@Path("/{param}")
@Produces(MediaType.APPLICATION_JSON)
public Response printMessage(@PathParam("param") String msg) {
return Response.ok(
AppContext.getContext().getBean("myRepository") == myRepository)
.build(); // false
}
public void setMyRepository(MyRepository myRepository) {
this.myRepository = myRepository;
}
}
上面的AppContext是ApplicationContextAware的简单实现
存储库
@Repository
public interface MyRepository extends MongoRepository<MyEntity, String> {
}
和配置类
@Configuration
@EnableMongoRepositories("my.package.repository")
@ComponentScan("my.package")
public class MyConfiguration {
@Bean
public MongoTemplate mongoTemplate() throws UnknownHostException {
return new MongoTemplate(new MongoClient("localhost"), "db");
}
}
在2条评论后编辑
我正在使用以下库进行RESTeasy-spring集成。
我还需要别的吗
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-spring</artifactId>
<version>3.0.8.Final</version>
</dependency>
和MyContext类以获得更清晰的信息
@Component
public class MyContext implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
public static ApplicationContext getContext() {
return context;
}
}
Spring可能设置正确,但这并不意味着Spring+RestEasy集成设置正确 我发布的代码是我在RestEasy 3.0.6和Spring 3.2.8中使用的web.xml配置,正确设置了RestEasy和Spring之间的集成,还设置了Spring MVC/api下的所有内容都由RestEasy处理,其他所有内容都由Spring MVC处理
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>my.package.config.ApplicationConfig</param-value>
</context-param>
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>web</param-value>
</context-param>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/api</param-value>
</context-param>
<!-- Spring + RESTEasy -->
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<listener>
<listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
</listener>
<!-- RESTEasy Servlet-->
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<!-- Spring MVC Servlet -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>my.package.config.MvcConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/jsp/error404.jsp</location>
</error-page>
</web-app>
Spring可能设置正确,但这并不意味着Spring+RestEasy集成设置正确 我发布的代码是我在RestEasy 3.0.6和Spring 3.2.8中使用的web.xml配置,正确设置了RestEasy和Spring之间的集成,还设置了Spring MVC/api下的所有内容都由RestEasy处理,其他所有内容都由Spring MVC处理
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>my.package.config.ApplicationConfig</param-value>
</context-param>
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>web</param-value>
</context-param>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/api</param-value>
</context-param>
<!-- Spring + RESTEasy -->
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<listener>
<listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
</listener>
<!-- RESTEasy Servlet-->
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<!-- Spring MVC Servlet -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>my.package.config.MvcConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/jsp/error404.jsp</location>
</error-page>
</web-app>
我自己设法解决了这个问题 正确的解决方案是要么更换
public class MyInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
container.addListener(new ResteasyBootstrap());
final AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
ContextLoaderListener springListener = new ContextLoaderListener(rootContext);
rootContext.register(MyConfiguration.class);
container.addListener(springListener);
}
}
与
但在这种情况下,我失去了使用AnnotationConfigWebApplicationContext的可能性。
还可以按如下方式更改初始值设定项以保留注释上下文
public class MyInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
container.addListener(new ResteasyBootstrap());
final AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
ContextLoaderListener springListener = new ContextLoaderListener(rootContext) {
@Override
protected ContextLoader createContextLoader() {
return new SpringContextLoader();
}
};
rootContext.register(MyConfiguration.class);
container.addListener(springListener);
}
}
我自己设法解决了这个问题 正确的解决方案是要么更换
public class MyInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
container.addListener(new ResteasyBootstrap());
final AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
ContextLoaderListener springListener = new ContextLoaderListener(rootContext);
rootContext.register(MyConfiguration.class);
container.addListener(springListener);
}
}
与
但在这种情况下,我失去了使用AnnotationConfigWebApplicationContext的可能性。
还可以按如下方式更改初始值设定项以保留注释上下文
public class MyInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
container.addListener(new ResteasyBootstrap());
final AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
ContextLoaderListener springListener = new ContextLoaderListener(rootContext) {
@Override
protected ContextLoader createContextLoader() {
return new SpringContextLoader();
}
};
rootContext.register(MyConfiguration.class);
container.addListener(springListener);
}
}
Spring不会将null注入到您的字段中。如果字段为null,则对象不受Spring管理。你有合适的Spring/JAX-RS支持库吗?我很确定你在MyInitializer中缺少了一些处理RestEasy和Spring之间集成的东西。不幸的是,我不知道具体是什么,因为我只使用web.xml进行过集成file@geoand您能提供一个web.xml示例吗?我在网上见过无数这样的人,他们都是differ@P.Šileikis你有没有做到这一点?我有一个非常类似的问题,我想知道你是如何解决这个问题的it@K2J不是真的,最后我们完全删除了spring,只是将普通jee与jboss容器一起使用。在这种情况下,resteasy工作得很好,并且易于设置。Spring不会将null注入到您的字段中。如果字段为null,则对象不受Spring管理。你有合适的Spring/JAX-RS支持库吗?我很确定你在MyInitializer中缺少了一些处理RestEasy和Spring之间集成的东西。不幸的是,我不知道具体是什么,因为我只使用web.xml进行过集成file@geoand您能提供一个web.xml示例吗?我在网上见过无数这样的人,他们都是differ@P.Šileikis你有没有做到这一点?我有一个非常类似的问题,我想知道你是如何解决这个问题的it@K2J不是真的,最后我们完全删除了spring,只是将普通jee与jboss容器一起使用。在这种情况下,resteasy可以完美地工作,并且易于设置。