FusionAuthClient singleton - java

I want to implement FusionAuth in a microservices enviroment with spring boot, so i want to make a singleton of the FusionAuth Client for java. But i get error for using the variables apiKey, baseUrl in a static context.
#Configuration
public class FusionAuthClientConfig {
private static FusionAuthClient INSTANCE = null;
#Value("${fusionAuth.apiKey}")
private String apiKey;
#Value("${fusionAuth.baseUrl}")
private String baseUrl;
public static FusionAuthClient getInstance() {
if(INSTANCE == null)
INSTANCE = new FusionAuthClient(apiKey, baseUrl);
return INSTANCE;
}
}
Anyway, is this scope of singleton class used? I mean for concurrency enviroments and performance, should i use a client for each request to fusion auth?

Your private static FusionAuthClient INSTANCE = null is unnecessary. By default, beans are scoped as singletons. #see: Bean Scopes
Since you're using #Configuration, all you need to do is change your FusionAuthClientConfigto the following and you will be able to reference it elsewhere in your application as an #Autowired property.
#Configuration
public class FusionAuthClientConfig {
#Value("${fusionAuth.apiKey}")
private String apiKey;
#Value("${fusionAuth.baseUrl}")
private String baseUrl;
#Bean
public FusionAuthClient fusionAuthClient() {
return new FusionAuthClient(apiKey, baseUrl);
}
}
Now your fusionAuthClient bean can be referenced elsewhere like this:
#Service
//or #Component etc
public class MyService {
private final FusionAuthClient fusionAuthClient;
public MyService(FusionAuthClient fusionAuthClient) {
this.fusionAuthClient = fusionAuthClient;
}
public void doTheThing() {
// use this.fusionAuthClient
}
}

Related

Static method to return a singleton spring bean

Trying to build a task executor app in spring boot. The idea is to design a template to retrieve the default TaskConfig so that executor can just execute it.
#Component
public class TaskExecutor {
private final TaskTemplate taskTemplate;
#Autowired
public TaskExecutor(TaskTemplate taskTemplate) {
this.taskTemplate=taskTemplate;
}
public void runTask() {
final TaskConfiguration taskConfig = taskTemplate.getTaskConfig("taskName");
taskConfig.do();
}
}
#Component
public class TaskTemplate {
private final TaskParam1 taskParam1;
private final TaskParam2 taskParam2;
#Autowired
public TaskTemplate(TaskParam1 taskParam1, TaskParam2 taskParam2) {
this.taskParam1 = taskParam1;
this.taskParam2 = taskParam2;
}
public TaskConfiguration getTaskConfig() {
// Logic to build the task configuration from task template params
}
}
The problem I see is that the TaskTemplate is coupled with the TaskExecutor (Autowired), which I wish to remove.
I wanted to replace it with a static convenient method to return the singleton Template so that I could execute the getTaskConfig with it.
Looking for suggestion to improve upon this.
Thanks
You can inject ApplicationContext to another bean like below code. After spring initialized you can use BeanGetter.getTaskTemplate() to get TaskTemplate singleton bean.
...
final TaskConfiguration taskConfig = BeanGetter.getTaskTemplate().getTaskConfig("taskName");
...
#Service
public class BeanGetter {
private static ApplicationContext applicationContext;
#Autowired
public BeanGetter(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public static TaskTemplate getTaskTemplate(){
return applicationContext.getBean(TaskTemplate.class);
}
}

Spring Boot create instance of class passing in parameters to constructor

In Spring Boot I have a class that I want to instantiate by passing parameters in the the constructor in runtime. I am able to do this but all of the AutoWire properties are null and PostConstruct doesn't get called.
Constructor<KafkaController> constructorsA[] = (Constructor<KafkaController>[]) KafkaController.class.getConstructors();
KafkaController kafkaObject = constructorsA[0].newInstance(new Object[] { "1", "2" });
This is the class in question
#Component
public class KafkaController {
private KafkaConsumer<String, String> consumer;
#Autowired
private Util sentinelUtil;
final String subscriberID;
final String interactionID;
#Autowired
public KafkaController(#Value("") String subscriberID, #Value("") String interactionID) {
this.subscriberID = subscriberID;
this.interactionID = interactionID;
}
#PostConstruct
private void initKafka() {
}
}
Do I have to instantiate the class using a different method?

Inject value from properties in Spring Boot

I have a Rest Controller in which I initialise a service like this :
class Config {
#Value(${"number.of.books"})
private final static String numberOfBooks;
}
class MyController {
private final Service myService = new ServiceImplementation(Config.numberOfBooks)
public ResponseEntity methodA() { ... }
}
The numberOfBooks field has a initialisation value but when it's passed in the ServiceImplementation constructor it comes null.
I'm thinking I'm missing something obvious over here.
What is the mistake and which would be the best practice to inject a value from a property file into a constructor?
I recommend you to directly inject numberOfBooks in your ServiceImplementation, as follows:
public class ServiceImplementation implements Service {
#Value("${number.of.books}")
private String numberOfBooks;
}
Otherwise use setter injection for static variables, as follows:
#Component
class Config {
public static String numberOfBooks;
#Value("${number.of.books}")
public void setNumberOfBooks(String numberOfBooks) {
numberOfBooks = numberOfBooks;
}
}
After studying a little I've found out that the dependency injection happens after the constructor has been called. This being said the approach used was to use Autowired on my services constructor.
class ServiceImplementation implements Service {
private final String numberOfBooks;
#Autowired
private ServiceImplementation(Config config) {
this.numberOfBooks = config.getNumberOfBooks();
}
}
In this way Spring creates the dependency tree and makes sure that Config is not null when injected.

autowired component is null

I'm developing a web application with spring. I've had no problem autowiring and using database #Service classes. Now I'm trying to read a global property file and provide the values to all classes that need them. The solution I've come up with so far seem to be overly complicated (too many classes - AppConfig, ServerConfig iface, ElasticServerConfig) for such a trivial task but I could live with it if it worked.
my applicationContext.xml contains
<context:component-scan base-package="my.package" />
AppConfig.java:
package my.package.configuration;
#Configuration
#PropertySource("classpath:application.properties")
public class AppConfig {
}
ServerConfig.java:
public interface ServerConfig {
String getUrl();
String getUser();
String getPassword();
}
ElasticSearchConfig.java:
package my.package.configuration;
#Component(value = "elasticServerConfig")
public class ElasticServerConfig implements ServerConfig {
private static final Logger LOGGER = LogManager.getLogger(ElasticServerConfig.class);
private String url;
private String user;
private String password;
#Autowired
public ElasticServerConfig(final Environment env) {
this.url = env.getProperty("elastic_server.url");
this.user = env.getProperty("elastic_server.user");
this.password = env.getProperty("elastic_server.password");
LOGGER.debug("url=" + url + "; user=" + user + "; password=" + password); // this works!
}
#Override
public final String getUrl() {
return url;
}
#Override
public final String getUser() {
return user;
}
#Override
public final String getPassword() {
return password;
}
}
When the web application boots, the ElasticServerConfig constructor prints out the correct url/user/pwd as read from application.properties. However an instance of ElasticServerConfig is not injected into a Search object:
package my.package.util;
public class Search {
#Autowired
#Qualifier("elasticServerConfig")
private ServerConfig elasticServerConfig;
public final List<Foobar> findByPatternAndLocation() {
if (elasticServerConfig == null) {
LOGGER.error("elasticServerConfig is null!");
}
// and i get a NullPointerException further on
// snip
}
}
You have to register the Search class as a Spring Bean and take it from the Spring context when you want to use it. It's important to get the bean from the spring context. If you create an object of that class with new, Spring has no way to know about that class and mange it's dependencies.
You can get get a bean from the Spring context by #Autowire it somewhere or by accessing an instance of the context and use the getBean method:
#Configuration
#PropertySource("classpath:application.properties")
public class AppConfig {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(AppConfig.class, args);
ctx.getBean...
}
}
Either use #Component annotation on the class and make sure that the class is in package thats under my.package
or register it in the configuration class
#Configuration
#PropertySource("classpath:application.properties")
public class AppConfig {
#Bean
public Search search(){
return new Search();
}
}

