Consider we have interface CountryService, which has one implementation: CountryServiceImpl which is annotated with #Service
We also have ITTest for CountryImpl.
The problem:
Frequently when running tests we want to explicitly show what we are testing so we are autowiring by implementation, so we create ITTest:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = Application.class, loader = SpringApplicationContextLoader.class)
#ActiveProfiles(ProfileConstants.SPRING_PROFILE_DEVELOPMENT)
public class CountryServiceITTest {
#Autowired
CountryServiceImpl countryServiceImpl;
#Test
public void test() {
countryServiceImpl.getAllCountriesTranslationsInLanguage("en");
}
So far so good. We run our test and it is green. Now lets make life little more fun.
We add a CountryResource with CountryResourceImpl #RestController which #Autowire (s) CountryService.
We run our test again and suddenly we receive error message causing maddness:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'countryResourceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com....service.CountryService com....rest.CountryResourceImpl.countryService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com....service.CountryService] 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)}
This behavior reveals an interesting thing:
Once we autowire by interface, we can't autowire by direct implementation somewhere else. This works vice versa(if we autowire by impl the interface injection will fail).
Please explain why is this happening?
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.
#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.
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
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.
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 {
// ...
}