Reputation: 53
I write some Rest app via spring ioc. But i cant solve this problem. Here is my exception:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'angularController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.akifev.dao.ContactDao com.akifev.controllers.AngularController.contactDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.akifev.dao.ContactDao] 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)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at com.akifev.controllers.AngularRun.main(AngularRun.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.boot.maven.RunMojo$LaunchRunner.run(RunMojo.java:418)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.akifev.dao.ContactDao com.akifev.controllers.AngularController.contactDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.akifev.dao.ContactDao] 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)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 22 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.akifev.dao.ContactDao] 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:1301)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 24 common frames omitted
My applicationContext.xml(all my dao classes is in "com.akifev.dao"):
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:tx="http://www.springframework.org/schema/cache" xmlns:mvc="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.akifev.dao"/>
<context:property-placeholder location="classpath:database.properties"/>
<mongo:mongo host="${mongo.host}" port="${mongo.port}"/>
<mongo:db-factory
dbname="${mongo.dbname}"
mongo-ref="mongo"
/>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>
</beans>
One of my DAOs:
@Repository
public class ContactDao {
@Autowired
MongoOperations mongoOperations;
public void save(Contact contact) {
mongoOperations.save(contact);
}
public Contact get(Long id) {
return mongoOperations.findOne(Query.query(Criteria.where("id").is(id)), Contact.class);
}
public List<Contact> getAll() {
return mongoOperations.findAll(Contact.class);
}
public void remove(Long id) {
mongoOperations.remove(Query.query(Criteria.where("id").is(id)), Contact.class);
}
}
My RestController:
@RestController
public class AngularController {
@Autowired
ContactDao contactDao;
@RequestMapping("/hola")
public String angularController(@RequestParam(value = "user", defaultValue = "user") String name){
//code for add the entry to database
return "added";
}
}
I tried annotate it with @Qualifer but it is not use. Can somebody help me? Thanks you for attention.
Upvotes: 0
Views: 1762
Reputation: 53
Problem solved.
Annotation which solved my problem is @ImportResources
.
Upvotes: 3
Reputation: 2541
there is no bean like mongoOperations declared in .xml, try update the repository DAO with
@Autowired
MongoTemplate mongoTemplate;
Upvotes: 0
Reputation: 461
Try setting up an interface that ContactDao should implement. For example
interface DAO {
void save(Contact contact);
Contact get(Long id);
...
}
and then autowire that interface in your controller.
public class AngularController {
@Autowired
DAO dao
}
Make sure you're also importing the right @Repository annotation also and you're next getting it mixed up with something else
Upvotes: 0
Reputation: 106500
It could be that my Spring is a bit rusty, but you don't have your ContactDao
defined in your beans anywhere. You should define it:
<bean id="contactDao" class="com.akifev.dao.ContactDao" />
Adding it to the component scan would work as well.
Upvotes: 0