Spring cloud #HystrixCommand doesn't proxy the method which is invoked in CompletableFuture.supplyAsync - spring

I have one spring component bean which contains a method methodA defined by #HystrixCommand with fallbackMethod. The bean has another method methodB invokes methodA by CompletableFuture.supplyAsync(...). I expect Hystrix javanica will weave the aspect on methodA, but when I debug it, I didn't see hystrix aspect is weaved.
Here are some of the main sudo code,
TestApplication:
package com.my.own.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication(scanBasePackages = "com.my.own.test")
public class TestApplication {
public static void main(final String[] args) throws Exception {
SpringApplication.run(TestApplication.class, args);
}
}
ApplicationConfiguration:
package com.my.own.test;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
#Configuration
#EnableAspectJAutoProxy(exposeProxy = true)
#EnableConfigurationProperties
#EnableCircuitBreaker
public class ApplicationConfig {
}
AsyncConfig:
package com.my.own.test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
#Configuration
#EnableAsync
public class AsyncConfig {
#Value("${spring.threadpool.executor.core:10}")
private int corePoolSize;
#Value("${spring.threadpool.executor.max:20}")
private int maxPoolSize;
#Value("${spring.threadpool.executor.queue:1000}")
private int queueCapacity;
#Value("${spring.threadpool.executor.timeout:true}")
private boolean coreThreadTimeOut;
#Value("${spring.threadpool.executor.keepalive:30000}")
private int keepAlive;
#Value("${spring.threadpool.executor.prefix:ThreadPoolTaskExecutor}")
private String threadNamePrefix;
#Bean("taskExecutor")
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setAllowCoreThreadTimeOut(coreThreadTimeOut);
executor.setKeepAliveSeconds(keepAlive);
executor.setThreadNamePrefix(threadNamePrefix + "-");
executor.initialize();
return executor;
}
}
TestController:
package com.my.own.test.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.my.own.test.core.TestProcessor;
#RestController
public class TestController {
private static Logger logger = LoggerFactory.getLogger(TestController.class);
#Autowired
TestProcessor tester;
#RequestMapping(value = "/test", method = { RequestMethod.POST })
public void test() {
tester.methodB();
}
}
TestProcessor:
package com.my.own.test.core;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
#Component
public class TestProcessor {
#Autowired
private ThreadPoolTaskExecutor executor;
public void methodB() {
final List<CompletableFuture<String>> a = new ArrayList<>();
a.add(CompletableFuture.supplyAsync(() -> methodA(), executor));
CompletableFuture.allOf(a.toArray(new CompletableFuture[a.size()])).join();
}
#HystrixCommand(fallbackMethod = "deferPushdown")
public String methodA() {
if (true) {
throw new RuntimeException();
} else {
return "methodA";
}
}
public String deferMethodA() {
return "deferMethodA";
}
}
Run output
Jan 03, 2018 2:55:55 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.util.concurrent.CompletionException: java.lang.RuntimeException] with root cause
java.lang.RuntimeException
at com.my.own.test.core.TestProcessor.methodA(TestProcessor.java:40)
at com.my.own.test.core.TestProcessor.lambda$0(TestProcessor.java:33)
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
P.S.
From the output, methodA is not invoked from hystrix aspect, but by lambda directly. Is this an issue on Hystrix or javanica? Please share if you know a solution. I appreciate it.

Unless you’re using aspectj weaving (which requires special handling of compile steps, I think), spring defaults to using interface/cglib weaving, which only applies to the first method called from outside the class, as described in https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#aop-understanding-aop-proxies.
In summary, if you call methodB, no aspect applies and the call from methodB to methodA is not eligible for aspect interception.
To activate the aspect you have to call methodB directly from outside the TestProcessor class.

Related

Encountered error "Consider defining a bean of type 'java.util.concurrent.atomic.AtomicReference' in your configuration"

