#Component
public class BankServicesImpl implements BankServices {
#Autowired
private DataRepoImpl db;
}
----------------------------------------------------------------------------
#Component
public class DataRepoImpl implements DataRepo{
private Map<Integer, Account> repo = new HashMap<Integer,Account>();
}
----------------------------------------------------------------------------
#Component
public class Account {
private Integer accountID;
private int balance;
}
The codes do the job, the repo HashMap object is created ( {} ). Yet i am trying to get the repo Map object to be generated by Spring. so i changed the DataRepoImpl into:
#Component
public class DataRepoImpl implements DataRepo{
#Autowired
private Map<Integer, Account> repo;
}
Error:
Nov 08, 2021 11:37:06 PM org.springframework.context.support.AbstractApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataRepoImpl': Unsatisfied dependency expressed through field 'repo'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.Map<java.lang.Integer, com.doubleliu.model.Account>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataRepoImpl': Unsatisfied dependency expressed through field 'repo'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.Map<java.lang.Integer, com.doubleliu.model.Account>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.Map<java.lang.Integer, com.doubleliu.model.Account>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
However, when i refactor accountID and Map key into String type,
i able to produce the HashMap containing a 'dummy' variable which is weird:
{account=com.doubleliu.model.Account#397fbdb}
Back to Integer, I couldnt fix the error, I then tried to move the Autowired to the empty constructor of the DataRepoImpl class:
#Autowired
public DataRepoImpl() {
}
However i get null value from repo, since (my assumption) the object hasnt been created or assigned to the repo Map variable.
Again, i then move the Autowired onto the constructor with map as the parameter:
#Autowired
public DataRepoImpl(Map<Integer, Account> repo) {
this.repo = repo;
}
This time the repo object is created ( {} ) and not null, i assume that the object is created through the parameter, is this the right way to create it utilizing spring? also my IDE flag me with an error of
Could not autowire. No beans of 'Map<Integer, Account>' type found. but i can still compile it. i wonder how to fix it.. fyi, I am using IntelliJ IDEA
I just started learning spring for a bit, trying to completely understand before i go to the next level. so any response, solutions, comments, advice to the code, fix, and issue is very pleased especially with the autowired.
Thank you.
EDIT: i am using xml annotation based configuration
Related to here
I just found out that i need to define the properties of the defined bean in the xml. everytime i define something like HashMap, add following to the xml and it enables me to inject the just defined Map bean by defining #Resource on the Map variable inside the class.
<util:map id="repo" scope="prototype" map-class="java.util.HashMap"
key-type="java.lang.String" value-type="com.doubleliu.model.Account"/>
Thank you for the help and comments, any more comments and advices are welcomed.
Related
I want to declare ApplicationContext and take out Bean using getBean.
But I got this error.
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'inactiveMemberJobConfig': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.capston.chatting.repository.MemberRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.capston.chatting.repository.MemberRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
I expect that I have already used #Repository to register the MemberRepository as an bean
However, the error log shows that there is no MemberRepository.
I don't understant where is wrong
Below is my code.
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(InactiveMemberJobConfig.class);
Job inactiveMemberJob = (Job) ac.getBean("inactiveMemberJob");
#Repository
public interface MemberRepository extends JpaRepository<Member, Long> {
#Query("select m from Member m where m.loginId = :loginId")
Member findMemberByLoginId(#Param("loginId") String loginId);
List<Member> findByUpdateDateBeforeAndStatusEquals(LocalDateTime localDateTime, MemberStatus status);
}
#Slf4j
#RequiredArgsConstructor
#Configuration
public class InactiveMemberJobConfig {
private final MemberRepository memberRepository;
//...
}
For a bean to be known to an application context it needs to be registered. That can happen e.g. through component scanning which detects the annotation on the class and registers it as a bean.
When you do new AnnotationConfigApplicationContext() you created a new application context. This application context did not do any component scanning yet. Furthermore, within this constructor it tries to find the bean dependencies of the class you pass as constructor parameter. But since no component scanning has taking place yet it cannot find it.
You need to create your application context with a class that has no bean dependencies yet. And then register your beans, e.g. through executing the component scan on you application context.
See example below:
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MyConfig.class);
ac.scan(MyConfig.class.getPackageName());
InactiveMemberJobConfig inactiveMemberJob = (InactiveMemberJobConfig) ac.getBean(InactiveMemberJobConfig.class);
I hope this helps.
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.
There is a JUnit test as follow:
Memory compiler compiles a string to class on.the.fly.The report, which contains some #Autowired declared fields, like reportRepository etc.
Everything works as expected, the dynamic class report can use autowired fields implements its functionality.
When it is put in MVC environment, using autowired application context, which is AnnotationConfigEmbeddedWebApplicationContext, autowired property value can't be populated.
with debug, findCandidateNames method return [], which should return an array containing field name as in test environment. Please refer to the regarding source code and give me some suggestion on it. Thanks.
results = compiler.compile("Report.java", reportClassString);
Class<?> clazz = compiler.loadClass("on.the.fly.Report", results);
IReport report = (IReport) applicationContext.getAutowireCapableBeanFactory().createBean(clazz);
report.creatPdf(reportObject);
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'on.the.fly.AgreementReport':
Unsatisfied dependency expressed through field
'agreementReportProperties'; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
'com.lims.api.report.domain.dto.AgreementReportProperties' available:
expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587)
at
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
at
com.lims.api.sample.web.controller.AgreementController.downloadPdfFile(AgreementController.java:161)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
After a few days research, I got the root reason.
Just make my customized class loader as the child of spring context class loader, and then the dependency could be injected successfully.
the source code is as follow:
Class clazz = compiler.loadClass("on.the.fly.AgreementReport",
results,context.getClassLoader());
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
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 {
// ...
}