Fala galera blz ?
Seguinte: Estou recebendo um NullPointer ao tentar injetar um dao em um validator, o que será que estou fazendo de errado, seguem os códigos que estou utilizando:
Classe AppWebConfigurator
@EnableWebMvc
@ComponentScan(basePackageClasses = { HomeController.class, ProdutoDAO.class, FileSaver.class, CarrinhoCompras.class,
UsuarioValidator.class })
@EnableCaching
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class AppWebConfiguration extends WebMvcConfigurerAdapter {
@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
// resolver.setExposeContextBeansAsAttributes(true);
resolver.setExposedContextBeanNames("carrinhoCompras");
return resolver;
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("/WEB-INF/messages");
messageSource.setDefaultEncoding("UTF-8");
messageSource.setCacheSeconds(1);
return messageSource;
}
@Bean
public FormattingConversionService mvcConversionService() {
DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
DateFormatterRegistrar registrar = new DateFormatterRegistrar();
registrar.setFormatter(new DateFormatter("dd/MM/yyyy"));
registrar.registerFormatters(conversionService);
return conversionService;
}
@Bean
public MultipartResolver multipartResolver() {
return new StandardServletMultipartResolver();
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
public CacheManager cacheManger() {
CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder().maximumSize(100).expireAfterAccess(5,
TimeUnit.MINUTES);
GuavaCacheManager manager = new GuavaCacheManager();
manager.setCacheBuilder(builder);
return manager; // new ConcurrentMapCacheManager();
}
@Bean
public ViewResolver contentNegotiationViewResolver(ContentNegotiationManager manager) {
List<ViewResolver> viewResolvers = new ArrayList<>();
viewResolvers.add(internalResourceViewResolver());
viewResolvers.add(new JsonViewResolver());
ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
resolver.setViewResolvers(viewResolvers);
resolver.setContentNegotiationManager(manager);
return resolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LocaleChangeInterceptor());
}
@Bean
public LocaleResolver localeResolver() {
return new CookieLocaleResolver();
}
@Bean
public MailSender mailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("seu servidor smtp");// por exemplo smtp.gmail.com
mailSender.setUsername("seu email");
mailSender.setPassword("sua senha");
mailSender.setPort(587);
Properties mailProperties = new Properties();
mailProperties.setProperty("mail.smtp.auth", "true");
mailProperties.setProperty("mail.smtp.starttls.enable", "true");
mailSender.setJavaMailProperties(mailProperties);
return mailSender;
}
}
Classe UsuarioValidator
@Component
public class UsuarioValidator implements Validator {
@Autowired
private UsuarioDAO dao;
@Override
public boolean supports(Class<?> clazz) {
return Usuario.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmpty(errors, "nome", "field.required");
ValidationUtils.rejectIfEmpty(errors, "email", "field.required");
ValidationUtils.rejectIfEmpty(errors, "senha", "field.required");
ValidationUtils.rejectIfEmpty(errors, "password", "field.required");
Usuario usuario = (Usuario) target;
if (usuario.getSenha().length() < 5) {
errors.rejectValue("senha", "notMatch.passwordSize");
}
if (!usuario.getSenha().equals(usuario.getConfirmPassword())) {
errors.rejectValue("senha", "notMatch.confirmPassword");
errors.rejectValue("confirmPassword", "notMatch.confirmPassword");
}
String email = usuario.getEmail();
if (isUser(email)) {
System.out.println("OK");
}
}
private boolean isUser(String email) {
System.out.println("M");
Usuario usuario = dao.loadUserByUsername(email);
if (usuario != null) {
return false;
} else {
return true;
}
}
}
Grato