Java Spring annotation error - java

I'm writing a web application using Spring MVC, I have en error, and I don`t know what's causing it.
I was looking for any solution but adding annotations like #Component didn't help.
Here is the code of classes:
Repository class:
#Repository("UserRepository")
public interface UserRepository extends JpaRepository<User, Integer> {
#Query("Insert INTO User Values(email,password)")
public User AddUser(#Param("email") String email, #Param("password") String password);
}
Controller class:
#RestController
public class UserActionController {
private UserRepository userservice;
#Autowired
public UserActionController(UserRepository userservice)
{
this.userservice = userservice;
}
#RequestMapping(value = { "/zaloguj" }, method = RequestMethod.GET)
public ModelAndView logowanie(#RequestParam String action) {
ModelAndView model = new ModelAndView();
model.addObject("title", "Injected MVC title");
model.addObject("message", "This is default page!");
model.setViewName("zaloguj");
userservice.AddUser("test", "test");
return model;
}
}
And the error:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userActionController' defined in file [C:\Users\bartlomiej.zuk\workspaceEclipse4\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\examplespringproject\WEB-INF\classes\com\mkyong\web\controller\UserActionController.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.mkyong.dao.UserRepository]: : No qualifying bean of type [com.mkyong.dao.UserRepository] 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.mkyong.dao.UserRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:838)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5236)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.mkyong.dao.UserRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
... 24 more

No qualifying bean of type [com.mkyong.dao.UserRepository]
This means that spring could not instantiate the UserRepository. You somehow should tell the spring where's your repositories location. For example, you should have a Data Access Configuration like following:
#Configuration
#EnableJpaRepositories(basePackages="com.mkyong.dao")
public class DataConfig {
// Put your DataSource, EntityManagerFactory, PlatformTransactionManager, etc here
}
basePackages="com.mkyong.dao" tells the spring container to look for repositories in com.mkyong.dao package. Also, when you're using Spring Data, there is no need for #Repository annotations.

Related

org.springframework.beans.factory.UnsatisfiedDependencyException occured. but I didn't change anything

org.springframework.beans.factory.UnsatisfiedDependencyException occured While I'm developing using Spring boot.
In to the point, I didn't change anything.
I tried check the fileUtil class, but there isn't any specific problem.
And did "Project clean" and reboot eclipse.
after searched on google, somebody says "check your annotation is valid"
so I checked annotations but there is nothing to guess
but error still not solved..
I'll show you my code
this is the exception.
심각: Exception sending context initialized event to listener instance of class [org.springframework.web.context.ContextLoaderListener]
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'fileUtil': Unsatisfied dependency expressed through field 'config': No qualifying bean of type [com.m2u.chatbotany.common.utils.ConfigProperties] found for dependency [com.m2u.chatbotany.common.utils.ConfigProperties]: 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.NoSuchBeanDefinitionException: No qualifying bean of type [com.m2u.chatbotany.common.utils.ConfigProperties] found for dependency [com.m2u.chatbotany.common.utils.ConfigProperties]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:350)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:775)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4770)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5236)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1423)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1413)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.m2u.chatbotany.common.utils.ConfigProperties] found for dependency [com.m2u.chatbotany.common.utils.ConfigProperties]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1398)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1051)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1018)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:570)
... 24 more
This is the stackTrace that I got from application after add #Component annotation.
심각: Exception sending context initialized event to listener instance of class [org.springframework.web.context.ContextLoaderListener]
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'fileUtil': Unsatisfied dependency expressed through field 'config': No qualifying bean of type [com.m2u.chatbotany.common.utils.ConfigProperties] found for dependency [com.m2u.chatbotany.common.utils.ConfigProperties]: 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.NoSuchBeanDefinitionException: No qualifying bean of type [com.m2u.chatbotany.common.utils.ConfigProperties] found for dependency [com.m2u.chatbotany.common.utils.ConfigProperties]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:350)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:775)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4770)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5236)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1423)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1413)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.m2u.chatbotany.common.utils.ConfigProperties] found for dependency [com.m2u.chatbotany.common.utils.ConfigProperties]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1398)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1051)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1018)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:570)
... 24 more
and this is [com.m2u.chatbotany.common.utils.ConfigProperties]
(#Component <<- I added this annotation but nothing change )
public class ConfigProperties {
private static final Logger LOG = LoggerFactory.getLogger(ConfigProperties.class);
private Properties properties;
public ConfigProperties() {
LOG.info("ConfigProperties");
}
// ~ Implementation Method. ~~~~~~~~
// ~ Self Methods. ~~~~~~~~~~~~~~~~~
public final void init() {
// 환경 변수 설정 log 출력
if (this.properties != null) {
LOG.info("Initialize Config Variables");
// LOG.info(JsonUtil.marshallingJsonWithPretty(this.properties));
}
}
// ~ Getter and Setter
// ==============================================================================================
public final void setProperties(final Properties properties) {
this.properties = properties;
}
public final String getString(final String key) {
return this.properties.getProperty(key);
}
public final Integer getInteger(final String key) {
String value = this.properties.getProperty(key);
return new Integer(value);
}
}
This is fileUtil
/**
* 업로드 파일 유틸
*/
#Component("fileUtil")
public class FileUtil {
/**
* The constant LOG.
*/
private static final Logger LOG = LoggerFactory.getLogger(FileUtil.class);
/**
* The Config.
*/
#Autowired
private ConfigProperties config;
......
}
This problem is I have never seen...
and my application had completely worked until 3 hours ago.
repeatly, I didn't change anything
waiting for your help.
No qualifying bean of type [com.m2u.chatbotany.common.utils.ConfigProperties]
Above error is because in FileUtil you have autowired ConfigProperties but ConfigProperties.class is not annotated with Stereotype (#Component, #Service etc)
So add the #Component annotation like below
#Component
public class ConfigProperties {
// Code logic
}

Inject custom feign client into Spring Application

I am currently trying to get a grip of both Spring and Feign.
Cutting straight to the point:
I am struggling to modify #FeignClient in this project:
Feign Hello World by Walery
so as to instead of
WikidataClient
#FeignClient(url = "https://www.wikidata.org/w")
// https://www.wikidata.org/w/api.php?action=wbsearchentities&search=apple&language=en&format=json
public interface WikidataClient {
#RequestMapping(value = "/api.php?action=wbsearchentities&language=en&format=json", method = GET)
WebsearchEntities searchForEntities(#RequestParam("search") final String search);
}
use #Autowired notation similar to one found here: Section called : Creating Feign Clients Manually
The purpose of this would be to inject custom decoder and encoder later on. I've been exprimenting with it for a while and all I managed to achieve was ruin the whole thing.
I gathered some clues from here and there and managed to come to the point where I created a Configuration class :
FeignConfig
#Import(FeignClientsConfiguration.class)
public class FeignConfig {
public WikidataClient fooclient;
#Autowired
public FeignConfig(Encoder encoder, Decoder decoder){
this.fooclient = Feign.builder()
.encoder(encoder)
.decoder(decoder)
.target(WikidataClient.class,"https://www.wikidata.org/w");
}
}
Modified
WikidataClient
interface slightly
//#FeignClient(url = "https://www.wikidata.org/w")
// https://www.wikidata.org/w/api.php?action=wbsearchentities&search=apple&language=en&format=json
public interface WikidataClient {
#RequestMapping(value = "/api.php?action=wbsearchentities&language=en&format=json", method = GET)
WebsearchEntities searchForEntities(#RequestParam("search") final String search);
}
and tried to use aforementioned class instead
WikidataRunner
#Component
public class WikidataRunner implements CommandLineRunner {
private final WikidataClient omdbClient;
#Autowired
public WikidataRunner(WikidataClient omdbClient){
this.omdbClient = omdbClient;
this.feignConfig = new FeignConfig(new Encoder.Default(), new Decoder.Default());
}
FeignConfig feignConfig;
#Override
public void run(final String... args) throws Exception {
final WebsearchEntities apple = feignConfig.fooclient.searchForEntities("apple");
System.out.println(apple);
}
}
All I got were different kind of Bean errors
2017-07-19 08:02:29.056 ERROR 2018 --- Exception in thread "main"
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'wikidataRunner' defined in file
[/home/mibi/IdeaProjects/FUFEign/feign-helloworld/target/classes/codes/walery/research/feign/wikidata/WikidataRunner.class]:
Unsatisfied dependency expressed through constructor argument with
index 0 of type [codes.walery.research.feign.wikidata.WikidataClient]:
: No qualifying bean of type
[codes.walery.research.feign.wikidata.WikidataClient] 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
[codes.walery.research.feign.wikidata.WikidataClient] found for
dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations: {} [
main] at
org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
o.s.boot.SpringApplication at
org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185)
: Application startup failed at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046)
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
[codes.walery.research.feign.wikidata.WikidataClient] found for
dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations: {} at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1326)
~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
at
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1072)
~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:967)
~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] at
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834)
at
org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] at
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
at
org.springframework.boot.SpringApplication.refresh(SpringApplication.java:667)
at
org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] at
org.springframework.boot.SpringApplication.doRun(SpringApplication.java:342)
... 18 common frames omitted at
org.springframework.boot.SpringApplication.run(SpringApplication.java:273)
at
org.springframework.boot.SpringApplication.run(SpringApplication.java:980)
at
org.springframework.boot.SpringApplication.run(SpringApplication.java:969)
at
codes.walery.research.feign.FeignHelloworldApplication.main(FeignHelloworldApplication.java:12)
Wrapped by:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'wikidataRunner' defined in file
[/home/mibi/IdeaProjects/FUFEign/feign-helloworld/target/classes/codes/walery/research/feign/wikidata/WikidataRunner.class]:
Unsatisfied dependency expressed through constructor argument with
index 0 of type [codes.walery.research.feign.wikidata.WikidataClient]:
: No qualifying bean of type
[codes.walery.research.feign.wikidata.WikidataClient] 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
[codes.walery.research.feign.wikidata.WikidataClient] found for
dependency: expected at least 1 beCaused by:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
[codes.walery.research.feign.wikidata.WikidataClient] found for
dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations: {} at
org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1326)
an which qualifies as autowire candidate for this dependency.
Dependency annotations: {} at
org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1072)
at
org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] at
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:967)
at
org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
at
org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185)
~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] at
org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143)
~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] ... 18 more at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046)
~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] at
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] at
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
~[spring-beans-4.2.1.RELEASE.jar:4.2.1.RELEASE] at
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834)
~[spring-context-4.2.1.RELEASE.jar:4.2.1.RELEASE] at
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
~[spring-context-4.2.1.RELEASE.jar:4.2.1.RELEASE] at
org.springframework.boot.SpringApplication.refresh(SpringApplication.java:667)
[spring-boot-1.3.0.M5.jar:1.3.0.M5] at
org.springframework.boot.SpringApplication.doRun(SpringApplication.java:342)
[spring-boot-1.3.0.M5.jar:1.3.0.M5] at
org.springframework.boot.SpringApplication.run(SpringApplication.java:273)
[spring-boot-1.3.0.M5.jar:1.3.0.M5] at
org.springframework.boot.SpringApplication.run(SpringApplication.java:980)
[spring-boot-1.3.0.M5.jar:1.3.0.M5] at
org.springframework.boot.SpringApplication.run(SpringApplication.java:969)
[spring-boot-1.3.0.M5.jar:1.3.0.M5] at
codes.walery.research.feign.FeignHelloworldApplication.main(FeignHelloworldApplication.java:12)
[classes/:na] 2017-07-19 08:02:29.059 INFO 2018 --- [ Thread-1]
s.c.a.AnnotationConfigApplicationContext : Closing
org.springframework.context.annotation.AnnotationConfigApplicationContext#3d8314f0:
startup date [Wed Jul 19 08:02:24 CEST 2017]; root of context
hierarchy
Process finished with exit code 1
I won't deny being novice at Spring and Feign. Thing is I need to undestand both of these desperately. So far I've spent 10+ hours researching about Feign to no avail.
Kindly asking for help and guidance
MissingBracket
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
[codes.walery.research.feign.wikidata.WikidataClient] found for
dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency.
try to declare a bean somewhere:
#Bean
public WikiDataClient wikiDataClient () {
}
Construct feign client with feign builder manually:
Feign.builder().client(client).encoder(encoder).decoder(decoder).contract(contract).target(WikiDataClient.class, "http://serviceId")

