NoSuchBeanDefinitionException: No qualifying bean of type found for dependency using annotaition - spring

I have such project structure:
persistence.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="todos" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/PostgreSQLDS</jta-data-source>
<class>server.entity.Holiday</class>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
Web.xml (As you can see, I'm using context Class where I will use JSON):
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd"
metadata-complete="false">
<display-name>Spring MVC Application</display-name>
<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>server.config</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
WebAppConfig.class:
#Configuration
#EnableWebMvc
#ComponentScan("server")
public class WebAppConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
}
#Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(new ObjectMapper());
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
converters.add(converter);
}
#Bean
public InternalResourceViewResolver setupViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/pages/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
}
HolidayController.class
#RestController
#RequestMapping("/holidays")
public class HolidayController {
#Autowired
private HolidayRepository holidayRepository;
#RequestMapping(value = "", method = RequestMethod.GET)
#ResponseBody
public List<Holiday> getAllHolidays() {
// List<User> list = userRepository.findAll();
return holidayRepository.findAll();
}
HolidayRepository.class:
#Repository
public interface HolidayRepository extends JpaRepository<Holiday, Long> {
}
I've got this error:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [server.repository.HolidayRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
Help me, please.

I seems you need #EnableJpaRepositories in your JavaConfig
See secton 3.2 -> http://docs.spring.io/spring-data/jpa/docs/1.9.4.RELEASE/reference/html/#repositories.definition
Create a new JavaConfig class:
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
#Configuration
#EnableJpaRepositories
class JpaSpringDataConfig {}
Or add this to your XML config:
<jpa:repositories base-package="server.repository"/>
Since your log says it expects a WebApplicationInitializer, remove your web.xml and include a class like:
public class RestWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
#Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { JpaSpringDataConfig.class };
}
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebAppConfig.class };
}
#Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}

Add the annotation #Repository to the implementation of HolidayRepository interface.

Related

Tomcat war deployment gives 404 when using annotations instead of web.xml

I am trying to learn Spring framework. When I use Spring annotation for configuration instead of web.xml and deploy war file on Tomcat in Docker container, it gives 404 error. After switching to web.xml and servlet.xml configuration, it gives no error.
My config file:
package com.janfranco.mvctutorial.config;
...
#Configuration
#EnableWebMvc
#ComponentScan
public class AppConfig implements WebMvcConfigurer {
#Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
My servlet file:
package com.janfranco.mvctutorial.config;
...
public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
#Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { AppConfig.class };
}
#Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
I use Maven for management. Here is my Dockerfile:
FROM tomcat:8.0.20-jre8
COPY target/mvctutorial.war /usr/local/tomcat/webapps/
How can I use annotations and get rid of xml configs?
Edit:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>mvctutorial</display-name>
<absolute-ordering />
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

GlassFish Server status 404 No mapping found for HTTP request with URI in DispatcherServlet

