How to Autowired in ConversionService in springboot - java

Trying to access the ConversionControl in model in springboot, no luck.
#Component
public class CityHelperService {
#Autowired
ConversionService conversionService;// = ConversionServiceFactory.registerConverters();
public City toEntity(CityDTO dto){
City entity = conversionService.convert(dto, City.class);
return entity;
}
public CityDTO toDTO(City entity){
CityDTO dto = conversionService.convert(entity, CityDTO.class);
return dto;
}
}
It shows the following error:
Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.lumiin.mytalk.model.CityModel com.lumiin.mytalk.controllers.CityController.cityModel;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cityModel' defined in file : Unsatisfied dependency expressed through constructor argument with index 1 of type [com.lumiin.mytalk.dao.CityHelperService]: : Error creating bean with name 'cityHelperService': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.core.convert.ConversionService com.lumiin.mytalk.dao.CityHelperService.conversionService;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.core.convert.ConversionService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)};
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cityHelperService': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.core.convert.ConversionService com.lumiin.mytalk.dao.CityHelperService.conversionService;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.core.convert.ConversionService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}

Apparently there is no ConversionService bean available, judging by the last nested exception:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.core.convert.ConversionService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
A look into the Spring documentation reveals, that you should declare a ConversionService bean. In the XML configuration it would look like this:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="example.MyCustomConverter"/>
</set>
</property>
</bean>
And since you're using Spring Boot, I assume you are creating the context programatically, so you should create a method annotated with #Bean, which returns a ConverstionService, like this (explained here):
#Bean(name="conversionService")
public ConversionService getConversionService() {
ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();
bean.setConverters(...); //add converters
bean.afterPropertiesSet();
return bean.getObject();
}

Not totally agreed with the accepted answers, because there would be a default ConverstionService named mvcConversionService so you would get duplicate bean exception. Instead addConverter to FormatterRegistry, here is the link for the part answer:
Java Config equivalent for conversionService / FormattingConversionServiceFactoryBean
Also you would need (in some cases) to define to at least an empty Component for ConversionService, something like below:
#Component #Primary
public class MyConversionService extends DefaultConversionService implements ConversionService {
// an empty ConversionService to initiate call to register converters
}
This is to force spring container to initiate a call to:
class WebMvcConfigurerAdapter {
...
public void addFormatters(FormatterRegistry registry) {
//registry.addConverter(...);
}
}

Existing answers didn't work for me:
Customizing via WebMvcConfigurerAdapter.addFormatters (or simply annotating the converter with #Component) only works in the WebMvc context and I want my custom converter to be available everywhere, including #Value injections on any bean.
Defining a ConversionService bean (via ConversionServiceFactoryBean #Bean or #Component) causes Spring Boot to replace the default ApplicationConversionService on the SpringApplication bean factory with the custom bean you've defined, which will probably be based on DefaultConversionService (in AbstractApplicationContext.finishBeanFactoryInitialization). The problem is that Spring Boot adds some handy converters such as StringToDurationConverter to the standard set in DefaultConversionService, so by replacing it you lose those conversions. This may not be an issue for you if you don't use them, but it means that solution won't work for everyone.
I created the following #Configuration class which did the trick for me. It basically adds custom converters to the ConversionService instance used by Environment (which is then passed on to BeanFactory). This maintains as much backwards compatibility as possible while still adding your custom converter into the conversion services in use.
#Configuration
public class ConversionServiceConfiguration {
#Autowired
private ConfigurableEnvironment environment;
#PostConstruct
public void addCustomConverters() {
ConfigurableConversionService conversionService = environment.getConversionService();
conversionService.addConverter(new MyCustomConverter());
}
}
Obviously you can autowire a list of custom converters into this configuration class and loop over them to add them to the conversion service instead of the hard-coded way of doing it above, if you want the process to be more automatic.
To make sure this configuration class gets run before any beans are instantiated that might require the converter to have been added to the ConversionService, add it as a primary source in your spring application's run() call:
#SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(new Class<?>[] { MySpringBootApplication.class, ConversionServiceConfiguration.class }, args);
}
}
If you don't do this, it might work, or not, depending on the order in which your classes end up in the Spring Boot JAR, which determines the order in which they are scanned. (I found this out the hard way: it worked when compiling locally with an Oracle JDK, but not on our CI server which was using a Azul Zulu JDK.)
Note that for this to work in #WebMvcTests, I had to also combine this configuration class along with my Spring Boot application class into a #ContextConfiguration:
#WebMvcTest(controllers = MyController.class)
#ContextConfiguration(classes = { MySpringBootApplication.class, ConversionServiceConfiguration.class })
#TestPropertySource(properties = { /* ... properties to inject into beans, possibly using your custom converter ... */ })
class MyControllerTest {
// ...
}