I am getting the below error while starting spring boot application.
The injection point has the following annotations:
#org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type
'java.util.concurrent.atomic.AtomicReference' in your configuration.
Below is the code .
package de.summer.sampleapplayerv1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
#SpringBootApplication(scanBasePackages = {"de.summer.sampleapplayerv1"})
#EnableConfigurationProperties
#EnableJpaRepositories (basePackages ="de.summer.sampleapplayerv1.repository")
#EnableTransactionManagement
public class Sampleapplayerv1Application {
public static void main(String[] args) {
SpringApplication.run(Sampleapplayerv1Application.class, args);
}
}
package de.summer.sampleapplayerv1.service;
import de.summer.sampleapplayerv1.domain.QueueAndPublish;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
#Slf4j
#Service
public class QueueAndPublishServiceImpl implements QueueAndPublishService{
private final AtomicReference<List<QueueAndPublish>> currentJob;
public QueueAndPublishServiceImpl(
#Qualifier("currentJob") AtomicReference<List<QueueAndPublish>> currentJob
){
this.currentJob=currentJob;
}
#Override
public QueueAndPublish getJobStatus(UUID jobId) {
return (QueueAndPublish) currentJob.get().stream()
.filter(j -> j.getJobId()==jobId)
.collect(Collectors.toList());
}
#Override
public List<QueueAndPublish> getAllJobStatus() {
return currentJob.get();
}
#Override
public QueueAndPublish getCategoryDataProcess() {
List<QueueAndPublish> processList=new ArrayList<QueueAndPublish>();
QueueAndPublish process=QueueAndPublish.builder()
.jobId(UUID.randomUUID())
.jobName("Name for Job")
.jobStatus("Not Yet Started")
.build();
Thread t1=new Thread(process.getJobId().toString()){
#Override
public void run(){
log.info("How are you doing");
process.setJobStatus("Completed");
}
};
t1.start();
processList.add(process);
currentJob.set(processList);
return process;
}
#Override
public QueueAndPublish getCatgeoryDataProcessStatus() {
return null;
}
}
package de.summer.sampleapplayerv1.domain;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.io.Serializable;
import java.util.UUID;
#Getter
#Setter
#Builder
#Entity
public class QueueAndPublish implements Serializable {
#Id
private UUID jobId;
private String jobName;
private String jobStatus;
}
If I remove the constructor, spring boot application is starting up without any errors. If included , start up is failing with unsatisfied dependency errors.
Can someone please help on what is wrong with config?
You expect Spring to create an instance of class QueueAndPublishServiceImpl for the implementation of QueueAndPublishService. This instance needs a constructor parameter of type AtomicReference<List<QueueAndPublish>> injected.
But you obviously do not define any Spring bean (Bean, Component, Service, ...) of that type.
Edit:
public QueueAndPublishServiceImpl(
#Qualifier("currentJob") AtomicReference<List<QueueAndPublish>> currentJob
){
this.currentJob=currentJob;
}
Here you define a constructor parameter to have a AtomicReference<List<QueueAndPublish>>, and even specify it with a #Qualifier. So you need to provide a Spring bean of this class with this qualifier, otherwise Spring cannot inject it into the constructor call.
Consider defining a bean of type 'java.util.concurrent.atomic.AtomicReference' in your configuration.
Means "something like" adding this to your Sampleapplayerv1Application:
#Bean("currentJob") AtomicReference<List<QueueAndPublish>> currentJob() {
// or a list implementation of your choice.
return new AtomicReference<>(new java.util.ArrayList<>());
}

Difference between Spring Boot QuartzAutoConfiguration 2.1.6.RELEASE & 2.2.2.RELEASE

