2
respostas

Error creating autowired UsuárioDAO

Ao tentar dar o @Autowired no UsuárioDAO, o console me responde dessa forma. Li algumas coisas no forúm, mas não consegui resolver. Alguém poderia me ajudar?

GRAVE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfiguration': Unsatisfied dependency expressed through field 'usuarioDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'br.com.casadocodigo.loja.daos.UsuarioDAO' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5116) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5634) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561) 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.NoSuchBeanDefinitionException: No qualifying bean of type 'br.com.casadocodigo.loja.daos.UsuarioDAO' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ... 24 more

2 respostas

package br.com.casadocodigo.loja.daos;

import java.util.List;

import javax.persistence.EntityManager; import javax.persistence.PersistenceContext;

import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Repository;

import br.com.casadocodigo.loja.models.Usuario;

@Repository public class UsuarioDAO implements UserDetailsService {

@PersistenceContext private EntityManager manager;

public Usuario loadUserByUsername(String email) { List usuarios = manager .createQuery("select u from Usuario u where u.email = :email", Usuario.class) .setParameter("email", email) .getResultList();

if (usuarios.isEmpty()) { throw new UsernameNotFoundException("Usuário " + email + " não foi encontrado"); }

return usuarios.get(0); }

public void gravar(Usuario usuario) { manager.persist(usuario); }

}

package br.com.casadocodigo.loja.models;

import java.util.ArrayList; import java.util.Collection; import java.util.List;

import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.ManyToMany;

import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.userdetails.UserDetails;

@Entity public class Usuario implements UserDetails {

private static final long serialVersionUID = 1L;

@Id private String email;

private String senha;

private String nome;

@ManyToMany(fetch=FetchType.EAGER, cascade=CascadeType.PERSIST) private List roles = new ArrayList();

public String getEmail() { return email; }

public void setEmail(String email) { this.email = email; }

public String getSenha() { return senha; }

public void setSenha(String senha) { this.senha = senha; }

public String getNome() { return nome; }

public void setNome(String nome) { this.nome = nome; }

public List getRoles() { return roles; }

public void setRoles(List roles) { this.roles = roles; }

@Override public Collection<? extends GrantedAuthority> getAuthorities() { return this.roles; }

@Override public String getPassword() { return this.senha; }

@Override public String getUsername() { return this.email; }

@Override public boolean isAccountNonExpired() { return true; }

@Override public boolean isAccountNonLocked() { return true; }

@Override public boolean isCredentialsNonExpired() { return true; }

@Override public boolean isEnabled() { return true; }

}

Bom dia Luan, posso ver suas classes de configuração?