Brak możlwości wstrzyknięcia klasy DAO

0

Witam mam problem ze wstrzyknięciem klasy DAO wyrzuca mi takie coś:
Analizując logi wychodzi na tą że totalnie nie widzi mojego obiektu DAO podczas dodania do adnotacji Autowired(required=false) przechodzi więc wychodziło by że tam jest null mimo. Nie mam pojęcia dlaczego tak sie dzieje połączenie z bazą danych mam bo generuje mi tabele takie jak mam encje itd, inne obiekty mogę wstrzykiwać za pomocą @Autowired totalnie nie mam pojęcie o co chodzi
W pliku xml kontekstu springa mam zadeklarowane to co powinno wystarczyć według mnie :

	<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd">
        

	<mvc:annotation-driven />
	<context:annotation-config/>
	<context:component-scan base-package="com.app.*" />		  
	<mvc:resources mapping="/resources/**" location="/resources/" />
	

	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/WEB-INF/"
		p:suffix=".jsp"/>

</beans>

Klasa DAO:

 
package com.app.dao;




import org.springframework.data.repository.CrudRepository;

import com.app.dto.UserDTO;

public interface UserDAO extends CrudRepository<UserDTO, Integer> {

}

Klasa kontrolera

package com.app.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class UserController {
	
	@Autowired 
	UserDAO userDao;

	public UserController(){
        }	

} 

LOGI Z SERWERA TOMCAT:

 
ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.app.dao.UserDAO com.app.controllers.UserController.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.app.dao.UserDAO] 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:287)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
	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(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.BeanCreationException: Could not autowire field: com.app.dao.UserDAO com.app.controllers.UserController.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.app.dao.UserDAO] 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:506)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
	... 22 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.app.dao.UserDAO] 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:924)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
	... 24 more
lis 09, 2014 8:16:34 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: 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 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.app.dao.UserDAO com.app.controllers.UserController.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.app.dao.UserDAO] 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:287)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
	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(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.BeanCreationException: Could not autowire field: com.app.dao.UserDAO com.app.controllers.UserController.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.app.dao.UserDAO] 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:506)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
	... 22 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.app.dao.UserDAO] 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:924)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
	... 24 more

Ma ktoś propozycje/ przyczynę dlaczego niechce mi wstrzyknąć tego DAO?

0

Ale WTF? Przeciez to twoje Dao to INTERFEJS. Wstrzyknąć możesz tylko OBIEKT, czyli IMPLEMENTACJE. Wstrzykuje się zwykle poprzez interfejs z pewnych względów, ale nie zmienia to faktu że musisz mieć klasę która taki interfejs implementuje i którą spring będzie mógł instancjonować. Jakim cudem zabierasz się za Springa skoro nie umiesz absolutnych podstaw javy? o_O

0

Ale on używa Spring Data, więc nie musi mieć własnej implementacji interfejsu CrudRepository, bo takowa jest dostarczana przez Springa. Wg. mnie jak zwykle problem leży w xmlu, ale nie chce mi się po raz enty patrzeć na te same błędy...

EDIT:
A powiedziałeś Springowi gdzie leżą twoje repository???

1

na moje oko brakuje tego:
<jpa:repositories base-package="sciezka.do.twojego.userrepo" />

0

@NoZi ale czy czasem w konfiguracji nie brakuje w takim razie

  <repositories base-package="cośtam.cośtam" />

?

0

Dlatego przy edicie napisałem ;) Moim zdaniem brakuje ;)

Z dokumentacji.

 <jpa:repositories base-package="twoj.package.repository" />

Polecam, Nozi:
http://docs.spring.io/spring-data/jpa/docs/1.7.1.RELEASE/reference/html/

0

Ale WTF? Przeciez to twoje Dao to INTERFEJS. Wstrzyknąć możesz tylko OBIEKT, czyli IMPLEMENTACJE. Wstrzykuje się zwykle poprzez interfejs z pewnych względów, ale nie zmienia to faktu że musisz mieć klasę która taki interfejs implementuje i którą spring będzie mógł instancjonować. Jakim cudem zabierasz się za Springa skoro nie umiesz absolutnych podstaw javy? o_O

Odpowiedz padła od NoZi'ego.