Related

Spring AutoWire not working when invoking CRUD Repository

I am having a spring boot app which connects to database using Spring JPA and retreives the data for processing. When I try to autowire using I am getting below error. Can someone please throw somelight.
I have added application entry point , Repo and Tasklet below. Please check it.
Application Entry Point
package com.printbatch;
#SpringBootApplication
#EnableJpaRepositories("com.printbatch.config")
public class AgencyBillPayFileUploadApplication implements
CommandLineRunner{
public static void main(String[] args) {
SpringApplication.run(AgencyBillPayFileUploadApplication.class, args);
}
//access command line arguments
#Override
public void run(String... args) throws Exception {
System.out.println("args");
System.out.println(args[0]);
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"ABPBatchInfrastructure.xml",
"AgencyBillPayFileUploadApp.xml"
);
JobLauncher jobLauncher = ctx.getBean(JobLauncher.class);
Job job = ctx.getBean(Job.class);
/*
* jobLauncher.run(job, new JobParametersBuilder().addString("inputResource",
* "file:./products.zip") .addString("targetDirectory",
* "./importproductsbatch/").addString("targetFile", "products.txt")
* .addString("date", "2020-06-02").toJobParameters());
*/
jobLauncher.run(job,
new JobParametersBuilder().addString("inputResource", "file:./products.zip")
.addString("targetDirectory", "./importproductsbatch/").addString("targetFile", "products.txt")
.addString("date", "2005-07-29").toJobParameters());
}
}
package com.printbatch.config;
#Component
public interface PrintJobItemRepo extends CrudRepository<PrintJobItem, Integer> {
List<PrintJobItem> findByPrintStatusEquals(String printStatus );
}
Calling this component in this class
package com.printbatch.tasklet;
public class ReadInputFile implements Tasklet {
private Resource inputResource;
private String targetDirectory;
#Autowired
private PrintJobItemRepo pjr;
}
Error below
Error creating bean with name 'scopedTarget.decompressTasklet': Unsatisfied dependency expressed
through field 'pjr'; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type
'com.printbatch.repo.PrintJobItemRepo' available: expected at least 1 bean which qualifies as
autowire candidate. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type
'com.printbatch.repo.PrintJobItemRepo' available: expected at least 1 bean which qualifies as
autowire candidate. Dependency annotations:
Stack trace tells pretty well what is going on. com.printbatch.repo.PrintJobItemRepo
You are trying to autowire a class that is not in the JPA repository path. Move your class into com.printbatch.config package.
Remove #Component from PrintJobItemRepo. Is ReadInputFile a managed bean? If not, you should annotate it with Component, Service, or make it as a Bean, so it must become managed object. Spring can only inject managed bean into another managed bean.
Add #Component to ReadInputFile class.
Remove #Component from PrintJobItemRepo class.
We generally use #Repository annotation over such classes.
Check out some examples to see how this works -
Spring boot crud example
Another Spring boot crud example

spring IOC qualifier and autowired strange behavior