I'm trying to integrate JSF 2 with Spring and I was following this example and making some modifications to access a DB and execute a Stored Procedure.
But when I run the project I'm getting a Status 404 - Not found from the GlassFish Server. In the console log I'm getting the message:
Warning: No mapping found for HTTP request with URI [/] in DispatcherServlet with name 'dispatcher'
Here is my resulting code:
Folder Structure
Initializer.java
public class Initializer implements WebApplicationInitializer {
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);
ctx.setServletContext(servletContext);
servletContext.addListener(new ContextLoaderListener(ctx));
servletContext.addListener(new RequestContextListener());
Dynamic dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
dynamic.addMapping("/");
dynamic.setLoadOnStartup(1);
}
}
AppConfig.java
#Configuration
#ComponentScan("source")
class AppConfig {
#Bean
public Service service() {
DriverManagerDataSource ds = new DriverManagerDataSource("jdbc:mysql://localhost:3306/test?zeroDateTimeBehavior=convertToNull", "root", "rootPass");
ds.setDriverClassName("com.mysql.jdbc.Driver");
return new ServiceImpl(ds);
}
}
ProcBean.java
#ManagedBean(name = "procBean", eager = true)
#RequestScoped
#Component
#RequestMapping("/")
public class ProcBean {
private int input;
private int output;
#Autowired public Service procService;
// Empty constructor, getters/setters
public String callStoredProcedure() {
this.output = procService.callStoredProcedure(input);
return "output";
}
}
ServiceImpl.java
public class ServiceImpl implements Service {
private DataSource dataSource;
private StoredProcedurePrueba prueba;
public ServiceImpl(DataSource dataSource) {
this.dataSource = dataSource;
}
// get/set dataSource
#Override
public int callStoredProcedure(int input) {
this.prueba = new StoredProcedurePrueba(dataSource);
return this.prueba.execute(input);
}
private class StoredProcedurePrueba extends StoredProcedure {
// Implementation tested separately and working correctly
}
}
Configuration
faces-config.xml
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
</application>
</faces-config>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<display-name>JSF 2 + Spring 4 Integration Example</display-name>
<servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
Web pages
<!-- input.xhtml -->
<h:body>
<h3>JSF 2 + Spring 4 Integration Example</h3>
<h:form id="studentForm">
<h:outputLabel value="Enter Student id:" />
<h:inputText value="#{procBean.input}" /> <br />
<h:commandButton value="Submit" action="#{procBean.callStoredProcedure()}"/>
</h:form>
</h:body>
<!-- output.xhtml -->
<h:body>
<h3>JSF 2 + Spring 4 Integration Example</h3>
<p>#{procBean.output}</p>
</h:body>
I was trying some other solutions but none of them works for me. Any idea? What am I missing?
Thanks in advance for your answers.
Not using Spring but I would think you need a welcome-file in web.xml like
<welcome-file-list>
<welcome-file>input.xhtml</welcome-file>
</welcome-file-list>

how to autowire bean in the servlet filters in spring application?

I have a spring-boot application.
I have no ApplicationContext.xml or web.xml files in my project. I prefer to avoid them and have everything configured in Java code.
I have read the following the posts about bean injection in servlet filters.
How can I get a Spring bean in a servlet filter?
http://www.deadcoderising.com/2015-05-04-dependency-injection-into-filters-using-delegatingfilterproxy/
spring injection in servlet filter
After reading them, I started to use DelegatingFilterProxy.
My question is how to autowire the bean into filter and avoid using xml files especially for DelegatingFilterProxy configuration.
The code snipped is available from the second post hosted in github.
public class AuditHandler {
public void auditRequest(String appName, ServletRequest request) {
System.out.println(appName + ": Received request from " + request.getRemoteAddr() );
}
}
public class AuditFilter implements Filter {
private final AuditHandler auditHandler;
private String appName;
public AuditFilter(AuditHandler auditHandler) {
this.auditHandler = auditHandler;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
auditHandler.auditRequest(appName, request);
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
appName = filterConfig.getInitParameter("appName");
}
public void destroy() {}
}
ApplicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="auditHandler" class="com.deadcoderising.AuditHandler">
</bean>
<bean id="auditFilter" class="com.deadcoderising.AuditFilter">
<constructor-arg ref="auditHandler"/>
</bean>
</beans>
web.xml
<web-app version="3.0"
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_3_0.xsd"
metadata-complete="true">
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<filter>
<filter-name>auditFilter</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>appName</param-name>
<param-value>di-example</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>auditFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
You should add a FilterRegistrationBean to your main Application class (class annotated with #SpringBootApplication) and let Spring provide instance of the AuditHandler:
#Bean
#Autowired
public FilterRegistrationBean auditFilterRegistration(AuditHandler handler) {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new AuditFilter(handler));
filterRegistrationBean.setOrder(3); // ordering in the filter chain
return filterRegistrationBean;
}
If this doesn't work (e.g. your AuditHandler implementation is not annotated properly or it's not on the default package scanning path) you can instruct Spring to provide it (also in your #SpringBootApplication annotated class):
#Bean
public AuditHandler auditHandler() {
return new AuditHandlerImplementation();
}

Trouble spring injection in JSF2 Bean with annotation

I have a trouble with Spring Injection in my web project. I must use in a JSF2 bean.
Show my work :
SgbdServiceImpl.java (shorted)
#Service
public class SgbdServiceImpl implements SgbdService {
#Override
public List<Sgbd> findAll() {
return null;
}
#Override
public Sgbd findOneByName(String nom) {
return null;
}
}
SgbdBean
#Component
#SessionScoped
#ManagedBean(name="sgbd")
public class SgbdBean {
#Autowired
SgbdService sgbdService;
public List<Sgbd> findAll(){
return sgbdService.findAll();
}
}
I put this configuration in the file : web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/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>
This Spring configuration in applicationContext.xml :
<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="main.java.com.erdf.agir.services" />
</beans>
And, in faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config 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-facesconfig_2_1.xsd"
version="2.1">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
I would like call findAll() from service but i obtain all time nullPointerException from sgbdService attribut (Autowired failled ?)
I follow this example : http://rsuna.blogspot.fr/2013/05/how-to-integrate-jsf-20-with-spring-3.html
Did I miss anything ?
You have a context clash; using both #Component and #ManagedBean on the same class definition put your bean in two contexts: JSF and Spring's. Let's now establish that #Autowired will not work in the JSF context.
You could get rid of the spring-based annotations and go with a JSF-centric setup. What this will leave with you with
#SessionScoped
#ManagedBean(name="sgbd")
public class SgbdBean {
#ManagedProperty(value="#{sgbdServiceImpl}")
SgbdService sgbdService;
public List<Sgbd> findAll(){
return sgbdService.findAll();
}
}
You could stick with a strictly spring-centric approach with
#Component
public class SgbdBean {
#Autowired
SgbdService sgbdService;
public List<Sgbd> findAll(){
return sgbdService.findAll();
}
}
Things I have encountered
1) Better to mention service name with annotation - #Service("sgbdService")
2) Rather than #ManagedBean it is better to use #Qualifier annotation -
#Qualifier("sgbdBean")
3) Add <context:sping-configured/> entry to applicationContext.xml file
4) Try with top level component scan entry as
<context:component-scan base-package="main.java.com.erdf" />

Spring autowiring fails in RESTeasy service

I have a simple service which fails to autowire bean.
Although getting the same bean through context succeeds.
So the bean creation and registration in repository is working, but autowiring does not.
Changing class of the field in the service (MyRepository -> YourRepository), there is an error thrown that such bean does not exists, so the autowiring mechanism is working.
Any ideas what might be missing?
#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 above is my simple implementation of ApplicationContextAware
Repository
#Repository
public interface MyRepository extends MongoRepository<MyEntity, String> {
}
There is no .xml configuration and spring is initialized through
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);
}
}
And configuration class
#Configuration
#EnableMongoRepositories("my.package.repository")
#ComponentScan("my.package")
public class MyConfiguration {
#Bean
public MongoTemplate mongoTemplate() throws UnknownHostException {
return new MongoTemplate(new MongoClient("localhost"), "db");
}
}
EDIT after 2 comments
I'm using the following library for RESTeasy - spring integration.
Do I need some other?
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-spring</artifactId>
<version>3.0.8.Final</version>
</dependency>
I think JAX-RS and RESTeasy is working correctly, because service is working and I can access it through web when deployed on JBoss
EDIT for workaround
Service is initialized correctly if I create the following constructor, but it feels more like a workaround
public RestService() {
this.myRepository = MyContext.getContext().getBean(MyRepository.class);
}
And MyContext class for more clarity
#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 might be setup correctly, but that doesn't necessary mean that Spring+RestEasy integration is setup correctly.
The code I am posting is the web.xml configuration that I have used (with RestEasy 3.0.6 and Spring 3.2.8) and correctly sets up the integration between RestEasy and Spring and also sets up Spring MVC (everything under /api is handled by RestEasy, everything else is handled by 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>
Managed to solve the issue myself.
The correct solution is either to replace
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);
}
}
with
public class MyInitializer implements WebApplicationInitializer {
#Override
public void onStartup(ServletContext container) throws ServletException {
container.addListener(new ResteasyBootstrap());
container.addListener(new SpringContextLoaderListener());
}
}
but in this case I'm losing possibility to use AnnotationConfigWebApplicationContext.
Also the initializer can be changed as follows to preserve annotation context.
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);
}
}

Resources