Nozi oczywiście robię to tylko zapomniałem dokleić jednego xml oto i on:

 
<?xml version="1.0" encoding="UTF-8"?>
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
        xmlns:jpa="http://www.springframework.org/schema/data/jpa"
        xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
			http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd
        	http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        ">
        


  
	<jpa:repositories base-package="app.com.*"></jpa:repositories>	

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://127.0.0.1:8081/app" />
        <property name="username" value="root" />
        <property name="password" value="admin" />
    </bean>
    
    
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.app.*" />

        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="database" value="MYSQL" />
                <property name="generateDdl" value="true" />
                <property name="showSql" value="true"/>
            </bean>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
0

Też jestem zdania, że brakuje:

<jpa:repositories base-package="path.to.your.repositories"/>

Tu masz przykład konfiguracji spring data w xml:
https://github.com/DanielMichalski/asco-webshop/blob/master/asco-web/src/main/resources/spring/spring-store.xml

0

Dalej to samo nawet jak dałem bezpośrednio do klasy
@up dzięki za link zabieram sie do analizy co do porad dałęm:
<jpa:repositories base-package="app.com.dao.UserDAO"></jpa:repositories>
<jpa:repositories base-package="app.com.dao"></jpa:repositories>
niestety dalej to samo

0

Nie miałeś dać ścieżki do klasy, tylko pakiet w którym są wszystkie interfejsy/implementacje korzystające ze Spring Data JPA

0

w pierwszym poście zmień

<context:component-scan base-package="com.app.*" />   

na np. <context:component-scan base-package="com.app" />

0

clear

0

Przerzuć

<jpa:repositories base-package="app.com.dao"></jpa:repositories>

do servletu, a nie do core

0

niech servlet wyglada tak:


<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd">
        

	<mvc:annotation-driven />
	<context:annotation-config/>
	<context:component-scan base-package="com.app.*" />		  
	<mvc:resources mapping="/resources/**" location="/resources/" />
        <jpa:repositories base-package="app.com.dao"/>
	

	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/WEB-INF/"
		p:suffix=".jsp"/>

</beans>

A z core wywal te linijke z jpa repositories

0

na początku miałem myśl tyczącą się nazwy tego:

@Autowired
UserDAO userDao; 

Może chodzi tu o nazewnictwo i spring nie łapie.. może daj nazwe jako userDAO, zamiast userDao?

Można też użyć @Qualifier tylko musiałbyś najpierw nazwać jakoś to repo (bo szczerze mówiąc nie wiem jak sie defaultowo nazywa)

1

tak przeglądam projekt i pozwoliłem sobie zauważyć że w UserServiceImpl wstrzykujesz UserServiceImpl, a nigdzie nie masz zadeklarowane ani UserService ani UserServiceImpl jako bean. (chyba że ślepy jestem i nie moge znalezc)

3

STARY!!!!!!!!
MASZ

<jpa:repositories base-package="app.com.dao"/>

A POWINNO BYĆ

<jpa:repositories base-package="com.app.dao"/>

Dodatkowo w app-core.xml powinno być

<property name="packagesToScan" value="com.app.dto" />

Poza tym, usuń ten projekt, i zacznij jeszcze raz. Klasa

UserDTO

w pakiecie com.app.dto

 jest oznaczona @Entity, a w pakiecie <code class="java">com.app.model

masz klasę User która rozszerza UserDTO. To jest WTF

TO:

  <filter>
        <filter-name>openEntityManagerInViewFilter</filter-name>
        <filter-class>
            org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
        </filter-class>
        <init-param>
            <param-name>entityManagerFactoryBeanName</param-name>
            <param-value>entityManagerFactory</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>openEntityManagerInViewFilter</filter-name>
        <url-pattern>/</url-pattern>
    </filter-mapping>

też jest do wyrzucenia

1

Problem rozwiązany przez NoZi Podziękowania także dla azalut za zainteresowanie sprawą, skorzystałem z forum 1 raz i nie sądziłem, że tak szybko uzyskam pomoc w problemie który tak naprawdę sam stworzyłem... Dzięki chłopaki za pomoc Pozdrawiam
Temat do zamknięcia

0

Jaki agent napisał posta żeby go odznaczyć jako odpowiedź. (facepalm)

1 użytkowników online, w tym zalogowanych: 0, gości: 1