1

Hi I'm trying to modify a Confluence Plugin to check every Network request against the OutboundWhitelist, however the plugin is written in a way where adding it into the constructor breaks quite a few things and causes quite a headache. I wanted to know if it is possible to somehow do this in a different way.

I tried using @ComponentImport on the field itself and tried other things like @Inject and @Autowired. However with all of these the OutboundWhitelist was null and the resulting code did not work.

Did I miss something or is adding @ComponentImport to the Constructor really the only way to achieve this behaviour?

Any help is appreciated. Edit: Added Code Samples.

This is what I'd like to avoid

private final OutboundWhitelist outboundWhitelist;


public YourMacro(@ComponentImport OutboundWhitelist outboundWhitelist) {
   this.outboundWhitelist = outboundWhitelist;
}

This is what I'd like to do:

public void setOutboundWhitelist(OutboundWhitelist ow) {  
    this.outboundWhitelist = ow;  
}  

2 Answers2

0

If OutboundWhitelist is a bean, you can use ComponentAccessor to get it directly in the code depending on the environment:

OutboundWhitelist list=ComponentAccessor.getComponent(OutboundWhitelist.class)
Andrii Maliuta
  • 331
  • 2
  • 7
0

Make sure that your OutboundWhitelist instance is defined a bean first: In any class annotated as @Configuration add this

@Bean
public OutboundWhitelist outboundWhitelist() {
  return new OutboundWhitelist() //Or any other code that creates and returns the instance of OutboundWhitelist
}

Than in any class (such as your class YourMacro) where you need that bean just simply do this:

@Resource
private OutboundWhitelist outboundWhitelist;

Of course, your YourMacro class should be marked as @Component or any annotation that includes @Component, such as @Service @Controller etc

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36