I've specified Qualifier on both autowired et bean method.
So What am i missing ?
#Configuration
#EnableWebSecurity
public class CustomSecurityCOnfig {
#Bean
#Qualifier("entryPoint")
AuthenticationEntryPoint loginUrlAuthenticationEntryPoint() {
return new LoginUrlAuthenticationEntryPoint("/login");
}
}
I autowire the field this way
#Autowired
#Qualifier("entryPoint")
private AuthenticationEntryPoint loginUrlAuthenticationEntryPoint;
Stacktrace of the error :
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.core.env.Environment' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1716) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1272) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1226) ~[spring-beans-5.2.6.RELEASE.jar:5.2.6.RELEASE]
EDIT
I Have another implementation of AuthenticationEntryPoint interface :
#Component
public class CustomAuthenticationEntryPoint extends BasicAuthenticationEntryPoint
But in my opinion it doesn't explain the error (as long as i specify the qualifiers)
You're mixing bean name and Qualifier
#Bean(name="someFancyBean")
public ClassXyx fooBar(){
return new ClassXyz()
}
In this example, method fooBar creates a bean of type ClassXyx and it's named someFancyBean. If you want to auto-wire this bean then you have to use
#Service
class SomeFancyService{
#Autowired #Qualifier("someFancyBean") ClassXyx xyz;
}
A configuration class can create multiple beans of the same type but their names are derived from function name. There's no point in using Bean annotation with name="XYZ" unless you want to rename that bean.
The Qualifier annotation is used for referring one of the beans of the same type.
Now coming back to your code
#Configuration
#EnableWebSecurity
public class CustomSecurityCOnfig {
#Bean
public AuthenticationEntryPoint entryPoint() {
return new LoginUrlAuthenticationEntryPoint("/login");
}
}
In your service you have to Autowired as.
#Autowired
#Qualifier("entryPoint")
private AuthenticationEntryPoint loginUrlAuthenticationEntryPoint;
Also, I would like to point one more thing bean accessibility across package/class.
Generally, all beans created by Spring IOC are public, but it has the same access modifier like Java classes. If you're creating a bean with package scope then you can't auto-wire in another package. Similarly, if a bean is created using private then that bean can be only auto-wired in that class.

Springboot application throws a NoSuchBeanDefinitionException trying to Autowire CouchbaseCluster [duplicate]

This question already has an answer here:
What is a NoSuchBeanDefinitionException and how do I fix it?
(1 answer)
Closed 2 years ago.
I work with a legacy springboot application which has a huge com.mycompany.MyApplication class where there are injected a bunch of spring objects through #Autowire annotation.
Trying to split MyApplication class up, I created several classes and put them in different packages but I got an issue with CouchbaseCluster autowire injection; the error is:
AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myApplication': Unsatisfied dependency expressed through field 'step'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jobSteps': Unsatisfied dependency expressed through field 'couchbaseFlushCacheTasklet'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'couchbaseFlushCacheTasklet': Unsatisfied dependency expressed through field 'cluster'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.couchbase.client.java.CouchbaseCluster' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
***************************
APPLICATION FAILED TO START
***************************
Description:
Field cluster in com.mycompany.batch.tasklet.CouchbaseFlushCacheTasklet required a bean of type 'com.couchbase.client.java.CouchbaseCluster' that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.couchbase.client.java.CouchbaseCluster' in your configuration.
Here is an example of my code:
package com.mycompany;
#EnableBatchProcessing
#SpringBootApplication
public class MyApplication implements CommandLineRunner {
#Autowired
JobSteps step;
...
}
package com.mycompany.batch;
#Configuration
public class JobSteps {
#Autowired
CouchbaseFlushCacheTasklet couchbaseFlushCacheTasklet;
#Autowired
CouchbaseDeleteNonBatchTasklet couchbaseDeleteNonBatchTasklet;
...
}
If you note in the next code snipped, cluster uses the #SuppressWarnings annotation because Intellij marks this with this error: 'Could not autowire. No beans of 'CouchbaseCluster' type found.' however it was before and after my changes, but before the application ran without issues.
package com.mycompany.batch.tasklet;
#Component
public class CouchbaseFlushCacheTasklet {
#SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
#Autowired
CouchbaseCluster cluster;
...
}
I am putting this next class to show that there is any problem with the bean Budget, which is located in the same library/package than CouchbaseCluster com.couchbase.client.java.Bucket
package com.mycompany.batch.tasklet;
#Component
public class CouchbaseDeleteNonBatchTasklet {
#Autowired
Bucket bucket;
...
}
POM dependencies associated with couchbase
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-couchbase</artifactId>
<version>3.1.9.RELEASE</version>
</dependency>
I tried to specify the scanBasePackeges but didn't work.
#SpringBootApplication(scanBasePackages = {"com.mycompany", "com.couchbase.client"})
Any clue will be appreciated since I have spent a lot of time looking for a solution and so far none of the suggested things in similar posts have worked.
I solved it :-)
Instead of autowire this
#Autowired
CouchbaseCluster cluster;
I autowire this
#Autowired
Cluster cluster;
I am not sure how was possible that at some point it worked autowiring CouchbaseCluster
First thing CouchbaseCluster class should be part of the bean life cycle, it should be annotated with an annotation like #Component etc. if so you must check you should not be creating the object of any class using the new keyword.
So in your case, it will be resolved something like this. add the below method into JobSteps.
#Bean
public CouchbaseCluster couchbaseCluster(){
CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder()
.bootstrapCarrierDirectPort(couchbase.getMappedPort(11210))
.bootstrapCarrierSslPort(couchbase.getMappedPort(11207))
.bootstrapHttpDirectPort(couchbase.getMappedPort(8091))
.bootstrapHttpSslPort(couchbase.getMappedPort(18091))
.build();
CouchbaseCluster cc = CouchbaseCluster.create(env);
return cc;
}
Modified the CouchbaseEnvironment object according to your configuration.
Have you tried creating your Cluster in the #PostConstruct?
#Service
public class CouchbaseFlushCacheTasklet {
private Cluster cluster;
#PostConstruct
private void init() {
CouchbaseEnvironment env = DefaultCouchbaseEnvironment.create();
cluster = CouchbaseCluster.create(env, "localhost");
}
...
}
Code from: https://www.baeldung.com/couchbase-sdk-spring
Alternatively, try adding an #Bean producer somewhere instead of using a #PostConstruct.

