Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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 为什么当我访问bean时@autowired不工作_Java_Spring_Autowired - Fatal编程技术网

Java 为什么当我访问bean时@autowired不工作

Java 为什么当我访问bean时@autowired不工作,java,spring,autowired,Java,Spring,Autowired,当我使用BeanFactory从SpringBean配置文件访问bean时,如下所示: public class Person { private String id,address; @Autowired private Customer customer; //setters & getters } 和bean配置文件 当我执行此操作时,我得到了null。BeanFactory不支持注释吗??有什么想法吗?您必须使用注释配置应用程序上下文

当我使用BeanFactory从SpringBean配置文件访问bean时,如下所示:

public class Person {
    private String id,address;
    @Autowired
    private Customer customer;
     //setters & getters
    }
和bean配置文件


当我执行此操作时,我得到了
null
。BeanFactory不支持注释吗??有什么想法吗?

您必须使用
注释配置应用程序上下文

您必须添加到yor Spring.xml以激活注释扫描。

您必须使用
AnnotationConfigApplicationContext
或 您必须添加到yor Spring.xml以激活注释扫描。

正如@jens建议的那样

您应该激活注释扫描

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

希望这有助于

正如@jens所建议的那样

您应该激活注释扫描

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


希望这有助于实现方法1:在xml中包含以下代码

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config />

    <!-- Remaining bean declaration -->
</beans>

方法1:在xml中包含以下代码

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config />

    <!-- Remaining bean declaration -->
</beans>
为什么不起作用? 在XML上下文中使用Spring时,默认情况下不会激活使用注释。这意味着将不会利用
@Autowired
@Transactional
@PostConstruct
以及您将使用的任何其他注释

我怎么修理它? 要使Spring了解注释,需要添加以下行:

<context:annotation-config />
在您的案例中不需要
。如果您使用完整的注释上下文(例如,使用
@Component
注释的类),这将非常有用。查看
之间的区别

替代溶液 同样,您也可以完全删除
@Autowired
,并将所有依赖项注入XML。有关更多详细信息,请参阅


1这包括
xmlns:context
属性(xmlns=XML名称空间)和
xsi:schemaLocation
中的两个URL为什么不起作用? 在XML上下文中使用Spring时,默认情况下不会激活使用注释。这意味着将不会利用
@Autowired
@Transactional
@PostConstruct
以及您将使用的任何其他注释

我怎么修理它? 要使Spring了解注释,需要添加以下行:

<context:annotation-config />
在您的案例中不需要
。如果您使用完整的注释上下文(例如,使用
@Component
注释的类),这将非常有用。查看
之间的区别

替代溶液 同样,您也可以完全删除
@Autowired
,并将所有依赖项注入XML。有关更多详细信息,请参阅



1这包括
xmlns:context
属性(xmlns=XML名称空间)和
xsi:schemaLocation

中的两个URL是否在dispatcher-servlet.XML文件中配置了注释支持?我不明白为什么有这么多相同的答案?虽然@jens已经回答了相同的问题,但您是否在dispatcher-servlet.xml文件中配置了注释支持?我不明白为什么会有这么多相同的答案?虽然@jens已经回答了相同的
组件扫描
,但此处不需要(没有
@组件
注释)<代码>注释配置就足够了(请参阅以了解两者之间的区别)。
此处不需要组件扫描
(没有
@组件
注释)<代码>注释配置就足够了(请参阅两者之间的区别)。我再次坚持:没有
@组件
,因此
是过度杀伤力。我再次坚持:没有
@组件
,因此
是过度杀伤力。
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config />

    <!-- Your context -->
</beans>