Spring,您可以在抽象类中自动布线吗? [英] Spring can you autowire inside an abstract class?

查看:69
本文介绍了Spring,您可以在抽象类中自动布线吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring无法自动连接对象?是否可以在抽象类中自动装配对象。假设所有模式都在application-context.xml中提供。

Spring is failing to autowire my object? Is it possible to autowire an object within an abstract class. Assume all schemas are supplied in application-context.xml

问题:@Service @Component应该在基类和扩展类(如果有)上使用什么批注?

Question: What annotation should be on the base and extending classes (if any) @Service @Component?

示例

abstract class SuperMan {

    @Autowire
    private DatabaseService databaseService;

    abstract void Fly();

    protected void doSuperPowerAction(Thing thing) {

        //busy code

        databaseService.save(thing);

    }
}

扩展课程 strong>

public class SuperGirl extends SuperMan {

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

    public doSomethingSuperGirlDoes() {

        //busy code

        doSuperPowerAction(thing)

    }

application-context.xml

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


推荐答案

通常,Spring应该自动布线,只要您的抽象类在用于组件扫描的基本程序包中。

Normally, Spring should do the autowiring, as long as your abstract class is in the base-package provided for component scan.

请参见以供进一步参考。

See this and this for further reference.

@Service @Component 都是创建带注释的bean的构造型在Spring容器中输入。如Spring Docs所述,

@Service and @Component are both stereotypes that creates beans of the annotated type inside the Spring container. As Spring Docs state,


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

This annotation serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning.

这篇关于Spring,您可以在抽象类中自动布线吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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