Spring - No default constructor found

I have a class which looks like this:
#Service("myService")
public class MyServiceImpl {
#Autowired
private SimpMessagingTemplate simpMessagingTemplate;
and I also have a test class which looks like this:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = {
MyServiceImpl.class})
...
I get this exception:
Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire
field: private org.springframework.messaging.simp.SimpMessagingTemplate
myPackage.MyServiceImpl.simpMessagingTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.messaging.simp.SimpMessagingTemplate] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
Does anyone know what I can do in order to get it work, SimpMessagingTemplate does not have a default constructor.
SimpMessagingTemplate seems to have either no default constructor or is not annotated with #Component (or #Service or another sub-class of #Component); or both.
Please check that a default constructor is available and the class is configured to be a Spring bean.
Its not related to missing constructor, but Spring fails to find proper bean to inject into your test class,
Two options to solve it as I see
#ContextConfiguration(classes = {
MyServiceImpl.class, SimpMessagingTemplate.class})
Add #mock SimpMessagingTemplate simpMessagingTemplate; to your test class

Autowiring Embedded Elastic with Spring

I'm trying to build a rest api with Spring and Embedded Elastic. I'm getting an NoSuchBeanDefinitionException when trying to start my application.
Currently, I have this for wiring the elastic db:
#Configuration
public class EsConfig {
Node node;
#Bean
public Client es() {
node = nodeBuilder().local(true).node();
return node.client();
}
(Destructor)
}
and in the controller:
#RestController
public class Controller {
#Autowired
public Client elasticSearchClient;
...
}
But when I start it up, I get this exception:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'controller': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public org.elasticsearch.client.Client package.Controller.elasticSearchClient;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [org.elasticsearch.client.Client] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
I've tried a few different annotations but I'm obviously way off.
No qualifying bean of type [some.Thing] means that spring knowns no class that is applicable for this interface.
Reasons for that can be
The class that has the #Bean method is not a #Configuration class
The #Configuration class is not picked up by the classpath component scanner.
Spring boot by default will only scan the child package hierarchy of the #SpringBootApplication. If you want to include code outside of that you can change the scanning behavior via the #ComponentScan annotation.
#SpringBootApplication
#ComponentScan(basePackageClasses = {MyApp.class, SomeOtherClassInARootPackage.class})
public class MyApp {
...
Would add the package (and sub packages) of some other class, while keeping the packages of the application scanned as well.

Categories

Resources