2

我有一个带有@Autowired 依赖项的抽象类“Command”和扩展抽象类的类。没有注入依赖项。抽象类和具体类使用@Component 注释并正在被扫描。似乎基础(抽象)类不是弹簧管理的。需要做什么才能做到这一点?是否有注释将其定义为抽象?我不想在 XML 中定义 bean。

public abstract class Command {
  @Autowired
  private SecurityUtils securityUtils;
....

@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
@Component
public class NoteCommand extends Command {
...
}

我的错误我道歉。命令类被注入到我的控制器中,其中一个(NoteCommand)是通过“new”手动实例化的。一切都很好。

4

2 回答 2

-1

这可以通过 XML 配置来实现(不确定注释)。阅读此http://docs.spring.io/spring-framework/docs/3.0.0.RC3/reference/html/ch03s07.html

试试这个(将其他配置添加到子 bean?)

<bean id = "command" class = "some.package.name.Command" abstract = "true">
  <property name = "securityUtils" ref = "securityUtils"/>
</bean>

<bean id ="noteCommand" class = "some.package.name.NoteCommand" parent="commadn">

</bean>

干杯!

于 2013-10-31T19:54:13.670 回答
-1

在我的情况下,在 Spring4 应用程序中,我必须使用经典的抽象工厂模式(我的想法来自 - http://java-design-patterns.com/patterns/abstract-factory/)来创建每个实例并且每次都有操作要做。所以我的代码设计如下:

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,但不知何故,在创建像 CampaignOperation 这样的子类实例时,超级抽象类 EO 是由于spring无法识别和注入其依赖项,因此其属性为null。经过多次试验和错误,我使用了这个**@Configurable(dependencyCheck = true)**注释,最后Spring能够注入依赖项,并且我能够使用子类中的属性而不会弄乱它们很多属性。

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

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

  1. http://www.captaindebug.com/2011/06/implementing-springs-factorybean.html#.WqF5pJPwaAN
  2. http://forum.spring.io/forum/spring-projects/container/46815-problem-with-autowired-in-abstract-class
  3. https://github.com/cavallefano/Abstract-Factory-Pattern-Spring-Annotation
  4. http://www.jcombat.com/spring/factory-implementation-using-servicelocatorfactorybean-in-spring
  5. https://www.madbit.org/blog/programming/1074/1074/#stash.XEJXdIR5.dpbs
  6. 在 Spring 框架中使用抽象工厂
  7. Spring自动装配不适用于抽象类
  8. 在抽象超类中注入spring依赖
  9. Spring 和 Abstract 类 - 在抽象类中注入属性
    1. Spring 你可以在抽象类中自动装配吗?

请尝试使用**@Configurable(dependencyCheck = true)**和更新这篇文章,如果您遇到任何问题,我可能会尝试帮助您。

于 2018-03-09T23:21:24.220 回答