Last active
September 26, 2017 16:40
-
-
Save OsaSoft/9832ab583dfee2576d01bf08ec14546e to your computer and use it in GitHub Desktop.
Spring Autowiring in instances that are not managed Spring beans
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InjectReadyClass { | |
@Autowired | |
public MyCoolService myCoolService; //public just for showing purposes | |
InjectReadyClass(ApplicationContext ctx) { | |
ctx.getAutowireCapableBeanFactory().autowireBeanProperties( | |
this, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Service | |
class SomeService { | |
@Autowired | |
ApplicationContext ctx; | |
void someServiceMethod() { | |
... | |
InjectReadyClass injectReadyInstance = new InjectReadyClass(ctx); | |
assert injectReadyIntance.myCoolService != null //just to show it gets injected properly | |
... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment