如果删除了组件扫描,则@Autowired不起作用 [英] @Autowired doesn't work if component-scan removed

查看:117
本文介绍了如果删除了组件扫描,则@Autowired不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临一个问题,如果我从config中删除了组件扫描标签,则@Autowired注释将不再起作用(在所有使用该注释的Java类中)

I'm facing the problem, that the annotation @Autowired doesn't work anymore (in all Java classes that uses this annotation) if I remove the component-scan tag from config

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.xsd">

  <context:component-scan base-package="efco.auth" />

here are some beans...

efco.auth包中只有一个类,该类与以下类EfcoBasketLogic没有关系.

There is only one class in the efco.auth package, and this one has no relation to the following class EfcoBasketLogic.

和使用@Autowired的类:

and a class that uses @Autowired:

package efco.logic;
    public class EfcoBasketLogic extends BasketLogicImpl {

        @Autowired
        private EfcoErpService erpService;

此Bean是在另一个spring配置文件中定义的:

This Bean is defined in an other spring config file:

<bean id="BasketLogic" class="efco.logic.EfcoBasketLogic">
    <property name="documentLogic" ref="DocumentLogic" />
    <property name="stateAccess" ref="StateAccess" />
    <property name="contextAccess" ref="ContextAccess" />
  </bean>

如您所见,未定义erpService.其他三个属性在BasketLogicImpl上并具有设置器.

As you can see, erpService is not defined. The other three properties are on BasketLogicImpl and have setters.

我做错了什么?

推荐答案

正如Tomasz所说,您需要<context:annotation-config/>才能使@Autowired正常工作.当您拥有<context:component-scan/>时,它会隐含为您提供annotation-config.

As Tomasz says, you need <context:annotation-config/> for @Autowired to work. When you had <context:component-scan/>, it implicitly included annotation-config for you.

这篇关于如果删除了组件扫描,则@Autowired不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