2016-03-02 75 views
1

我想使用mongodb實現泛型DAO。我的觀點不是隻創建一個存儲庫。這樣的代碼是實用和對象。我能夠創建幾個類和interfesów,但得到錯誤。SpringBoot使用MongoDB實現泛型DAO

我的代碼:

Dao.java

public interface Dao<T, ID extends Serializable> extends MongoRepository<T, ID>{ 


} 

NewsDao.java

public interface NewsDao extends Dao<News, Long> { 


} 

AbstractService.java

public abstract class AbstractService<T, ID extends Serializable> implements 
     Service<T, ID> { 

    protected final Logger logger = LoggerFactory.getLogger(getClass()); 
    protected Dao<T, ID> dao; 

    public AbstractService(Dao<T, ID> dao) { 
     this.dao = dao; 
    } 

    @Override 
    public T save(T entity) { 
     this.logger.debug("Create a new {} with information: {}", entity.getClass(), 
       entity.toString()); 
     return this.dao.save(entity); 
    } 

} 

NewsService.java

@Service 
public class NewsService extends AbstractService<News, Long> { 

    @Autowired 
    public NewsService(NewsDao moduleDao) { 
     super(moduleDao); 
    } 

} 

Service.java

public interface Service<T, ID extends Serializable> { 

    T save(T entity); 

} 

NewsController.java

@RestController 
@RequestMapping("/news") 
public class NewsController { 

    private final NewsService newsService; 

    @Autowired 
    public NewsController(NewsService newsService) { 
     this.newsService = newsService; 
    } 

    @RequestMapping(method = RequestMethod.GET) 
    public void save(){ 
     newsService.save(new News("elo", "kot", "cos")); 
    } 
} 

錯誤:

2016-03-02 19:04:11.509 WARN 4476 --- [   main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'newsService' defined in file [C:\Users\Lukasz\IdeaProjects\NewsSystem_REST\build\classes\main\com\newssystem\lab\dao\NewsService.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.newssystem.lab.dao.NewsDao]: : No qualifying bean of type [com.newssystem.lab.dao.NewsDao] 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.newssystem.lab.dao.NewsDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 
2016-03-02 19:04:11.516 INFO 4476 --- [   main] o.apache.catalina.core.StandardService : Stopping service Tomcat 
2016-03-02 19:04:11.538 ERROR 4476 --- [   main] o.s.boot.SpringApplication    : Application startup failed 

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'newsService' defined in file [C:\Users\Lukasz\IdeaProjects\NewsSystem_REST\build\classes\main\com\newssystem\lab\dao\NewsService.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.newssystem.lab.dao.NewsDao]: : No qualifying bean of type [com.newssystem.lab.dao.NewsDao] 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.newssystem.lab.dao.NewsDao] 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) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE] 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE] 
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE] 
    at com.newssystem.lab.NewsSystemApplication.main(NewsSystemApplication.java:24) [main/:na] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_25] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_25] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_25] 
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_25] 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) [idea_rt.jar:na] 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.newssystem.lab.dao.NewsDao] 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) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    ... 24 common frames omitted 

我要以正確的方式?

+0

我把@Repository註釋給Dao.java,但是我仍然有錯誤。 – lukassz

+1

試試我的答案,讓我知道它是否可行 – Pragnani

+0

這是作品,謝謝!我想添加新的類「評論」,我可以創建一個服務,一個將被全部服務的存儲庫?所有人的一個實施? – lukassz

回答

1

試試這個

春數據無法找到你的蒙戈包庫 告訴春天有關存儲庫位置

,你應該在你的SpringBootCofiguration文件

+0

這是作品,謝謝。我忘了這個。我的解決方案是否實用?我想創建一個「評論」類。如何做到不讓她成爲額外的存儲庫?有一種方法可以創建一個通用的單一存儲庫? – lukassz

+1

不,您不需要額外的界面。這也是您現在具有特定界面的最佳實踐。在另一個說明,如果你可以有一個通用的服務類,你做錯了什麼imho ... –

+0

@ M.Deinum我已經讀過你的博客,它是非常豐富 – Pragnani

0

是否需要定義一個bean,指定正確的impl?

類似:

@Configuration 
class MyConfig{ 
    public NewsService NewsService() { return new Newservice(); } 

並導入這個配置?

+0

我一直在創建類的對象,不能以這種方式做到這一點? – lukassz

+0

請忽略我的答案,它看起來像我想知道,並沒有拿起你的DAO,我認爲它沒有拿起服務。 – berlinguyinca

0

使用

@EnableMongoRepositories(basePackages="com.newssystem.lab.dao") 

最好的方法是:

@NoRepositoryBean 
public interface Dao<T, ID extends Serializable> extends MongoRepository<T, ID>{ 


} 

這就是你必須要做的。否則,Spring會認爲它是一個bean。