Я попробовал использовать «реализует ServletContextAware»:
Код: Выделить всё
@Component
public class Utils implements ServletContextAware{
private ServletContext servletContext;
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
System.out.println("**** "+servletContext);
}
}
И маршрут @Autowired:
Код: Выделить всё
@Component
public class Utils{
@Autowired
private ServletContext servletContext;
Код: Выделить всё
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.servlet.ServletContext] found for dependency: expected at le
ast 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Код: Выделить всё
public class WebAppInitializer implements WebApplicationInitializer {
private static Logger LOG = LoggerFactory.getLogger(WebAppInitializer.class);
@Override
public void onStartup(ServletContext servletContext) {
WebApplicationContext rootContext = createRootContext(servletContext);
configureSpringMvc(servletContext, rootContext);
FilterRegistration.Dynamic corsFilter = servletContext.addFilter("corsFilter", CORSFilter.class);
corsFilter.addMappingForUrlPatterns(null, false, "/*");
// configureSpringSecurity(servletContext, rootContext);
}
private WebApplicationContext createRootContext(ServletContext servletContext) {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
// rootContext.register(CoreConfig.class, SecurityConfig.class);
rootContext.register(CoreConfig.class);
rootContext.refresh();
servletContext.addListener(new ContextLoaderListener(rootContext));
servletContext.setInitParameter("defaultHtmlEscape", "true");
return rootContext;
}
Код: Выделить всё
@Configuration
public class CoreConfig {
@Bean
public CaptionFixture createCaptionFixture() {
return new CaptionFixture();
}
@Bean
public Utils createUtils () {
return new Utils();
}
}
Я просмотрел предложенные ответы: здесь и здесь, и это не сработало.
Подробнее здесь: https://stackoverflow.com/questions/214 ... t-spring-3
Мобильная версия