2016-03-23 5 views
6

autowiring मैं अपने Springboot आवेदन की following code structure है:Springboot CrudRepository त्रुटि

structure

मैं सेम UserDao के लिए NoSuchBeanDefinitionException के अपवाद हो रही है।

अपवाद ट्रेस: ​​

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.matlb.dao.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotat`enter code here`ion.Autowired(required=true)} 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    ... 37 common frames omitted 

UserDao.java इंटरफेस के स्रोत कोड

package com.matlb.dao; 

import com.matlb.domain.User; 
import org.springframework.data.repository.CrudRepository; 
import org.springframework.stereotype.Repository; 

@Repository 
public interface UserDao extends CrudRepository<User,Integer>{ 

    User findByEmail(String email); 
} 

है और मैं इसे यहाँ autowiring हूँ

package com.matlb.service; 

import com.matlb.dao.UserDao; 
import com.matlb.domain.User; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 

import java.util.List; 

@Service 
public class UserServiceImpl implements UserService { 

    @Autowired 
    private UserDao userDao; 

    @Override 
    public List<User> findAll() { 
     return (List<User>) getUserDao().findAll(); 
    } 

    @Override 
    public User createUser(String email) { 
     User user = new User(email); 
     return saveUser(user); 
    } 

    @Override 
    public User findById(Integer userId) { 
     return getUserDao().findOne(userId); 
    } 

    @Override 
    public User saveUser(User user) { 
     return getUserDao().save(user); 
    } 

    @Override 
    public User findByEmail(String email) { 
     return getUserDao().findByEmail(email); 
    } 

    @Override 
    public void delete(Integer userId) { 
     getUserDao().delete(userId); 
    } 

    public UserDao getUserDao() { 
     return userDao; 
    } 
} 

मुख्य वर्ग

के स्रोत
package com.matlb; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 

@SpringBootApplication 
public class MatlbApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(MatlbApplication.class, args); 
    } 
} 

build.gradle

buildscript { 
    ext { 
     springBootVersion = '1.3.3.RELEASE' 
    } 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
     classpath("org.springframework:springloaded:1.2.1.RELEASE") 
    } 
} 

apply plugin: 'java' 
apply plugin: 'eclipse' 
apply plugin: 'idea' 
apply plugin: 'spring-boot' 

jar { 
    baseName = 'demo' 
    version = '0.0.1-SNAPSHOT' 
} 
sourceCompatibility = 1.8 
targetCompatibility = 1.8 

repositories { 
    mavenCentral() 
} 


dependencies { 
    compile('org.springframework.boot:spring-boot-starter-aop') 
    compile('org.springframework.boot:spring-boot-starter-web') 
    compile("org.springframework.boot:spring-boot-devtools") 
    compile("org.springframework.boot:spring-boot-starter-data-jpa") 
    runtime('mysql:mysql-connector-java') 
    testCompile('org.springframework.boot:spring-boot-starter-test') 
    testCompile('org.springframework.restdocs:spring-restdocs-mockmvc') 
} 

idea { 
    module { 
     inheritOutputDirs = false 
     outputDir = file("$buildDir/classes/main/") 
    } 
} 

eclipse { 
    classpath { 
     containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER') 
     containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8' 
    } 
} 

task wrapper(type: Wrapper) { 
    gradleVersion = '2.9' 
} 

कृपया मुझे पता है जहाँ मैं गलत कर रहा हूँ। मैं इंटेलिज को आईडीई के रूप में उपयोग कर रहा हूं और इसकी दिखावट बीन के रूप में बनाई गई है जब मैं @Repository एनोटेशन का उपयोग करता हूं।

+2

आपका 'UserDao' सिर्फ एक सादे' CrudRepository' है - नहीं एक 'JpaRepository',' MongoRepository', या अन्य प्रकार है कि वसंत बताता है जो विशिष्ट बैकएंड उपयोग करने के लिए। – chrylis

+0

क्या यह अपवाद पैदा कर रहा है? – Prashant

+0

यह एक शुरुआत है। आपको '@ EnableFooRepositories' एनोटेशन की भी आवश्यकता हो सकती है। – chrylis

उत्तर

8

सबसे पहले, आपको अपने डीएओ इंटरफ़ेस से @Repository को हटाने की आवश्यकता है। स्प्रिंग डेटा जेपीए कार्यान्वयन का निर्माण करेगा और स्प्रिंग कंटेनर में @ रिपोजिटरी के बिना इसे तैनात करेगा। यह @EnableJpaRepository है जो स्ट्रिंग डेटा जेपीए को निर्देश देगा। स्प्रिंग बूट स्वत: कॉन्फ़िगरेशन आपके लिए @ EnableJpaRepository घोषित करेगा।

फिर, JpaRepository द्वारा CrudRepository को प्रतिस्थापित करें।

अंत में, सुनिश्चित करें कि आपने spring-boot-starter-data-jpa को मैवेन निर्भरता के रूप में घोषित किया है।

सादर, डैनियल

+0

किसी भी तरह से मैं JpaRepository आयात करने में असमर्थ हूं। मैं निर्भरता { \t संकलन उपयोग कर रहा हूँ ('org.springframework.boot: वसंत-बूट स्टार्टर-aop') \t संकलन ('org.springframework.boot: वसंत-बूट स्टार्टर-वेब') \t संकलन ("org.springframework.boot: spring-boot-devtools") संकलन ("org.springframework.boot: spring-boot-starter-data-jpa") \t रनटाइम ('mysql: mysql-connector-java') \t testCompile ('org.springframework.boot: वसंत-बूट स्टार्टर परीक्षण') \t testCompile ('org.springframework.restdocs:-mockmvc वसंत-restdocs') } bui में ld.gradle। – Prashant

+0

निश्चित रूप से, मैं प्रश्न संपादित कर रहा हूं। – Prashant

+0

ऐसा लगता है कि आपके पास एक समस्या है। चेकआउट आप निर्भरता पेड़ और सुनिश्चित करें कि वसंत-डेटा-जेपीए इसका हिस्सा है। –

संबंधित मुद्दे