Failing injection SubjectRepository to DataService in gradle multiproject [spring data]

I have issue with injecting repository to service.
It's my first project in spring.
I made a tutorial from this site:
http://blog.netgloo.com/2014/10/27/using-mysql-in-spring-boot-via-spring-data-jpa-and-hibernate/
and i don't know what is wrong. I try annotation #EnableJpaRepositories, #Repository in interface but didn't help me.
All code is on my github: https://github.com/kklimczak/OpenStudies
Stacktrace:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [pl.konradklimczak.openstudies.data.Subject.SubjectRepository] 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)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
#Transactional
#Repository
use this two annotation in your SubjectRepository from
import org.springframework.stereotype.Repository; import
org.springframework.transaction.annotation.Transactional;

Spring Boot - Bean creation exception

I'm creating a sample application using using Spring boot and maven. The auto-wiring using #Autowire is not working. I'm getting the following exception:
[2015-10-16 16:39:51.233] boot - 3216 INFO [main] --- AnnotationConfigApplicationContext: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext#5f0fd5a0: startup date [Fri Oct 16 16:39:51 IST 2015]; root of context hierarchy
[2015-10-16 16:39:51.921] boot - 3216 WARN [main] --- AnnotationConfigApplicationContext: Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testApp': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.bean.TestBean com.test.TestApp.testBean; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.bean.TestBean] 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)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) ~[spring-context-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) [spring-context-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
at com.test.TestApp.main(TestApp.java:22) [classes/:?]
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.bean.TestBean com.test.TestApp.testBean; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.bean.TestBean] 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)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
... 15 more
TestApp.java
#SpringBootApplication
#EnableAutoConfiguration
public class TestApp {
#Autowired
private TestBean testBean;
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(TestApp.class, args);
context.getBean(TestApp.class);
}
}
TestBean.java
#Component
public class TestBean {
#PostConstruct
public void init() {
System.out.println("init from TestBean");
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Can anyone help me ?
The default #SpringBootApplication enables component scan only for the package of the application class and all subpackages. Since the package of TestApp is com.test, and the package of TestBean is com.bean, component scan won't detect the bean class.
Solution:
If you are using Spring Boot 1.2.x:
Either add the following annotation to your TestApp class:
#ComponentScan({"com.test", "com.bean"})
or move the TestBean to a subpackage of com.test, e.g. com.test.bean
If you are already using Spring Boot 1.3.x:
Add the following parameter to your #SpringBootApplication annotation:
#SpringBootApplication(scanBasePackages = {"com.test", "com.bean"})
It seems that Spring is not aware of your TestBean so #Autowired cannot find it in the bean registry.
In order to make the #Autowired work add #ComponentScan after #EnableAutoConfiguration annotation.
If you defined TestBean in your-app-context.xml try adding this
#ImportResource(value={"your-app-context.xml"})
after #EnableAutoConfiguration annotation.
So, for example, if your-app-context.xml was in the META-INF/spring folder located into src/main/resources of your Eclipse project, the correct line would be:
#ImportResource(value={"/META-INF/spring/your-app-context.xml"})
It will expose all the beans declared in that context to Spring Boot.

Spring MVC #EnableWebMvc Bean config

I´m trying to configure a Bean to be used in a Spring MVC Controller app using the #Bean annotation.
As far as I know, the #Bean annotation is equivalent to the tag in XML configuration. What I am doing is the following:
The class with the configuration
#Configuration
public class ContextConfig {
#Bean
public MyBean myBean() {
return new MyBean();
}
}
But when trying to autowire myBean in an MVC Controller it fails.
The bean injection
#Controller
public class HomeController {
#Autowired
private MyBean myBean;
#RequestMapping({"/", "/home"})
public ModelAndView home (ModelAndView model) {
model.setViewName("home");
return model;
}
}
The error is:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.validation.ValidatorFactory com.proeza.sgs.controller.HomeController.factory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.validation.ValidatorFactory] 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)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.validation.ValidatorFactory com.proeza.sgs.controller.HomeController.factory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.validation.ValidatorFactory] 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)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
... 22 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.validation.ValidatorFactory] 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)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1100)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
... 24 more
Final comments
There are other beans configured via annotation (#Bean), that are being injected without problem.
The MyBean class is not implementing nor extending anything.
The configuration location is declared in the web.xml
Conf:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.myapp.config</param-value>
</context-param>
The ContextConfig class is in that package
Any idea why is this happening?
The exception is not related to the MyBean class. The exception says:
No qualifying bean of type [javax.validation.ValidatorFactory] found for dependency
Spring failed to inject javax.validation.ValidatorFactory to the HomeController class. Check that configuration for ValidatorFactory class.
Thanks for the response Ilya.
You´re right, the stack I pasted was not the correct one. The cause was that to simplify, I gave the example with MyBean and I forgot to rename it in the stack.
The problem has been solved. The real name of MyBean is MessageResolver and what was happening was that I already had another method called messageResolver inside the ContextConfig class, used to create a bean for Thymeleaf. The difference is that mine does not receive parameters and the other receives a MessageSource, BUT...Reading the Spring docmentation I found this:
Spring Doc Quote:
2.2.6. Customizing bean naming
By default, JavaConfig uses a #Bean method's name as the name of the
resulting bean. This functionality can be overridden, however, using
the BeanNamingStrategy extension point.
Spring #Bean doc
So, the problem is that the bean was not being created because it was being overrided its definition.
The error disappears just renaming the messageResolver method.
Thanks again. Regards!

Categories