I have put a jar file under lib folder of my war file. Inside the jar I have some classes with #Autowired fileds. Inside the jar , in the applicationContext xml I have given
<context:component-scan base-package="com.main.java.mypath" />
Code:
package com.main.java.mypath.client;
#Component
public class ServiceProvider {
#Autowired
private StoreField storeField;
package com.main.java.mypath.data;
public interface StoreField {
}
Error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serviceProvider': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.main.java.mypath.data.StoreField com.main.java.mypath.client.ServiceProvider.storeField; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.main.java.mypath.data.StoreField] 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)}
It is not possible to inject the dependency as long as you don't have the implementation for your StoreField interface. Make sure that it is implemented as a class and properly initialized in spring context via xml or annotation.
Related
#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.
Through maven command everything is fine and tests are running good.
I am creating an executable jar which will except same arguments and run the testng tests.
I am using assembly plugin to create the executable jar with my main method class.
When running through jar
java -jar <jar name>.jar
Getting following error.
Caused by:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'apiClientFacade' defined in
com.test.framework.TestContext: Unsatisfied dependency
expressed through constructor argument with index 0 of type
[com.test.framework.configuration.TestConfiguration]: :
No qualifying bean of type
[com.test.framework.configuration.TestConfiguration]
found for dependency: expected at least 1 bean which qualifies as
autowire candidate for this dependency. Dependency annotations: {};
nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
[com.test.framework.configuration.TestConfiguration]
found for dependency: expected at least 1 bean which qualifies as
autowire candidate for this dependency. Dependency annotations:
Related JAVA files
TestContext.java
#Configuration
#Import({EmbeddedContext.class, FullDataContext.class})
public class TestContext {
#Bean
#Autowired
#Primary
public ApiClientFacade apiClientFacade(TestConfiguration configuration,
#Qualifier("apiClient") ApiClient apiClient) {
return new ApiClientFacadeImpl(configuration, apiClient);
}
}
TestConfiguration.java
#Component
public class TestConfiguration { }
ApiClientFacadeImpl.java
#Component
public class ApiClientFacadeImpl implements ApiClientFacade {
}
ApiClientFacade.java
public interface ApiClientFacade {}
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.
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?
Trying to launch a Hystrix dashboard and receiving an error upon launch. Following the example provided via github as the base. Have not made any changes to the code really. Just trying to launch.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Configuration
#ComponentScan
#EnableAutoConfiguration
#EnableHystrixDashboard
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}
#Controller
class HystrixDashboardController {
#RequestMapping("/")
public String home() {
return "forward:/hystrix/index.html";
}
}
Below is the error being reported:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'restTemplate' defined in class path resource [org/springframework/cloud/netflix/ribbon/RibbonAutoConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.cloud.netflix.ribbon.RibbonInterceptor]: : Error creating bean with name 'ribbonInterceptor' defined in class path resource [org/springframework/cloud/netflix/ribbon/RibbonAutoConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.cloud.client.loadbalancer.LoadBalancerClient]: : Error creating bean with name 'loadBalancerClient': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.cloud.netflix.ribbon.SpringClientFactory org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.clientFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.cloud.netflix.ribbon.SpringClientFactory] 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 'loadBalancerClient': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.cloud.netflix.ribbon.SpringClientFactory org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.clientFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.cloud.netflix.ribbon.SpringClientFactory] 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.UnsatisfiedDependencyException: Error creating bean with name 'ribbonInterceptor' defined in class path resource [org/springframework/cloud/netflix/ribbon/RibbonAutoConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.cloud.client.loadbalancer.LoadBalancerClient]: : Error creating bean with name 'loadBalancerClient': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.cloud.netflix.ribbon.SpringClientFactory org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.clientFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.cloud.netflix.ribbon.SpringClientFactory] 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 'loadBalancerClient': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.cloud.netflix.ribbon.SpringClientFactory org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.clientFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.cloud.netflix.ribbon.SpringClientFactory] 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 have tried both spring-cloud-starters 1.0.0.BUILD-SNAPSHOT & 1.0.0.M1.
The workaround for now is to add #EnableEurekaClient.
I reproduced and worked around the issue here: https://github.com/spencergibb/communityanswers/tree/so26450251
I have raised issue #35
I am using Spring boot 2.1.5 release.
Faced same issue.
Tried to launch hystrix dashboard using below url -
http://localhost:8082/hystrix.html
but it didn't work then I tried
http://localhost:8082/hystrix
and it worked.