How to use Singleton object in Spring?

I am newbie to Spring Framework.I have tried following example in spring.
#Path("/XZY")
#Service
#Transactional
public class XZY {
#Autowired
SampleDAO sampleDao;
#Autowired
TestDAO testDao;
#Autowired
XZYinterface xzyinterface;
#POST
#Produces("text/plain")
#Path("/checkservice")
public Response XZYservice(#FormParam("Code") String Code,
#FormParam("source") String source,
#FormParam("value") String value) {
//return xzyinterface.checkXYZService(Code,sourceName,source);
XZYinterface xyz = ServiceFactory.getXZY(999);
return xyz.checkXYZService(Code,sourceName,source);
}
}
The following code will use to create singleton object
public class Singleton {
private static sampleA sampleClassA=null;
private static SampleB sampleClassB=null;
public static XZYAbstract getXZY(long id){
if(id == 999){
if(sampleClass == null){
sampleClassA = new sampleA();
}
return sampleClass;
}
if(id == 9999){
sampleClassB = new sampleA();
}
return sampleClassB;
}
}
Interface
public interface XZYinterface {
Response XZYservice(String Code, String source,String value)
}
Abstract class and implements Interface
public class XZYAbstract implements XZYinterface {
public XZYAbstract(){
super();
}
#Autowired
SampleDAO sampleDao;
#Autowired
TestDAO testDao;
public Response checkXYZService(String Code,String source,String value){
String sample = sampleDao.getValue(code);
//..source code
}
}
The following class extends abstract class.
public class sampleA extends XZYAbstract {
//some methods.
}
If i run the application it throws following errors
SEVERE [com.sun.jersey.spi.container.ContainerResponse] The RuntimeException could not be mapped to a response, re-throwing to the HTTP container: java.lang.NullPointerException
at com.test.xyz.XZYAbstract.checkXYZService(XZYAbstract.java:112) [:]
at com.test.XYZ.XZYservice(XZY.java:140) [:]
If i call directly without singleton object, values are initialized properly using Auto wired (//return xzyinterface.checkXYZService(Code,sourceName,source);) and it's working fine.
Throw from singleton object, values(sampleDAo,testDao) are not initialized properly.
How to resolve this error?
The reason is quite trivial: it's because Spring is just a library, and not a change to the Java language. Spring doesn't instrument nor enhance constructors, so the only way to get initialized Spring bean is to get it from the Spring context.
If you call new Bean(), you becomes Bean instance untouched by Spring.
For the question how to use singleton bean: do nothing. Spring beans are Singletons by default. You can specify other scope via #org.springframework.beans.factory.config.Scope annotation. See for example #Scope("prototype") bean scope not creating new bean, how it works.

Categories

Resources