Spring可以在抽象类中自动连接吗?

Spring可以在抽象类中自动连接吗?,spring,abstract-class,autowired,Spring,Abstract Class,Autowired,Spring无法自动关联我的对象?是否可以在抽象类中自动关联对象。假设所有架构都在application-context.xml中提供 问题:在@Service@Component的基础类和扩展类(如果有)上应该有什么注释 示例 abstract class SuperMan { @Autowire private DatabaseService databaseService; abstract void Fly(); protected void doS

Spring无法自动关联我的对象?是否可以在抽象类中自动关联对象。假设所有架构都在application-context.xml中提供

问题:在@Service@Component的基础类和扩展类(如果有)上应该有什么注释

示例

abstract class SuperMan {

    @Autowire
    private DatabaseService databaseService;

    abstract void Fly();

    protected void doSuperPowerAction(Thing thing) {

        //busy code

        databaseService.save(thing);

    }
}
扩展类

public class SuperGirl extends SuperMan {

    @Override
    public void Fly() {
        //busy code
    }

    public doSomethingSuperGirlDoes() {

        //busy code

        doSuperPowerAction(thing)

    }
应用程序上下文.xml

<context:component-scan base-package="com.baseLocation" />
<context:annotation-config/>

通常,只要抽象类位于为组件扫描提供的基本包中,Spring就应该进行自动连接

有关更多参考,请参阅和

@Service
@Component
都是在Spring容器中创建带注释类型bean的原型。正如Spring博士所说

此注释用作@Component的专门化,允许 要通过类路径扫描自动检测的实现类


我有那种弹簧装置在工作

具有自动连接字段的抽象类

public abstract class AbstractJobRoute extends RouteBuilder {

    @Autowired
    private GlobalSettingsService settingsService;

在我的例子中,在Spring4应用程序中,我必须使用一个经典的抽象工厂模式(我的想法来源于-)来创建实例,每次都要执行一个操作。因此,我的代码设计如下:

public abstract class EO {
    @Autowired
    protected SmsNotificationService smsNotificationService;
    @Autowired
    protected SendEmailService sendEmailService;
    ...
    protected abstract void executeOperation(GenericMessage gMessage);
}

public final class OperationsExecutor {
    public enum OperationsType {
        ENROLL, CAMPAIGN
    }

    private OperationsExecutor() {
    }

    public static Object delegateOperation(OperationsType type, Object obj) 
    {
        switch(type) {
            case ENROLL:
                if (obj == null) {
                    return new EnrollOperation();
                }
                return EnrollOperation.validateRequestParams(obj);
            case CAMPAIGN:
                if (obj == null) {
                    return new CampaignOperation();
                }
                return CampaignOperation.validateRequestParams(obj);
            default:
                throw new IllegalArgumentException("OperationsType not supported.");
        }
    }
}

@Configurable(dependencyCheck = true)
public class CampaignOperation extends EO {
    @Override
    public void executeOperation(GenericMessage genericMessage) {
        LOGGER.info("This is CAMPAIGN Operation: " + genericMessage);
    }
}
最初,为了在抽象类中注入依赖项,我尝试了所有构造型注释,如@Component、@Service等,但即使Spring上下文文件中有ComponentScanning来扫描整个包,但不知何故,在创建子类实例(如campativeoperation)时,超级抽象类EO的属性为null,因为spring无法识别和注入其依赖项。经过多次尝试和错误后,我使用了此
**@Configurable(dependencyCheck=true)**
annotation,最后Spring能够注入依赖项,并且我能够使用子类中的属性,而不会让它们与太多属性混淆

<context:annotation-config />
<context:component-scan base-package="com.xyz" />

我还尝试了其他参考资料以找到解决方案:


  • 请尝试使用
    ***可配置(dependencyCheck=true)**
    并更新此帖子,如果您遇到任何问题,我可能会尝试帮助您。

    如果您需要在
    SuperGirl
    中执行任何数据库操作,您将再次将其注入
    SuperGirl

    我认为主要思想是在不同的类中使用相同的对象引用。 那么这个呢:

    //There is no annotation about Spring in the abstract part.
    abstract class SuperMan {
    
    
        private final DatabaseService databaseService;
    
        public SuperMan(DatabaseService databaseService) {
         this.databaseService = databaseService;
        }
    
        abstract void Fly();
    
        protected void doSuperPowerAction(Thing thing) {
    
            //busy code
    
            databaseService.save(thing);
    
        }
    }
    


    在我看来,InjectOnce到处运行:)

    可能重复的虽然帖子看起来很旧,但我想指出的是自动连线中有一个打字错误,它应该是@Autowired而不是抽象类Superman。如果
    设置服务
    设置为
    受保护
    ,孩子们能用吗?@OrtomalaLokni是的,他们能。Java访问修饰符不会被自动连线更改。不确定是否使用了类似以下内容:`ApplicationContextProvider.getApplicationContext().getAutowireCapableBeanFactory().autowireBean(this);`可以帮助您在非您创建的实例上自动连接bean。对于工厂方法,我会自动连接
    BeanFactory
    ,并像这样调用
    BeanFactory.getBean(EnrollOperation.class)
    。这样,如果EnrollOperation类具有自动连接的注释,它将自动为您注入注释。仅供参考,在枚举开关情况下,您的默认情况将永远不会被调用:)您还应检查类型==null,它不是默认情况
    @Component
    public class SuperGirl extends SuperMan {
    
    private final DatabaseService databaseService;
    
    @Autowired
    public SuperGirl (DatabaseService databaseService) {
         super(databaseService);
         this.databaseService = databaseService;
        }
    
    @Override
    public void Fly() {
        //busy code
    }
    
    public doSomethingSuperGirlDoes() {
    
        //busy code
    
        doSuperPowerAction(thing)
    
    }