我知道这个问题已经在Stackoverflow中得到解决/回答了很多次,在我阅读了大约85%的问题(包括重复项)后,我仍然找不到与我的案例匹配的内容:
我有以下存储库界面:
@Component
public interface IUserRepository {
Boolean checkIfDuplicateName(String String);
}
该接口具有Repository实现:
@Repository("userRepositoryImpl")
public class UserRepositoryImpl extends RepositoryImpl implements
ApplicationEventPublisherAware, ApplicationContextAware,
SessionFactoryAwareNode, IUserRepository {
@Autowired
private SessionFactory sessionFactory;
ApplicationContext context;
ApplicationEventPublisher eventPublisher;
static Logger log = Logger.getLogger(UserRepositoryImpl.class.getName());
public UserRepositoryImpl() {
}
public String eventTestPublish() {
return "test of the event publisher finished succesefully";
}
@Override
public void setApplicationEventPublisher(
ApplicationEventPublisher applicationEventPublisher) {
this.eventPublisher = applicationEventPublisher;
}
@Override
public void setApplicationContext(ApplicationContext context)
throws BeansException {
this.context = context;
}
public SessionFactory getSessionFactory() {
return super.getSessionFactory();
}
@Override
public void setSessionFactory(SessionFactoryImplementor sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public Boolean checkIfDuplicateName(String userName) {
....
}
}
有一个服务类:
@Service("userServiceImpl")
@Transactional
public class UserServiceImpl extends ServiceImpl implements ApplicationEventPublisherAware,
ApplicationContextAware,
SessionFactoryAwareNode,
IUserService {
@Autowired
public IUserRepository iuserRepository;
static Logger log = Logger.getLogger(UserServiceImpl.class.getName());
ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
Validator validator = vf.getValidator();
ExecutableValidator executableValidator = validator.forExecutables();
ApplicationContext context;
SessionFactory sessionFactory;
ApplicationEventPublisher eventPublisher;
public UserServiceImpl (){
}
@Override
public void setSessionFactory(SessionFactoryImplementor sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public void setApplicationContext(ApplicationContext context)
throws BeansException {
this.context = context;
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher arg0) {
}
...
...
}
如果我@Autowire我的存储库接口,即“public IUserRepository iuserRepository;”在我的服务类中一切正常,即我没有得到任何null NullPointerException。即我想要发生的是spring创建代理,即JDK动态代理NOT CGLIB代理,因为我的Repository类实现了一个接口!!!
一切正常并且符合预期,但是同一个过程不适用于另一个Validator类,即我已经定义了一个Constrain / Annotation“@DuplicateUser”:
@Target( {METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
@Constraint(validatedBy = DuplicateUserValidator.class)
@Documented
public @interface DuplicateUser {
String message() default "{org.syncServer.ValidationAnnotations.message}";
Class<?>[] groups() default {};
Class<? extends Payload> [] payload() default{};
}
此Constrain在DuplicateUserValidator.class中实现。:
@Component
public class DuplicateUserValidator implements ConstraintValidator<DuplicateUser, String> {
@Autowired(required = true)
public IUserRepository iuserRepository;
public DuplicateUserValidator(){
System.out.println("initializing DuplicateUserValidator");
}
public void initialize(DuplicateUser userName) {
// do nothing
}
public boolean isValid(String str, ConstraintValidatorContext ctx) {
return validate(str);
}
public boolean validate(final String userName) {
Boolean result = false;
result = iuserRepository.checkIfDuplicateName(userName);
return result;
}
在我的DuplicateUserValidator.class中,我试图@Autowire相同的存储库接口(不是实现类),但是每当调用此Constrain时,接口始终为null。它是在服务层中成功@Autowired的相同接口。
我正在使用Spring Boot并且已经放置了注释
@ComponentScan(basePackages = "org.syncServer.*")
在我的Application.class下,我还有另外5个@Configuration类(HibernatePersistence,Tomcat,MVC,安全性,安全性,WebSockets),他们没有这个注释。我的所有Repository,DAO / Model,Service,Controller,CustomeAnnotations,Validators(包括DuplicateUserValidator类)类都位于“org.syncServer。*”结构下。
我的问题是为什么“iuserRepository”接口的@Autowier不在我的DuplicateUserValidator类中工作,但它正在我的服务层工作。 当我的应用程序启动时,我有一个代码,列出由Spring管理的所有bean和“duplicateUserValidator”bean,以及“userRepositoryImpl”bean由spring管理。我也不要在任何地方使用“新”操作员!!!!
我从一开始就要求对接口进行编码,因此不得使用CGLIB。
非常感谢任何帮助。
相关文章Spring 3.1 Autowiring does not work inside custom constraint validator