我想使用Spring MVC和JavaConfigs而不是XML文件製作Hello World應用程序。我讀過你可以使用@Bean聲明一個bean,如果該類用@Configuration註解的話。我有一個用@Configuration註解的類和一個用@Bean註解的方法。一個@Configuration 註解類「@Bean方法僅在@Configuration註釋類中聲明時有效」錯誤
而下面的內聲明的,只有當
@Bean方法是有效的:
我的IntelliJ內12(紅色下劃線錯誤)收到錯誤消息在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回購託管在Bitbucket here上。
發佈的代碼在這裏工作正常,使用3.2.6.RELEASE。你使用的是什麼版本的春天?你在代碼中的其他地方使用@Bean嗎? –
@SheenaArtrip我也在使用3.2.6.RELEASE。這是唯一的@Bean。我的另一個類是'公共類WebAppInitializer實現WebApplicationInitializer',我使用[Spring文檔](http://docs.spring.io/spring/docs/3.1.x/javadoc-api/org/springframework/web/WebApplicationInitializer的.html)。如果我嘗試啓動tomcat,我得到'SEVERE:上下文初始化失敗 ...無法讀取候選組件類:文件[... \ com \ springapp \ mvc \ WebAppConfig.class];' –
這也可以。發佈您的WebApplicationInitializer。你在哪個容器中運行?你刪除的進口是什麼樣的? –