We were using Spring Boot 2.1.6.RELEASE.
after that we updated spring version to 2.2.2.RELEASE. When we change the version we noticed our quartz jobs not working. We have multiple jobs and we configured them like below. After some reasearch i found some differences between in QuartzAutoConfiguration class.
How can i inject my triggers in spring 2.2.2.RELEASE. Is there any easy way? I dont want to write to many triggers and trigger details.
MyConfig
import io.rkpc.commons.util.ApplicationReflectionUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
#Slf4j
#Configuration
#ConfigurationProperties(prefix = "quartz")
#Profile("quartz")
#Data
public class JobConfig {
private List<Job> jobs;
#Bean
public JobDetail[] jobDetail() throws SchedulerConfigException {
Set<Class<QuartzJobBean>> subClasses = ApplicationReflectionUtil.getSubClasses(QuartzJobBean.class, "io.rkpc");
List<JobDetail> jobDetails = new ArrayList<>();
for (Class<QuartzJobBean> quartzJobBeanClass : subClasses) {
Job job = getJob(quartzJobBeanClass.getSimpleName());
if (job.isEnabled()) {
JobDetail jobDetail = JobBuilder.newJob(quartzJobBeanClass)
.withIdentity(quartzJobBeanClass.getSimpleName())
.storeDurably()
.build();
jobDetails.add(jobDetail);
}
}
return jobDetails.toArray(new JobDetail[0]);
}
#Bean
public Trigger[] jobATrigger(JobDetail[] jobADetails) throws SchedulerConfigException {
List<Trigger> triggers = new ArrayList<>();
for (JobDetail jobDetail : jobADetails) {
Job job = getJob(jobDetail.getKey().getName());
CronTrigger trigger = TriggerBuilder.newTrigger().forJob(jobDetail)
.withIdentity(jobDetail.getKey().getName().concat("Trigger"))
.withSchedule(CronScheduleBuilder.cronSchedule(job.getCron()))
.build();
triggers.add(trigger);
}
return triggers.toArray(new Trigger[0]);
}
private Job getJob(String name) throws SchedulerConfigException {
List<Job> filteredJobs = jobs.stream().filter(job -> job.getName().equals(name)).collect(Collectors.toList());
if (CollectionUtils.isEmpty(filteredJobs) || filteredJobs.size() > 1) {
log.error("{} is not configured", name);
throw new SchedulerConfigException("Job is not configured");
}
return filteredJobs.get(0);
}
#Data
public static class Job {
private String name;
private String cron;
private boolean enabled;
}
}
QuartzAutoConfiguration.java Spring version 2.1.6 github url ; https://github.com/spring-projects/spring-boot/blob/v2.1.6.RELEASE/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfiguration.java
QuartzAutoConfiguration.java Spring version 2.2.2 github url https://github.com/spring-projects/spring-boot/blob/v2.2.2.RELEASE/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfiguration.java
Main difference i notice is ; in 2.1.6 version Quartz AutoConfiguration was "Trigger" array but 2.2.2 doesn't have "Trigger" array.
Spring has always some magic :)
import io.rkpc.commons.util.ApplicationReflectionUtil;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.util.CollectionUtils;
import javax.annotation.PostConstruct;
import javax.validation.constraints.NotNull;
import java.text.ParseException;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
#Slf4j
#Configuration
#ConfigurationProperties(prefix = "quartz")
#Profile("quartz")
#Data
#AutoConfigureBefore({QuartzAutoConfiguration.class})
#RequiredArgsConstructor(onConstructor = #__({#Autowired, #NotNull}))
public class JobConfig {
private final List<Job> jobs;
private final DefaultListableBeanFactory beanFactory;
#PostConstruct
public void init() throws SchedulerConfigException, ParseException {
Set<Class<QuartzJobBean>> subClasses = ApplicationReflectionUtil.getSubClasses(QuartzJobBean.class, "io.rkpc");
for (Class<QuartzJobBean> quartzJobBeanClass : subClasses) {
Job job = getJob(quartzJobBeanClass.getSimpleName(), jobs);
if (job.isEnabled()) {
JobDetailImpl jobDetail = (JobDetailImpl) JobBuilder.newJob(quartzJobBeanClass)
.withIdentity(quartzJobBeanClass.getSimpleName())
.storeDurably()
.build();
CronTriggerImpl trigger = (CronTriggerImpl) TriggerBuilder.newTrigger().forJob(jobDetail)
.withIdentity(jobDetail.getKey().getName().concat("Trigger"))
.withSchedule(CronScheduleBuilder.cronSchedule(job.getCron()))
.build();
GenericBeanDefinition jobBeanDefinition = new GenericBeanDefinition();
jobBeanDefinition.setBeanClass(JobDetailImpl.class);
jobBeanDefinition.getPropertyValues().addPropertyValue("jobClass", quartzJobBeanClass);
jobBeanDefinition.getPropertyValues().addPropertyValue("key", jobDetail.getKey());
jobBeanDefinition.getPropertyValues().addPropertyValue("durability", jobDetail.isDurable());
beanFactory.registerBeanDefinition(quartzJobBeanClass.getSimpleName(), jobBeanDefinition);
GenericBeanDefinition triggerBeanDefinition = new GenericBeanDefinition();
triggerBeanDefinition.setBeanClass(CronTriggerImpl.class);
triggerBeanDefinition.getPropertyValues().addPropertyValue("jobKey", trigger.getJobKey());
triggerBeanDefinition.getPropertyValues().addPropertyValue("key", trigger.getKey());
triggerBeanDefinition.getPropertyValues().addPropertyValue("cronExpression", new CronExpression(trigger.getCronExpression()));
beanFactory.registerBeanDefinition(trigger.getName(), triggerBeanDefinition);
}
}
}
public Job getJob(String name, List<Job> jobs) throws SchedulerConfigException {
List<Job> filteredJobs = jobs.stream().filter(job -> job.getName().equals(name)).collect(Collectors.toList());
if (CollectionUtils.isEmpty(filteredJobs) || filteredJobs.size() > 1) {
log.error("{} is not configured", name);
throw new SchedulerConfigException("Job is not configured");
}
return filteredJobs.get(0);
}
#Data
public static class Job {
private String name;
private String cron;
private boolean enabled;
}
}
You are exposing a single Trigger[] bean rather than multiple Trigger beans. You should define one bean per Trigger. You should also do the same for each JobDetail. This was working by accident with Spring Boot 2.1.x as you were relying on the auto-configuration using ObjectProvider<Trigger[]>. The intent of the auto-configuration was to consume all Trigger beans, with the application context turning those beans into an array before injecting them.
If you don't want to define multiple Trigger and JobDetail beans, you may be better configuring the SchedulerFactoryBean yourself rather than relying on the auto-configuration.

