Cannot autowire child bean (defined in a web context) from root context - java

I have a bean in the root context (ApplicationContext) which autowires another bean in a WebContext and it's throwing:
Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pageFlowController': Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.flex.messaging.MessageTemplate
com.biosds.gcfs.nassic.flow.PageFlowController.msgTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type
[org.springframework.flex.messaging.MessageTemplate] 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'm trying to get a MessageTemplate in PageFlowController, where PageFlowController is defined in the root context and MessageTemplate in a web context.

A child context has access to beans in a parent context, but a parent context has no access to beans in a child context.
See the Spring Reference for more information.

Related

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'pessoaDAO' defined in VFS resource

I have a JSF project and now I need to create some REST API inside it to return DB info for another aplication. I configured another servelet for the Spring (DispatcherServlet) in web.xml.
I created the Controller, Service and DAO(api package). The folowwing problem is this: When I DONT inject the DAO in the Service, it runs normaly, but when I #Inject the DAO into the Service it goes the exception:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'pessoaDAO' defined in VFS resource ["/C:/Users/Ian/Desktop/cadWeb/target/cadWeb/WEB-INF/classes/br/rr/gov/digigov/cadWeb/api/PessoaDAO.class"]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManager' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
**
WEB.XML
dispatcher-servlet.XML
DAO
My controller and Service are just simple classes with #Controller and #Service anotation and #Inject to the other class

Error creating bean; Unsatisfied dependency expressed through field

I have a central library for certain functions and now I have trouble integrating that library.
The library is written in spring boot and contains a class: com.common.Security.
It is defined like this:
package com.common;
....
#Service
#EnableConfigurationProperties(SecurityProperties.class)
#Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class Security {
....
}
I try to use this in another class:
package org.special;
import com.common.Security;
#Configuration
public class WebServiceConfig {
#Autowired
private Security security;
....
}
But I get some errors:
Error creating bean with name 'myController': Unsatisfied dependency expressed through field 'WebServiceclient';
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'webserviceClient':
Unsatisfied dependency expressed through field 'template'; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'webServiceTemplate' defined in class path resource [org/special/WebServiceConfig.class]:
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.springframework.ws.client.core.WebServiceTemplate]: Factory method 'webServiceTemplate' threw exception;
nested exception is org.springframework.beans.factory.BeanCreationException:
nested exception is org.springframework.beans.factory.support.ScopeNotActiveException: Error creating bean with name 'scopedTarget.Security': Scope 'request' is not active for the current thread;
consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found:
What can I do to fix this?
Removing the Scope was quite helpfull.
I tried this before but some of my tests failed after that. I didn't see, that this was because of missing settings in application.yaml for the tests.
They were not neccessary when scope is request.

Injection of autowired dependencies failed inside a jar file

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.

Use Bean from an another module

