Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java XML bean中的Autowire@Component bean_Java_Spring_Javabeans - Fatal编程技术网

Java XML bean中的Autowire@Component bean

Java XML bean中的Autowire@Component bean,java,spring,javabeans,Java,Spring,Javabeans,我想在xml配置中声明的bean中自动连接一个用@Component注释的bean XML <context:annotation-config /> <context:component-scan base-package="test" /> <bean id="testClass" class="test.TestClass"/> LocaleProvider.java public class TestClass { @Autowired

我想在xml配置中声明的bean中自动连接一个用
@Component
注释的bean

XML

<context:annotation-config />
<context:component-scan base-package="test" />

<bean id="testClass" class="test.TestClass"/>
LocaleProvider.java

public class TestClass {
    @Autowired
    LocaleProvider locale;

    public void test() {
        locale.getMessage("test");
    }
}
@Component
public class LocaleProvider {
    ...

    public String getMessage(String key, Object... args) throws NoSuchMessageException {
        return ...;
    }
})
@Controller
public class TestController {
    @Autowired
    TestClass test;

    @RequestMapping("/")
    public Object handle() {
        test.test();
    }
}
TestController.java

public class TestClass {
    @Autowired
    LocaleProvider locale;

    public void test() {
        locale.getMessage("test");
    }
}
@Component
public class LocaleProvider {
    ...

    public String getMessage(String key, Object... args) throws NoSuchMessageException {
        return ...;
    }
})
@Controller
public class TestController {
    @Autowired
    TestClass test;

    @RequestMapping("/")
    public Object handle() {
        test.test();
    }
}
xml bean已正确创建,但变量
locale
始终为空

Stacktrace

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
,它可以正常工作。另外,如果我用
@Component
注释
TestClass
,它也可以正常工作。我需要做注解+xml混合配置

我如何解决这个问题

环境:

  • 玻璃鱼3.1
  • 春季3.1.3
  • Spring Security 3.1.3

显示一些代码。如何在XML中定义bean以及如何在Code中设置值也请确保不要在构造函数中使用注入的字段。如果您需要在bean启动后立即调用它们,请使用
@PostConstruct
initialized@St埃芬·尼科尔:刚刚添加了一些代码@格罗夫:正如你所看到的,我没有在构造函数中使用注入字段,我认为实际上没有必要使用
@PostConstruct
。无论如何,谢谢。顺便说一句,
LocaleProvider
在另一个用
@Component
注释的bean中正常工作。您确定没有覆盖
TestController
中的autowired
测试
字段或
TestClass
中的
locale
?因为这应该可以很好地工作,否则Spring会抱怨如果它没有为依赖项找到autowire候选项