我正在尝试使用Spring MVC和JavaConfigs而不是XML文件来创建Hello World应用程序。我读过如果使用@Configuration注释类,可以使用@Bean声明bean。我有一个用@Configuration注释的类和一个用@Bean注释的方法。
我在IntelliJ 12中收到错误消息(红色下划线错误):
@Bean方法仅在@Configuration中声明时才有效 注释类
Tomcat 7.0.37启动时出现以下错误消息:
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("/");
}
}
作为一个例子,我使用了本教程:Migrate Spring MVC servlet.xml to Java Config以及WebAppInitializer上的Spring文档。
我的git repo托管在Bitbucket here上。
答案 0 :(得分:1)
看看你的pom,你添加了spring-javaconfig。 spring-javaconfig已合并到spring 3.X中,因此您不需要它。删除它,它应该运行正常。