Created
September 10, 2019 08:07
-
-
Save Mukundhan-I2I/c114c91c89820fcb68d819d4b8172b84 to your computer and use it in GitHub Desktop.
StaticContextAccessor
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
package com.ntst.services.mobile.util; | |
import javax.annotation.PostConstruct; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.stereotype.Component; | |
/** | |
* <p> | |
* StaticContextAccessor for accessing Autowired Component instances inside | |
* a static method directly. | |
* </p> | |
* <p> | |
* StaticContextAccessor.getBean(SyncConfiguration.class); | |
* </p> | |
* @author mukundhan | |
* @created Aug-30-2019 | |
*/ | |
@Component | |
public class StaticContextAccessor { | |
private static StaticContextAccessor instance; | |
@Autowired | |
private ApplicationContext applicationContext; | |
@PostConstruct | |
public void registerInstance() { | |
instance = this; | |
} | |
public static <T> T getBean(Class<T> clazz) { | |
return instance.applicationContext.getBean(clazz); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment