I am trying to make a Hello World app using Spring MVC and JavaConfigs instead of XML files. I read that you can declare a bean using @Bean if the class is annotated with @Configuration. I have a class annotated with @Configuration and a method inside annotated with @Bean.
I'm getting an error message within IntelliJ 12 (red underline error):
@Bean methods are valid only when declared within a @Configuration annotated class
And the following error message on Tomcat 7.0.37 startup:
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [S:\dropbox\Shared Folders\Majerus Eric (BBY)\SpringMVCBarebones\target\SpringMVCBarebones\WEB-INF\classes\com\springapp\mvc\WebAppConfig.class]; nested exception is java.lang.annotation.AnnotationFormatError: Invalid default: public abstract org.springframework.beans.factory.annotation.Autowire org.springframework.config.java.annotation.Bean.autowire()
WebAppConfig
package com.springapp.mvc;
import org.springframework.config.java.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@EnableWebMvc
@ComponentScan("com.springapp.mvc")
@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
return resolver;
}
}
WebAppInitializer
package com.springapp.mvc;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
public class WebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
// Create the 'root' Spring application context.
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
// Manage the lifecycle of the root application context.
container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(WebAppConfig.class);
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
As an example, I used this tutorial: Migrate Spring MVC servlet.xml to Java Config and also Spring documentation on WebAppInitializer.
My git repo is hosted on Bitbucket here.
Taking a look at your pom, you have spring-javaconfig added. spring-javaconfig has been merged into spring 3.X so you should not need it. Remove it and it should run fine.