2017-03-03 5 views
0

は、私は私のUserControllerではエラー名でBeanを作成する「UserServiceの」:不満依存関係がフィールドを通して表現「userJpaRepository」

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userJpaRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'net.codejava.spring.repository.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=userJpaRepository)} 

のメインページにアクセスもしようとしたとき、私は次のコード

を持って、次のエラーが発生しています
@Controller 
public class UserController { 

    private UserService employeeServiceImpl; 

    @RequestMapping("/") 
    public String employee() { 
     this.employeeServiceImpl.listAlUsers(); 
     return "employee"; 
    } 

    @Autowired(required = true) 
    public void setEmployeeService(UserService employeeServiceImpl) { 
     this.employeeServiceImpl = employeeServiceImpl; 
    } 

} 

マイUserServiceの

public interface UserService { 
    public abstract List<User> listAlUsers(); 
    public abstract User addUser(User user); 
    public abstract int removeUser(int id); 
    public abstract User updateUser(User user); 
} 

マイUserServiceImpl

@Service("userService") 
public class UserServiceImpl implements UserService { 

    @Autowired 
    @Qualifier("userJpaRepository") 
    private UserRepository userJpaRepository; 

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

    @Override 
    public User addUser(User user) { 
     return userJpaRepository.save(user); 
    } 

    @Override 
    public int removeUser(int id) { 
     userJpaRepository.delete(id); 
     return 0; 
    } 

    @Override 
    public User updateUser(User user) { 
     return userJpaRepository.save(user); 
    } 
} 

マイJpaRepository

@Repository("userJpaRepository") 
public interface UserRepository extends JpaRepository<User,Serializable> { 

} 

web.xmlの

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/root-context.xml</param-value> 
    </context-param> 

    <!-- Creates the Spring Container shared by all Servlets and Filters --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Processes application requests --> 
    <servlet> 
     <servlet-name>appServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>appServlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

私のサーブレット・コンテキスト

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 

    <!--The <context:component-scan...> tag will be use to activate Spring MVC annotation scanning capability which allows to make use of annotations like @Controller and @RequestMapping etc.--> 
    <!--Step 1 : HandlerMapping --> 
    <context:component-scan base-package="com.loiane.controller" /> 

    <!--JPA Repository--> 

    <jpa:repositories base-package="com.loiane.repository.EmployeeJpaRepository"/> 

    <mvc:annotation-driven /> 

    <!--Step 3 : View Resolver--> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix"> 
      <value>/WEB-INF/views/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 


    <!--Bundles--> 
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
     <property name="basename"> 
      <value>messages</value> 
     </property> 
    </bean> 

    <!--Declaramos el interceptor para permitir el cambio de idioa en tiempo de ejecucion--> 
    <mvc:interceptors> 
     <mvc:interceptor> 
      <mvc:mapping path="/**" /> 
      <bean id="localeChangeInterceptor" 
        class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> 
       <property name="paramName" value="lang" /> 
      </bean> 
     </mvc:interceptor> 
    </mvc:interceptors> 

    <!--Si el idioma no existe cargamos el en--> 

    <bean id="localeResolver" 
      class="org.springframework.web.servlet.i18n.CookieLocaleResolver"> 
     <property name="cookieName" value="lang" /> 
     <property name="defaultLocale" value="en" /> 
    </bean> 

</beans> 

ルートコンテキストは空です。

どこがエラーの原因なのか分かりません。私はdifferentsオプションを見つけようとしますが、DAOパターンを使用すると同じエラーが発生しますので、問題を解決するために問題があることがわかります。

プロジェクトの設定はxmlで行われていますが、この状況を解決することは重要ではないと思います。

よろしくお願いいたします。

+0

XMLは、この問題を解決することが非常に重要である、あなたの春のコンテキスト内でスキャンするためにパッケージを追加する限り、その定義済みのリポジトリインタフェース、インタフェースをスキャンするのスプリングを有効にするにはリポジトリがautowiredになっていないので、投稿してください。 xml構成を使用したい理由がわからない場所で注釈を使用しています。 – lsiva

+0

正しい、私はこれを使用するために注釈を使用しています、私はこのXMLの理由は、XMLに宣言されていないと思う、私は眠る必要があります... haha​​a – jc1992

+0

なぜ '

答えて

0

リポジトリインタフェース@Repository("userJpaRepository")でBean修飾子名を使用しています。修飾子の名前は、2つ以上のクラスが共通インターフェースを実装している場合に便利です。これは、特定のbeanを解決するのに役立ちます。これは、他のbeanのインターフェース変数にautowiredされることになります。それをインタフェース上で宣言することは、その目的を破るでしょう。修飾子名を削除すると、他のクラス(名前がuserRepositoryImplのバネ作成されたプロキシBean以外)がUserRepositoryインタフェースを実装していない場合に機能します。

@Autowired 
private UserRepository userJpaRepository; 

と整数またはIdclassなどのID列のデータ型を持つ直列化を交換してください。

@Repository 
public interface UserRepository extends JpaRepository<User,Integer> { 

} 

問題があるためであるとして

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.xsd 
    http://www.springframework.org/schema/data/jpa 
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> 

    <jpa:repositories base-package="net.codejava.spring.repository"/> 
関連する問題