Fórum Problemas com anotação spring 4 (Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException) #557231
16/06/2016
0
Olá pessoal
alguem please pode me dar um help :)
eu implementei o spring da seguinte forma abaixo:
spring.xml
spring-mvc
LoginInterceptor.class
UserService.class
estou com um probleminha com anotações spring, estou criando um login e estou recebendo esse erro abaixo ao rodar
alguem please pode me dar um help :)
ERRO
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.project.repository.UserRepository com.project.service.UserService.userRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.project.repository.UserRepository] 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)}
eu implementei o spring da seguinte forma abaixo:
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.project.controller" />
<context:component-scan base-package="com.project.model" />
<context:component-scan base-package="com.project.repository" />
<context:component-scan base-package="com.project.service" />
<!-- Diz ao Spring que ele deve usar a configuração das annotations -->
<mvc:annotation-driven />
<!-- Define pagina inicial (ingnora a configuração do web.xml) -->
<mvc:view-controller path="/" view-name="ola" />
<!-- SpringMVC -->
<import resource="spring-mvc.xml"/>
</beans>spring-mvc
<?xml version="1.0" encoding="UTF-8"?>
<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"
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">
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<!-- Login Interceptor -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/protected/**"/>
<bean class="com.project.interceptor.LoginInterceptor"/>
</mvc:interceptor>
<!-- workaround to fix IE8 problem -->
<bean id="webContentInterceptor"
class="org.springframework.web.servlet.mvc.WebContentInterceptor">
<property name="cacheSeconds" value="0"/>
<property name="useExpiresHeader" value="true"/>
<property name="useCacheControlHeader" value="true"/>
<property name="useCacheControlNoStore" value="true"/>
</bean>
</mvc:interceptors>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
</bean>
</beans> LoginInterceptor.class
public class LoginInterceptor extends HandlerInterceptorAdapter {
@Autowired
private UserService userService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
if(user == null){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String email = auth.getName();
user = userService.findByEmail(email);
session.setAttribute("user", user);
}
return super.preHandle(request, response, handler);
}
}
UserService.class
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public User findByEmail(String email){
return userRepository.findByEmail(email);
}
}Leonardo Silva
Curtir tópico
+ 0
Responder
Post mais votado
19/03/2018
Ola !
Estou passando pelo mesmo problema no meu código, qual foi a solução que vc criou ?
Obrigado
Estou passando pelo mesmo problema no meu código, qual foi a solução que vc criou ?
Obrigado
Gladson Reis
Responder
Gostei + 1
Mais Posts
16/06/2016
Leonardo Silva
Consegui corrigir, era problema no spring-mvc com as anotaçoes
Responder
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)