我想在xml配置中声明的bean中使用@Component
自动注释bean。
XML
<context:annotation-config />
<context:component-scan base-package="test" />
<bean id="testClass" class="test.TestClass"/>
TestClass.java
public class TestClass {
@Autowired
LocaleProvider locale;
public void test() {
locale.getMessage("test");
}
}
LocaleProvider.java
@Component
public class LocaleProvider {
...
public String getMessage(String key, Object... args) throws NoSuchMessageException {
return ...;
}
})
TestController.java
@Controller
public class TestController {
@Autowired
TestClass test;
@RequestMapping("/")
public Object handle() {
test.test();
}
}
正确创建了xml bean,但变量locale
始终为null ...
堆栈跟踪
java.lang.NullPointerException
at test.TestClass.test(TestClass.java:16) // = locale.getMessage("test");
at test.TestController.handle(TestController.java:23)
日志
14:38:23,982 | INFO | DefaultListableBeanFactory:577 | Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1ef6144: defining beans [org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#0,_jsonConfigMapEditorConfigurer,_hibernateSerializationConfigPostProcessor,**testClass**,org.springframework.security.filterChains,org.springframework.security.filterChainProxy,org.springframework.security.web.DefaultSecurityFilterChain#0,org.springframework.security.web.PortMapperImpl#0,org.springframework.security.web.PortResolverImpl#0,org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0,org.springframework.security.authentication.ProviderManager#0,org.springframework.security.web.context.HttpSessionSecurityContextRepository#0,org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy#0,org.springframework.security.web.savedrequest.HttpSessionRequestCache#0,org.springframework.security.access.vote.AffirmativeBased#0,org.springframework.security.web.access.intercept.FilterSecurityInterceptor#0,org.springframework.security.web.access.DefaultWebInvocationPrivilegeEvaluator#0,org.springframework.security.authentication.AnonymousAuthenticationProvider#0,org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0,org.springframework.security.userDetailsServiceFactory,org.springframework.security.web.DefaultSecurityFilterChain#1,sessionRegistry,org.springframework.security.authentication.DefaultAuthenticationEventPublisher#0,org.springframework.security.authenticationManager,pathMatchingResourcePatternResolver]; root of factory hierarchy
...
14:38:24,002 | DEBUG | DefaultListableBeanFactory:217 | Creating shared instance of singleton bean 'testClass'
14:38:24,005 | DEBUG | DefaultListableBeanFactory:430 | Creating instance of bean 'testClass'
14:38:24,007 | DEBUG | DefaultListableBeanFactory:504 | Eagerly caching bean 'testClass' to allow for resolving potential circular references
14:38:24,012 | DEBUG | DefaultListableBeanFactory:458 | Finished creating instance of bean 'testClass'
...
14:38:24,970 | INFO | DefaultListableBeanFactory:577 | Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@ef96bc: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,**localeProvider**,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,viewResolver,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#1,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#1,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#2,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#2,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#3,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#3,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#4,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#4,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#5,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#5,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@1ef6144
...
14:38:24,993 | DEBUG | DefaultListableBeanFactory:217 | Creating shared instance of singleton bean 'localeProvider'
14:38:24,995 | DEBUG | DefaultListableBeanFactory:430 | Creating instance of bean 'localeProvider'
14:38:24,996 | DEBUG | DefaultListableBeanFactory:504 | Eagerly caching bean 'localeProvider' to allow for resolving potential circular references
14:38:25,007 | DEBUG | DefaultListableBeanFactory:458 | Finished creating instance of bean 'localeProvider'
...
14:38:25,008 | DEBUG | InjectionMetadata :61 | Found injected element on class [test.TestController]: AutowiredFieldElement for test.TestClass test.TestController.test
14:38:25,008 | DEBUG | DefaultListableBeanFactory:504 | Eagerly caching bean 'testController' to allow for resolving potential circular references
14:38:25,016 | DEBUG | InjectionMetadata :90 | Processing injected method of bean 'testController': AutowiredFieldElement for test.TestClass test.TestController.test
14:38:25,017 | DEBUG | DefaultListableBeanFactory:245 | Returning cached instance of singleton bean 'testClass'
14:38:25,017 | DEBUG | AutowiredAnnotationBeanPostProcessor:432 | Autowiring by type from bean name 'testController' to bean named 'testClass'
如果我在xml中声明LocaleProvider
,它可以正常工作。另外,如果我使用TestClass
注释@Component
,它也可以正常工作。我需要做注释+ xml混合配置。
我该如何解决这个问题?
环境: