I have the following repository, as simple as this:
package br.com.portal.repository;
public interface UserRepository extends CrudRepository<User, Long> {
@Query("SELECT u FROM User WHERE u.login = :login")
User findByLogin(@Param("login") String login);
}
Here, it's supposed to inherit all the common crud operations defined in CrudRepository
and also expose the findByLogin
function.
Most examples, if not all, do not annotate such repository with the @Repository
annotation. Why is that? Is there a need to implement this interface or does the @Query
somehow does it behind the scenes?
Here is what I currently have:
package br.com.portal.service;
public interface UserService {
User findByLogin(String login);
}
@Service
public class UserServiceImpl implements UserService {
private UserRepository repository;
@Autowired
public UserServiceImpl(UserRepository repository) {
this.repository = repository;
}
User findByLogin(String login) {
return repository.findByLogin(login);
}
}
And the spring-mvc.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- Defines the static resources location, otherwise resource requests will result in 404 errors (not found) -->
<mvc:resources mapping="/assets/**" location="/assets/" order="0" cache-period="31556926" />
<mvc:resources mapping="/favicon.ico" location="/assets/icon/favicon.ico" cache-period="31556926" />
<!-- Defines the custom Spring components packages -->
<context:component-scan base-package="br.com.portal.repository">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
</context:component-scan>
<context:component-scan base-package="br.com.portal.service">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
<context:component-scan base-package="br.com.portal.controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- JPA -->
<bean class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" id="entityManagerFactory">
<property name="persistenceUnitName" value="default" />
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
I am not using Spring Boot.
With the current above informations, we should be able to reproduce the following error:
Error creating bean with name 'userServiceImpl' defined in file xxx: Unsatisfied dependency expressed through constructor parameter 0; nested exception is NoSuchBeanDefinitionException: No qualifying bean of type 'UserRepository'
Am I missing something?