Search code examples
spring-bootaspectjaspectj-maven-plugin

How should I use @Configurable to automatically assemble beans into common objects in springboot


What I need now is to have an ordinary abstract class and its subclasses. These beans need to be injected into the abstract class. They are just ordinary objects, but have some ioc-managed beans to perform operations. I need to serialize them into redis. Save it, and then take it out of redis to deserialize, but when taken out, these beans become null. I originally wanted to manually inject them one by one, but they may have nesting problems. I can’t do that. It is very troublesome to configure beans without recursively, so I looked for a lot of solutions, I found the @Configurable annotation, but when I want to use it, there is no complete document, very difficult, need to configure aspectJ, LTW, aspectj-maven -plugin, you also need to use -javaagent:/path/to/spring-instrument.jar when restarting, I am completely confused, I don’t know how to use it, it’s too much trouble, and I can’t find a complete one available The @Configurable use case, I don't want to use -javaagent:/path/to/spring-instrument.jar at all, I found many articles similar to these, and even mentioned bytebuddy. But they are not comprehensive and can’t be used, which makes me a headache

I want to use it like this

@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;
}

Solution

  • I eventually enabled LTW through Bytebuddy

    @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);
        }
    }