Spring Hibernate自定义bean验证 - NullPointerException

时间:2017-10-01 21:06:12

标签: java spring hibernate spring-mvc

我尝试使用注释和ConstraintValidator接口创建自定义bean验证。目的是使模型的名称独一无二。我不知道为什么,但是当我提交表单以添加产品时,会抛出NullPointerException。 显示java.lang.NullPointerException pl.dsdev.validator.UniqueNameValidator.isValid(UniqueNameValidator.java:20) 型号类: @实体 公共类产品{ @ID @GeneratedValue 私人长身份; @NotNull(消息="名称是必需的") @ Size.List({@ Size(min = 3,message ="必须长于{min}个字符"),@ Size(max = 25,message ="必须小于{最多}个字符")}) @UniqueName(message ="此名称由其他产品使用。") 私有字符串名称; 注释界面: @target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy = UniqueNameValidator.class) public @interface UniqueName { 字符串message()默认"&#34 ;; 类<?> [] groups()默认{}; 类&LT ;?扩展Payload> [] payload()default {}; } ConstraintValidator的实现: 公共类UniqueNameValidator实现ConstraintValidator< UniqueName,String> { @Autowired private ProductService productService; public void initialize(UniqueName uniqueName){ } public boolean isValid(String name,ConstraintValidatorContext constraintValidatorContext){     return productService.findByName(name)== null; } public ProductService getProductService(){     return productService; } public void setProductService(ProductService productService){     this.productService = productService; } } 我的root-context.xml文件: <?xml version =" 1.0"编码=" UTF-8"> < beans xmlns =" http://www.springframework.org/schema/beans"    的xmlns:=的xsi" HTTP://www.w3.org/2001/XMLSchema-instance"    的xmlns:上下文=" HTTP://www.springframework.org/schema/context"    的xmlns:MVC =" HTTP://www.springframework.org/schema/mvc"的xmlns:TX =" HTTP://www.springframework.org/schema/tx"    的xmlns:P =" HTTP://www.springframework.org/schema/p"    xsi:schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/ schema / context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring -mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> < context:component-scan base-package =" pl.dsdev" /> < mvc:annotation-driven /> <! - 将@Controllers选择用于呈现的视图解析为/ WEB-INF / views目录中的.jsp资源 - > < bean class =" org.springframework.web.servlet.view.InternalResourceViewResolver">     < property name =" prefix"值=" / WEB-INF /视图/" />     < property name =" suffix"值=" .JSP" /> < /豆腐> < bean id =" dataSource"类=" org.apache.commons.dbcp.BasicDataSource"破坏法="靠近">     < property name =" driverClassName"值=" com.mysql.jdbc.Driver" />     < property name =" url"值=" JDBC:MySQL的://本地主机:3306 / springbaza" />     < property name =" username"值="教程" />     < property name =" password"值="密码" /> < /豆腐> < bean id =" entityManagerFactory"类=" org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">     < property name =" dataSource" REF ="数据源" />     < property name =" packagesToScan"值=" pl.dsdev.model" />     < property name =" jpaVendorAdapter">         < bean class =" org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />     < /性>     < property name =" jpaProperties">         <道具>             < prop key =" hibernate.hbm2ddl.auto"> create< / prop>             < prop key =" hibernate.dialect"> org.hibernate.dialect.MySQL5Dialect< / prop>             < prop key =" hibernate.show_sql"> true< / prop>         < /道具>     < /性> < /豆腐> < tx:注释驱动/> < bean id =" transactionManager"类=" org.springframework.orm.jpa.JpaTransactionManager">     < property name =" entityManagerFactory" REF ="的entityManagerFactory" /> < /豆腐> < bean id =" persistenceExceptionTranslationPostProcessor"类=" org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> < bean id =" messageSource"类=" org.springframework.context.support.ResourceBundleMessageSource">     < property name =" basename"值="验证" /> < /豆腐> < bean id ="验证器"       类=" org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

1 个答案:

答案 0 :(得分:1)

看起来你的productService没有被春天注入(这解释了你得到的NPE)。

您的配置错误(或根本未配置)spring bean validation