Spring #Autowired null problem on weblogic 12c

Spring's #Autowired value is null. My application is deployed on weblogic 12c.
I have pasted relevant files below along with explanations.
I tried changing to #Component
DriverServlet.java: Set in web.xml. Called by weblogic. Initializes
application context.
package com.clarksburg.services.configuration;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
public class DriverServlet extends HttpServlet {
public void init() throws ServletException {
// Call the AppConfig class to scan all packages
AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
System.out.println("Driver Servlet initialized;");
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void destroy() {
// do nothing.
}
}
AppConfig.java: Does component scanning.
package com.clarksburg.services.configuration;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
#Configuration
#ComponentScan(basePackages = { "com.clarksburg.services" })
public class AppConfig {
private static final Logger logger = LogManager.getLogger(AppConfig.class.getName());
public AppConfig() {
logger.debug("App Config called by driver");
// TODO Auto-generated constructor stub
}
// Put Other Application configuration here.
}
The class 'AsynchAQMessageSvcImpl' implements 'AsynchAQMessageSvc' which was generated using WSDL. It's wired to a OSB proxy on weblogic and get's called whenever the proxy gets a JMS message on a AQ queue.
It's annotated with #Service. I tried to autowire ProcessWsPayload in it but when i use it i get a null pointer exception. Printing it's value is also null.
package com.clarksburg.services.messaging;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.clarksburg.services.wsimport.AsynchAQMessageRequestType;
import com.clarksburg.services.wsimport.AsynchAQMessageResponseType;
import com.clarksburg.services.wsimport.AsynchAQMessageSvc;
import com.clarksburg.services.services.ProcessWsPayload;
#Service
#WebService(name = "AsynchAQMessageSvc", targetNamespace = "http://www.clarksburg.com/AsynchAQMessageSvc")
#SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public class AsynchAQMessageSvcImpl implements AsynchAQMessageSvc {
#Autowired
ProcessWsPayload processWsPayload;
private static final Logger logger = LogManager.getLogger(AsynchAQMessageSvcImpl.class.getName());
#Override
#WebMethod(operationName = "GetAsynchAQMessage", action = "http://www.clarksburg.com/AsynchAQMessageSvc")
#WebResult(name = "AsynchAQResponse", targetNamespace = "http://www.clarksburg.com/AsynchAQMessageSvc", partName = "response")
public AsynchAQMessageResponseType getAsynchAQMessage(
#WebParam(name = "AsynchAQRequest", targetNamespace = "http://www.clarksburg.com/AsynchAQMessageSvc", partName = "request")
AsynchAQMessageRequestType request) {
logger.debug("processWsPayload is " +processWsPayload);
processWsPayload.processWsPayload(request);
// new ProcessWsPayload().processWsPayload(request);
AsynchAQMessageResponseType response = new AsynchAQMessageResponseType();
response.setServiceResponse("Success");
return response;
}
}
ProcessWsPayload.java: It's called (successfully) by 'AsynchAQMessageSvcImpl'. It's annotated as #Component.
package com.clarksburg.services.services;
import java.math.BigInteger;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.clarksburg.services.configuration.JDBCConfig;
#Component
public class ProcessWsPayload {
private static final Logger logger = LogManager.getLogger(ProcessWsPayload.class.getName());
#Autowired
JDBCConfig jdbcConfig;
/*
* This method is called by a JMS listener. It is being invoked successfully.
* However the value of 'jdbcConfig' is null
*/
public void processWsPayload(wsPayload request) {
BigInteger wsAuditId = request.getAuditId();
system.out.println("The value of JDBCConfig is " +jdbcConfig);
try {
String SQL = "UPDATE AUDIT_TBL SET ws_descriptions = ? WHERE ws_audit_id = ?";
JDBCConfig.jdbcTemplate().update(SQL, "Success with soap send", wsAuditId);
} catch (Exception e) {
logger.debug("Failed to update table with success");
logger.error(e);
}
}
I turned on Spring's TRACE logs and have pasted lines pertaining to Autowiring below.
Spring's relevant autowired logs during deployment:-
InjectionMetadata - Registered injected element on class [com.multiplan.services.messaging.AsynchAQMessageSvcImpl]: AutowiredFieldElement for com.multiplan.services.services.ProcessWsPayload com.multiplan.services.messaging.AsynchAQMessageSvcImpl.processWsPayload
InjectionMetadata - Processing injected element of bean 'AsynchAQMessageSvcImpl': AutowiredFieldElement for com.multiplan.services.services.ProcessWsPayload com.multiplan.services.messaging.AsynchAQMessageSvcImpl.processWsPayload
InjectionMetadata - Registered injected element on class [com.multiplan.services.services.ProcessWsPayload]: AutowiredFieldElement for com.multiplan.services.configuration.JDBCConfig com.multiplan.services.services.ProcessWsPayload.jdbcConfig
InjectionMetadata - Processing injected element of bean 'processWsPayload': AutowiredFieldElement for com.multiplan.services.configuration.JDBCConfig com.multiplan.services.services.ProcessWsPayload.jdbcConfig
AutowiredAnnotationBeanPostProcessor - Autowiring by type from bean name 'processWsPayload' to bean named 'JDBCConfig'
AutowiredAnnotationBeanPostProcessor - Autowiring by type from bean name 'AsynchAQMessageSvcImpl' to bean named 'processWsPayload'

How to create same Bean using Constructor Injection in Spring boot for different property values

I have to create multiple beans of same type for different property value which is to be injected using constructor.
Currently I have used Bean scope as Prototype & created multiple methods to read different properties to create new object. How to combine all the different methods into single method and to supply different values at run time to create new bean.
package com.demo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
#Configuration
public class ConfigClass {
#Bean(name="RC")
public JavaClient getClient(#Autowired Environment env)
{
String sName=env.getProperty("RCSName");
String aUrl=env.getProperty("RCAUrl");
String dUrl=env.getProperty("RCDUrl");
return new JavaClient(sName,aUrl,dUrl);
}
#Bean(name="O")
public JavaClient getOClient(#Autowired Environment env)
{
String sName=env.getProperty("OSName");
String aUrl=env.getProperty("OAUrl");
String dUrl=env.getProperty("ODUrl");
return new JavaClient(sName,aUrl,dUrl);
}
}
Now it is creating 2 beans as per above code. Expectation: How to combine getClient & getOClient into single method, but the property to be supplied in a loop to create multiple beans of same type JavaClient
I have modified my ConfigClass as below & created ApplicationContextAware to inject beans by reading file properties.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.annotation.EnableScheduling;
#Configuration
#EnableScheduling
public class ConfigClass {
#Bean
#Scope("prototype")
public JavaClient getClient(String sName,String aUrl,String dUrl)
{
return new JavaClient(sName,aUrl,dUrl);
}
}
Then Have created ApplicationContextAware to create Beans.
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Iterator;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
#Component
public class ApplicationContextProvider implements ApplicationContextAware {
private ApplicationContext applicationContext;
#Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public ApplicationContext getContext() {
return applicationContext;
}
#PostConstruct
public void init()
{
try {
String fileName = "Url.txt";
Resource resource = new ClassPathResource(fileName);
File file = resource.getFile();
List<String> lines = Files.readAllLines(file.toPath());
for (Iterator<String> iterator = lines.iterator(); iterator.hasNext();) {
String line = (String) iterator.next();
String[] s = line.split(",");
applicationContext.getBean(JavaClient.class,s[0], s[1], s[2]);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

Overriding Bean Configuration with annotations

Use case: I have my container configured via classpath scanning #ComponentScan. For my test configuration I need the ability to mock specific beans.
Due to the order of loading, beans loaded via classpath scan are not overriding properly when using #Configuration. The following code samples demonstrate the problem. BaseExample.java shows how it is possible to override beans via configuration. ScanExample.java shows that overriding a bean that was loaded via #ComponentScan is skipped (see final note).
A demo project is available on bitbucket.
// BaseExample.java
package com.glassworks.mock;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.common.base.Joiner;
public class BaseExample {
private static final Logger log = LoggerFactory.getLogger(BaseExample.class);
private static AnnotationConfigApplicationContext ctx;
public static void main(String args[]) {
ctx = new AnnotationConfigApplicationContext(Config.class, OverrideConfig.class);
String beans[] = ctx.getBeanDefinitionNames();
log.info("{} beans found: {}", beans.length, Joiner.on(",").join(beans));
for(String bean : beans) {
log.info("{}: {}", bean, ctx.getBean(bean));
}
}
#Configuration
public static class Config {
#Bean
public AccountDao accountDao() {
log.debug("Creating accountDao [Config]");
return new AccountDao();
}
}
#Configuration
public static class OverrideConfig {
#Bean
public Object accountDao() {
log.debug("Creating accountDao [OverrideConfig]");
return Mockito.mock(AccountDao.class);
}
}
}
Output:
21:05 INFO | com.glassworks.mock.BaseExample | accountDao: Mock for AccountDao, hashCode: 666537607
[// ScanExample.java
package com.glassworks.mock;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.google.common.base.Joiner;
public class ScanExample {
private static final Logger log = LoggerFactory.getLogger(ScanExample.class);
private static AnnotationConfigApplicationContext ctx;
public static void main(String args[]) {
ctx = new AnnotationConfigApplicationContext(Config.class, OverrideConfig.class);
String beans[] = ctx.getBeanDefinitionNames();
log.info("{} beans found: {}", beans.length, Joiner.on(",").join(beans));
for(String bean : beans) {
log.info("{}: {}", bean, ctx.getBean(bean));
}
}
#Configuration
#ComponentScan("com.glassworks.services")
public static class Config {
}
#Configuration
public static class OverrideConfig {
#Bean
public AccountDao accountDao() {
log.debug("Creating accountDao [OverrideConfig]");
return Mockito.mock(AccountDao.class);
}
}
}
Output:
21:08 INFO | com.glassworks.mock.ScanExample | accountDao: com.glassworks.services.AccountDao#48805ebb
// AccountDao.java
package com.glassworks.services;
import org.springframework.stereotype.Repository;
#Repository
public class AccountDao {
}
Note
Its worth noting on that with logging set to debug, Spring indicates that it is skipping over the definition. This appears to be a bug.
21:09 DEBUG | o.s.c.a.ConfigurationClassBeanDefinitionReader | Skipping loading bean definition for [BeanMethod:name=accountDao,declaringClass=com.glassworks.mock.ScanExample$OverrideConfig]: a definition for bean 'accountDao' already exists. This is likely due to an override in XML.

Resources