I am creating a Batch application to put some data from a web-service on the database.
To do this I need to convert DTO's received by the web-service on utilizable classes. This is possible using some mapper defined in an other module.
When I tried to #Autowired the mapper I have an Exception and I don't find how to pass out.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.execution.batch.processor.UtilisateurProcessor com.execution.batch.jobConfiguration.JobConfigurationAbstract.utilisateurProcessor; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'utilisateurProcessor': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.execution.server.common.mapper.UtilisateurMapper com.execution.batch.processor.UtilisateurProcessor.utilisateurMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.execution.server.common.mapper.UtilisateurMapper] 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)}
**Caused by**: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.execution.batch.processor.UtilisateurProcessor com.execution.batch.jobConfiguration.JobConfigurationAbstract.utilisateurProcessor; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'utilisateurProcessor': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.execution.server.common.mapper.UtilisateurMapper com.execution.batch.processor.UtilisateurProcessor.utilisateurMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.execution.server.common.mapper.UtilisateurMapper] 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)}
**Caused by**: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'utilisateurProcessor': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.execution.server.common.mapper.UtilisateurMapper com.execution.batch.processor.UtilisateurProcessor.utilisateurMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.execution.server.common.mapper.UtilisateurMapper] 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)}
**Caused by**: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.execution.server.common.mapper.UtilisateurMapper com.execution.batch.processor.UtilisateurProcessor.utilisateurMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.execution.server.common.mapper.UtilisateurMapper] 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)}
**Caused by**: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.execution.server.common.mapper.UtilisateurMapper] 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)}
The mappers are working fine in their module. I can #Autowierdd them and use it without problems. And the my batch is working fine if I don't #Autowierd the mapper.
Someone have an idea?
Thanks!
EDIT
My configuration is on my job configuration class :
#Configuration
#EnableBatchProcessing
#ComponentScan(basePackages = {"com.execution.batch"})
I tried to put : #ComponentScan(basePackages = {"com.execution.batch", "com.execution.serve"})
But if I do that I have errors reported to the injection of the job's bean :
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.batch.core.configuration.annotation.StepBuilderFactory com.execution.batch.jobConfiguration.JobConfigurationAbstract.stepBuilderFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stepBuilders' defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.configuration.annotation.StepBuilderFactory]: Factory method 'stepBuilders' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jobRepository' defined in com.execution.batch.jobConfiguration.JobConfiguration: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.batch.support.transaction.ResourcelessTransactionManager]: : No qualifying bean of type [org.springframework.batch.support.transaction.ResourcelessTransactionManager] 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 [org.springframework.batch.support.transaction.ResourcelessTransactionManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.batch.core.configuration.annotation.StepBuilderFactory com.execution.batch.jobConfiguration.JobConfigurationAbstract.stepBuilderFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stepBuilders' defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.configuration.annotation.StepBuilderFactory]: Factory method 'stepBuilders' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jobRepository' defined in com.execution.batch.jobConfiguration.JobConfiguration: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.batch.support.transaction.ResourcelessTransactionManager]: : No qualifying bean of type [org.springframework.batch.support.transaction.ResourcelessTransactionManager] 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 [org.springframework.batch.support.transaction.ResourcelessTransactionManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stepBuilders' defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.configuration.annotation.StepBuilderFactory]: Factory method 'stepBuilders' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jobRepository' defined in com.execution.batch.jobConfiguration.JobConfiguration: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.batch.support.transaction.ResourcelessTransactionManager]: : No qualifying bean of type [org.springframework.batch.support.transaction.ResourcelessTransactionManager] 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 [org.springframework.batch.support.transaction.ResourcelessTransactionManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jobRepository' defined in com.execution.batch.jobConfiguration.JobConfiguration: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.batch.support.transaction.ResourcelessTransactionManager]: : No qualifying bean of type [org.springframework.batch.support.transaction.ResourcelessTransactionManager] 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 [org.springframework.batch.support.transaction.ResourcelessTransactionManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.batch.support.transaction.ResourcelessTransactionManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
Like I said, separately the to modules work fine.
when I scan the server and the batch module the scan of the batch don't work.
When I scan just the batch module, the server module don't work because isn't scan I think.
Since your configurations work independently, there is a good change that you have conflicting names in both packages. Which could result that configurations or beans are overwritten.
E.g.
package com.x;
#Configuration
public class AConfiguration {
}
package com.y;
#Configuration
public class AConfiguration {
}
As the beanname in the springcontext, only the class name is used without the package. So, if you scan both packages, only an Configuration-Class will actually be processed. The same is also valid for classes annotated with #Component.
It is important to understand, that #Component and #Configuration classes are also instantiated as beans, with the simple classname as beanname.
Also with #Bean annoted methods are a potential source of errors. If you have the same methodname in two different Configuration-Beans, only one will be instantiated.
Moreover, if you use as #Bean methodname the same name as a #Configuration class name has, then again, one is overwritten by the other.
I guess that you have forgotten to set the "scanner" with the appropriate package of the class that you want.
Your bean is utilisateurProcessor, fqn is com.execution.batch.processor.UtilisateurProcessor. Is it configured as a #Component or in your XML?
Please paste the spring' config, to see if you have added com.execution.server.common.mapper to Spring's scanner.

Having an issue with launching Spring Cloud Hystrix Dashboard Example

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.

Categories

Resources