我应该如何使用@Configable在SpringBoot中自动将Bean组装成通用对象 [英] How should I use @Configurable to automatically assemble beans into common objects in springboot

查看:20
本文介绍了我应该如何使用@Configable在SpringBoot中自动将Bean组装成通用对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在需要的是拥有一个普通的抽象类及其子类。这些Bean需要注入到抽象类中。它们只是普通对象,但有一些由IoC管理的Bean来执行操作。我需要把它们序列化为红色。保存它,然后从redis中取出它以进行反序列化,但是当取出时,这些Bean就变成空的。我原本想手动逐一注射,但它们可能会出现嵌套问题。我不能那么做。不递归地配置Bean非常麻烦,所以我找了很多解决方案,我找到了@Configurable注释,但是当我想使用它的时候,没有完整的文档,很难,需要配置aspectJLTWaspectj-maven -plugin,重启的时候也需要使用-javaagent:/path/to/spring-instrument.jar,我完全搞不懂了,不知道怎么用,太麻烦了,而且我找不到一个完整的@Configurable用例,我根本不想用-javaagent:/path/to/spring-instrument.jar,我找到了很多类似的文章,甚至提到了bytebuddy。但它们不全面,不能使用,这让我很头疼

我想这样使用

@Data
@NoArgsConstructor
@Slf4j
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
@JsonSubTypes(
        value = {
            @JsonSubTypes.Type(value = SinglePipeLink.class),
            @JsonSubTypes.Type(value = SequencePipeLink.class),
            @JsonSubTypes.Type(value = SplitFlowPipeLink.class),
            @JsonSubTypes.Type(LoopPipeLink.class)
        })
@Accessors(chain = true)
@Configurable(autowire = Autowire.BY_NAME, preConstruction = true)
public abstract class BasePipeLink implements Serializable {
    private static final long serialVersionUID = -5308571029549664750L;
    protected String copyFrom;

    @Resource @JsonIgnore protected transient RedisService redisService;
}

推荐答案

我最终通过字节伙伴启用了LTW

@EnableSpringConfigured
@EnableAspectJAutoProxy
@EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.ENABLED)
public class Application {
    public static void main(String[] args) {
        // Get access to an instrumentation instance
        Instrumentation instrumentation = ByteBuddyAgent.install();
        // Activate AspectJ weaver
        Agent.agentmain("", instrumentation);
        // Save instrumentation instance for later use
        InstrumentationSavingAgent.agentmain("", instrumentation);
        SpringApplication.run(Application.class, args);
    }
}

这篇关于我应该如何使用@Configable在SpringBoot中自动将Bean组装成通用对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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