Generate DDL with spring boot using a custom delimiter - spring

I want generate create and drop ddl scripts using spring boot v1.4.3 with JPA - Hibernate 5.0.11.
Most answers I found use the javax.persistence.schema-generation properties. E.g. https://stackoverflow.com/a/36966419/974186
The problem with this approach is the it outputs the sql statements without an delimiter. E.g.
create table ... (...)
create table ... (...)
I want it to output the statements with the delimiter ;
create table ... (...);
create table ... (...);
But I can't find any javax.persistence.schema-generation property to configure it.
So I thought to use the SchemaExport from hibernate, because you can set the delimiter property. But to create a SchemaExport I need a MetadataImplementor (non deprected api).
I can not figure out how to get a MetadataImplementor from spring boot.
Does anyone know if there is either
a javax.persistence.schema-generation property to define the delimiter
or how to create a SchemaExport (get the dependencies)
or has another solution?
Here is some code you can play with
#SpringBootApplication
#ComponentScan(basePackageClasses = Application.class)
#EntityScan(basePackageClasses = User.class)
public class Application {
#Bean
public ApplicationRunner getApplicationRunner() {
return new ApplicationRunner() {
public void run(ApplicationArguments args) throws Exception {
// MetadataImplementor metadataImplementor = ???;
// new SchemaExport(metadataImplementor);
}
};
}
public static void main(String[] args) {
SpringApplication application = new SpringApplication(Application.class);
application.run(args);
}
}
Entity Class
#Entity
public class User {
#Id
#GeneratedValue
private Long id;
private String name;
public Long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Another entity class
#Entity
public class Person {
#Id
#GeneratedValue
private Long id;
private String name;
public Long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
application.properties
spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=create.sql
maven dependencies
<properties>
<spring.boot.version>1.4.3.RELEASE</spring.boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
With Hibernate 5.0
I just tried the code above with hibernate 5.0.11.Final. The only thing you must change is
SchemaExport schemaExport = new SchemaExport((MetadataImplementor) metadata);
schemaExport.setDelimiter(";");
schemaExport.setFormat(false);
schemaExport.setOutputFile(dropAndCreateDdlFile.getAbsolutePath());
schemaExport.execute(true, false, false, false);
or of course let the java configuration return a MetadataImplementor instead of Metadata and change the ApplicationRunner constructor param.

You might want to try setting the following Hibernate property:
spring.jpa.properties.hibernate.hbm2ddl.delimiter=;
#in addition to the other standard JPA properties you refered to, namely:
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=create.sql
I found this to do the job in a Spring Boot 2.1.2.RELEASE + matching Hibernate version (5.3.7.Final) project where I needed the same feature.
It might very well work on your not-so-different Hibernate environment.
Slightly off-topic, but one issue I have remains: Hibernate appends to create.sql. I whish I found a way to have it replace the file contents.

Finally after a lot of investigation I think I found an easy solution that uses public APIs. The solution I found uses hibernate 5.2 (more concrete 5.2.6.Final). But I think it can also be adapted to 5.0
Here is my spring java configuration
#Configuration
#AutoConfigureAfter({ HibernateJpaAutoConfiguration.class })
public class HibernateJavaConfig {
#ConditionalOnMissingBean({ Metadata.class })
#Bean
public Metadata getMetadata(StandardServiceRegistry standardServiceRegistry,
PersistenceUnitInfo persistenceUnitInfo) {
MetadataSources metadataSources = new MetadataSources(standardServiceRegistry);
List<String> managedClassNames = persistenceUnitInfo.getManagedClassNames();
for (String managedClassName : managedClassNames) {
metadataSources.addAnnotatedClassName(managedClassName);
}
Metadata metadata = metadataSources.buildMetadata();
return metadata;
}
#ConditionalOnMissingBean({ StandardServiceRegistry.class })
#Bean
public StandardServiceRegistry getStandardServiceRegistry(JpaProperties jpaProperties) {
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder();
Map<String, String> properties = jpaProperties.getProperties();
ssrb.applySettings(properties);
StandardServiceRegistry ssr = ssrb.build();
return ssr;
}
#ConditionalOnMissingBean({ PersistenceUnitInfo.class })
#Bean
public PersistenceUnitInfo getPersistenceUnitInfo(EntityScanPackages entityScanPackages) {
List<String> packagesToScan = entityScanPackages.getPackageNames();
DefaultPersistenceUnitManager persistenceUnitManager = new DefaultPersistenceUnitManager();
String[] packagesToScanArr = (String[]) packagesToScan.toArray(new String[packagesToScan.size()]);
persistenceUnitManager.setPackagesToScan(packagesToScanArr);
persistenceUnitManager.afterPropertiesSet();
PersistenceUnitInfo persistenceUnitInfo = persistenceUnitManager.obtainDefaultPersistenceUnitInfo();
return persistenceUnitInfo;
}
}
The java configuration creates a Metadata bean. This bean can be used in hibernate 5.2 to execute a schema generation. E.g.
#Component
public class GenerateDDLApplicationRunner implements ApplicationRunner {
private Metadata metadata;
public GenerateDDLApplicationRunner(Metadata metadata) {
this.metadata = metadata;
}
public void run(ApplicationArguments args) throws Exception {
File dropAndCreateDdlFile = new File("drop-and-create.ddl");
deleteFileIfExists(dropAndCreateDdlFile);
SchemaExport schemaExport = new SchemaExport();
schemaExport.setDelimiter(";");
schemaExport.setFormat(false);
schemaExport.setOutputFile(dropAndCreateDdlFile.getAbsolutePath());
schemaExport.execute(EnumSet.of(TargetType.SCRIPT), Action.BOTH, metadata);
}
private void deleteFileIfExists(File dropAndCreateDdlFile) {
if (dropAndCreateDdlFile.exists()) {
if (!dropAndCreateDdlFile.isFile()) {
String msg = MessageFormat.format("File is not a normal file {0}", dropAndCreateDdlFile);
throw new IllegalStateException(msg);
}
if (!dropAndCreateDdlFile.delete()) {
String msg = MessageFormat.format("Unable to delete file {0}", dropAndCreateDdlFile);
throw new IllegalStateException(msg);
}
}
}
}
The hibernate dialect is configured using the spring boot application.properties. In my case:
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL57InnoDBDialect

I have modified the solution from René to work in Spring Boot 2. Tested with version 2.0.4:
#Configuration
#AutoConfigureAfter({ HibernateJpaAutoConfiguration.class })
public class HibernateMetadataBean {
#ConditionalOnMissingBean({ Metadata.class })
#Bean
public Metadata getMetadata(StandardServiceRegistry standardServiceRegistry,
PersistenceUnitInfo persistenceUnitInfo) {
MetadataSources metadataSources = new MetadataSources(standardServiceRegistry);
List<String> managedClassNames = persistenceUnitInfo.getManagedClassNames();
for (String managedClassName : managedClassNames) {
metadataSources.addAnnotatedClassName(managedClassName);
}
return metadataSources.buildMetadata();
}
#ConditionalOnMissingBean({ StandardServiceRegistry.class })
#Bean
public StandardServiceRegistry getStandardServiceRegistry(JpaProperties jpaProperties) {
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder();
Map<String, String> properties = jpaProperties.getProperties();
ssrb.applySettings(properties);
return ssrb.build();
}
#ConditionalOnMissingBean({ PersistenceUnitInfo.class })
#Bean
public PersistenceUnitInfo getPersistenceUnitInfo(BeanFactory beanFactory) {
List<String> packagesToScan = EntityScanPackages.get(beanFactory).getPackageNames();
if (packagesToScan.isEmpty() && AutoConfigurationPackages.has(beanFactory)) {
packagesToScan = AutoConfigurationPackages.get(beanFactory);
}
DefaultPersistenceUnitManager persistenceUnitManager = new DefaultPersistenceUnitManager();
String[] packagesToScanArr = StringUtils.toStringArray(packagesToScan);
persistenceUnitManager.setPackagesToScan(packagesToScanArr);
persistenceUnitManager.afterPropertiesSet();
return persistenceUnitManager.obtainDefaultPersistenceUnitInfo();
}
}

What I found out is that the sessionFactoryholds the schemaExport values like createSQL, dropSQL, outputFile and delimiter.
The sessionFactory as a Bean can be created like that and is then available for autowiring:
....
#Autowired
private EntityManagerFactory entityManagerFactory;
#Bean
public SessionFactory sessionFactory() {
if (entityManagerFactory.unwrap(SessionFactory.class) == null) {
throw new NullPointerException("factory is not a hibernate factory");
}
return entityManagerFactory.unwrap(SessionFactory.class);
}
That is not a working solution but could help you to manually configure the schemaExport using sessionFactory. I dont find a solution for just using the properties and setting the delimiter just there. But could be a little helper for finding the working solution.
If I find more useful infos I will update my answer.

It's maybe a workaround but in my case it's enough to add semicolons during the build of the project. You can do that using a maven plugin, e.g.:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>add-semicolon-to-sql-file</id>
<phase>generate-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>sed</executable>
<arguments>
<argument>-i</argument>
<argument>/;$/!s/$/;/</argument>
<argument>src/main/resources/db/migration/V1__init.sql</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>

Related

Cannot show Togglz using a JDBCStateRepository in Spring Boot environment

In a Spring Boot 2.1 environment, I would like to use Togglz that are stored in a JDBCStateRepository.
The problem is: The Togglz are not shown in the console. The Togglz are not stored in the database.
My setup happens via the following files:
Maven:
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-spring-boot-starter</artifactId>
<version>2.6.1.Final</version>
</dependency>
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-console</artifactId>
<version>2.6.1.Final</version>
</dependency>
FeatureOptions:
public enum FeatureOptions implements Feature {
#EnabledByDefault
#Label("Zwerfobjecten geheel draaien in volgende snapshot")
FEATURE_ONE;
public boolean isActive() {
return FeatureContext.getFeatureManager().isActive(this);
}
}
TogglzConfiguration:
#Configuration
public class TogglzConfiguration implements TogglzConfig {
#Autowired
private DataSource dataSource;
public Class<? extends Feature> getFeatureClass() {
return FeatureOptions.class;
}
#Bean
public StateRepository getStateRepository() {
return new JDBCStateRepository(dataSource);
}
#Bean
public UserProvider getUserProvider() {
return new NoOpUserProvider();
}
#Bean
public FeatureProvider featureProvider() {
return new EnumBasedFeatureProvider(FeatureOptions.class);
}
}
The application.properties are:
togglz.feature-enums=nl.xyz.project.togglz.FeatureOptions
togglz.console.path=/togglz-console
togglz.console.enabled=true
togglz.console.secured=false
Only one property was missing ...
togglz.console.use-management-port=false

Spring Aspect doesn't work if bean is instantiated manually first

I have a simple aspect like below
#Aspect
public class PersistentAspect {
#AfterReturning("#annotation(org.aspect.PersistentOperation)")
public void log(JoinPoint jp) {
System.out.println("aspect call");
}
}
and an AppConfig like below
public class AppConfig {
private Integer num;
private String text;
public Integer getNum() {
return num;
}
#PersistentOperation
public void setNum(Integer num) {
this.num = num;
}
public String getText() {
return text;
}
#PersistentOperation
public void setText(String text) {
this.text = text;
}
}
And configuration class like below
#EnableWs
#Configuration
public class WsConfig extends WsConfigurerAdapter {
#Override
public void addInterceptors(List<EndpointInterceptor> interceptors) {
AppConfig config = config();
interceptors.add(new CustomValidatingInterceptor(schema(), null));
}
#Bean
public AppConfig config() {
AppConfig config = null;
config = new AppConfig();
return config;
}
#Bean
public PersistentAspect persistentAspect() {
PersistentAspect persistentAspect = new PersistentAspect();
return persistentAspect;
}
}
If I use below in the addInterceptors
AppConfig config = config();
The Aspect will not work. The obvious solution I have is to change the code to
AppConfig config = new AppConfig();
Now what I want to understand is, is there a config in which AppConfig config = config(); could still be made working. I assume that when spring initiates the Bean, it can make an AOP proxy of the AppConfig, and when I initiate the bean it interferes with that process somehow. What is the spring/spring-boot way of handling this?
Below is the pom.xml, so basically latest Spring 5.0.5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
Edit-1
Before posting the I had already adding a #EnableAspectJAutoProxy, but that had not helped.
The git repo to try the issue is on below
https://github.com/tarunlalwani/spring-aop-so-50084308
Your AppConfig bean (which you initialize, correctly, using a method annotated with #Bean) needs to be wrapped in a proxy that then executes the aspect logic.
You have to enable this behavior by adding the #EnableAspectJAutoProxy annotation to your WsConfig class.
By the way: it is totally correct to use
AppConfig config = config();
in your addInterceptors method. Spring will return the bean that was created by the config method.

How to set default properties in spring dataset DatabaseConfig

I am using springtestdbunit for writing test cases for my repository/dao.
I am populating data using db scripts and then matching with expected-sql-scripts.
I want to set escape character for my sample sql scripts.
My sample test class looks like this
#RunWith(SpringJUnit4ClassRunner.class)
#TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class})
#DbUnitConfiguration(dataSetLoader = ReplacementDataSetLoader.class)
#DatabaseSetup({"notification-init.xml"})
#SpringBootTest(classes = {TestApplication.class, DaoConfig.class})
public class NotificationRepositoryTest {
#Autowired
private NotificationRepository notificationRepository;
#After
#DatabaseTearDown
public void tearDown() throws Exception {
}
#Before
public void setUp() throws Exception {
//donorModel = donorRepository.findOne(0L);
}
All I want to set these two properties when I am configuring my H2 database.
DatabaseConfig.PROPERTY_ESCAPE_PATTERN
DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS
Here is an example from a spring boot project using JavaConfig to set those 2 properties:
#Configuration
#Profile("test")
public class DBUnitConfiguration {
#Bean
public DatabaseConfigBean dbUnitDatabaseConfig() {
DatabaseConfigBean bean = new DatabaseConfigBean();
bean.setAllowEmptyFields(true);
bean.setEscapePattern("\"?\"");
return bean;
}
#Bean
public DatabaseDataSourceConnectionFactoryBean dbUnitDatabaseConnection(
DatabaseConfigBean dbUnitDatabaseConfig,
DataSource dataSource
) {
DatabaseDataSourceConnectionFactoryBean bean = new DatabaseDataSourceConnectionFactoryBean(dataSource);
bean.setDatabaseConfig(dbUnitDatabaseConfig);
return bean;
}
}
It requires 1.3.0 version of spring-test-dbunit to set the ALLOW_EMPTY_FIELDS prop:
<dependency>
<groupId>com.github.springtestdbunit</groupId>
<artifactId>spring-test-dbunit</artifactId>
<version>1.3.0</version>
<scope>test</scope>
</dependency>

Mocked Spring #Service that has #Retryable annotations on methods fails with UnfinishedVerificationException

I'm using Spring Boot 1.4.0.RELEASE with spring-boot-starter-batch, spring-boot-starter-aop and spring-retry
I have a Spring Integration test that has a #Service which is mocked at runtime. I've noticed that if the #Service class contains any #Retryable annotations on its methods, then it appears to interfere with Mockito.verify(), I get a UnfinishedVerificationException. I presume this must be something to do with spring-aop? If I comment out all #Retryable annotations in the #Service then verify works ok again.
I have created a github project that demonstrates this issue.
It fails in sample.batch.MockBatchTestWithRetryVerificationFailures.batchTest() at validateMockitoUsage();
With something like:
12:05:36.554 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - After test method: context [DefaultTestContext#5ec0a365 testClass = MockBatchTestWithRetryVerificationFailures, testInstance = sample.batch.MockBatchTestWithRetryVerificationFailures#5abca1e0, testMethod = batchTest#MockBatchTestWithRetryVerificationFailures, testException = org.mockito.exceptions.misusing.UnfinishedVerificationException:
Missing method call for verify(mock) here:
-> at sample.batch.service.MyRetryService$$FastClassBySpringCGLIB$$7573ce2a.invoke(<generated>)
Example of correct verification:
verify(mock).doSomething()
However I have another class (sample.batch.MockBatchTestWithNoRetryWorking.batchTest()) with a mocked #Service that doesn't have any #Retryable annotation and verify works fine.
What am I doing wrong?
In my pom.xml I have the following:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
...
Then all the related Java Classes
#SpringBootApplication
#EnableBatchProcessing
#Configuration
#EnableRetry
public class SampleBatchApplication {
#Autowired
private JobBuilderFactory jobs;
#Autowired
private StepBuilderFactory steps;
#Autowired
private MyRetryService myRetryService;
#Autowired
private MyServiceNoRetry myServiceNoRetry;
#Bean
protected Tasklet tasklet() {
return new Tasklet() {
#Override
public RepeatStatus execute(StepContribution contribution,
ChunkContext context) {
myServiceNoRetry.process();
myRetryService.process();
return RepeatStatus.FINISHED;
}
};
}
#Bean
public Job job() throws Exception {
return this.jobs.get("job").start(step1()).build();
}
#Bean
protected Step step1() throws Exception {
return this.steps.get("step1").tasklet(tasklet()).build();
}
public static void main(String[] args) throws Exception {
// System.exit is common for Batch applications since the exit code can be used to
// drive a workflow
System.exit(SpringApplication
.exit(SpringApplication.run(SampleBatchApplication.class, args)));
}
#Bean
ResourcelessTransactionManager transactionManager() {
return new ResourcelessTransactionManager();
}
#Bean
public JobRepository getJobRepo() throws Exception {
return new MapJobRepositoryFactoryBean(transactionManager()).getObject();
}
}
#Service
public class MyRetryService {
public static final Logger LOG = LoggerFactory.getLogger(MyRetryService.class);
#Retryable(maxAttempts = 5, include = RuntimeException.class, backoff = #Backoff(delay = 100, multiplier = 2))
public boolean process() {
double random = Math.random();
LOG.info("Running process, random value {}", random);
if (random > 0.2d) {
throw new RuntimeException("Random fail time!");
}
return true;
}
}
#Service
public class MyServiceNoRetry {
public static final Logger LOG = LoggerFactory.getLogger(MyServiceNoRetry.class);
public boolean process() {
LOG.info("Running process that doesn't do retry");
return true;
}
}
#ActiveProfiles("Test")
#ContextConfiguration(classes = {SampleBatchApplication.class, MockBatchTestWithNoRetryWorking.MockedRetryService.class}, loader = AnnotationConfigContextLoader.class)
#RunWith(SpringRunner.class)
public class MockBatchTestWithNoRetryWorking {
#Autowired
MyServiceNoRetry service;
#Test
public void batchTest() {
service.process();
verify(service).process();
validateMockitoUsage();
}
public static class MockedRetryService {
#Bean
#Primary
public MyServiceNoRetry myService() {
return mock(MyServiceNoRetry.class);
}
}
}
#ActiveProfiles("Test")
#ContextConfiguration(classes = { SampleBatchApplication.class,
MockBatchTestWithRetryVerificationFailures.MockedRetryService.class },
loader = AnnotationConfigContextLoader.class)
#RunWith(SpringRunner.class)
public class MockBatchTestWithRetryVerificationFailures {
#Autowired
MyRetryService service;
#Test
public void batchTest() {
service.process();
verify(service).process();
validateMockitoUsage();
}
public static class MockedRetryService {
#Bean
#Primary
public MyRetryService myRetryService() {
return mock(MyRetryService.class);
}
}
}
EDIT: Updated question and code based on a sample project I put together to show the problem.
So after looking at a similar github issue for spring-boot
I found that there is an extra proxy getting in the way. I found a nasty hack by unwrapping the aop class by hand, makes verification work, ie:
#Test
public void batchTest() throws Exception {
service.process();
if (service instanceof Advised) {
service = (MyRetryService) ((Advised) service).getTargetSource().getTarget();
}
verify(service).process();
validateMockitoUsage();
}
Hopefully, this can be fixed similar to the above github issue. I'll raise an issue and see how far I get.
EDIT: Raised the github issue
After I've seen #Joel Pearsons answer, and especially the linked GitHub issue, I worked around this by temporarily using a static helper method that unwraps and verifies:
public static <T> T unwrapAndVerify(T mock, VerificationMode mode) {
return ((T) Mockito.verify(AopTestUtils.getTargetObject(mock), mode));
}
With this method the only difference in the test cases is the verification call. There is no overhead other than this:
unwrapAndVerify(service, times(2)).process();
instead of
verify(service, times(2)).process();
Actually, it was even possible to name the helper method like the actual Mockito method, so that you only need to replace the import, but I didn't like the subsequent confusion.
However, unwrapping shouldn't be required if #MockBean is used instead of mock() to create the mocked bean. Spring Boot 1.4 supports this annotation.

Dependency Injection in JSR-303 Constraint Validator with Spring fails

I have the same problem as here and here but couldn't find a solution yet.
So my sample test project will show the whole relevant configuration and code:
Constraint annotation:
#Target({ ElementType.METHOD, ElementType.FIELD })
#Retention(RetentionPolicy.RUNTIME)
#Constraint(validatedBy = FooValidator.class)
public #interface FooValid {
String message();
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Annotated PoJo:
public class Foo {
#FooValid(message = "Test failed")
private Integer test;
[...]
}
Annotated Service with #Validated:
#Service
#Validated
public class FooService {
private final Test test;
#Autowired
public FooService(final Test test) {
this.test = test;
}
public void foo(#Valid final Foo foo) {
this.test.test(foo);
}
}
JSR-303 ConstraintValidator:
public class FooValidator implements ConstraintValidator<FooValid, Integer> {
#Autowired
private ValidationService validationService;
#Override
public void initialize(final FooValid constraintAnnotation) {
// TODO Auto-generated method stub
}
#Override
public boolean isValid(final Integer value, final ConstraintValidatorContext context) {
// this.validationService is always NULL!
Assert.notNull(this.validationService, "the validationService must not be null");
return false;
}
}
Injected ValidationService:
#Service
public class ValidationService {
public void test(final Foo foo) {
System.out.println(foo);
}
}
Spring boot application and configuration:
#Configuration
#ComponentScan
#EnableAutoConfiguration
public class Application {
public static void main(final String[] args) {
final ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
final FooService service = context.getBean(FooService.class);
service.foo(new Foo());
}
#Bean
public static LocalValidatorFactoryBean validatorFactory() {
return new LocalValidatorFactoryBean();
}
#Bean
public static MethodValidationPostProcessor validationPostProcessor() {
return new MethodValidationPostProcessor();
}
}
relevant maven pom:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.9.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>demo.Application</start-class>
<java.version>1.7</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
I'm using the LocalValidatorFactoryBean with the default SpringConstraintValidatorFactory.
But why the dependency injection is not working in the ConstraintValidator and the ValidationService could not be autowired?
By the way if I don't use #Validated at the service, inject in opposite the spring or javax Validator interface and call manually "validator.validate" the dependency injection will work. But I don't want to call the validate method in every service manually.
Many thanks for help :)
I have fought the same problem in Spring Boot environment and I found out that Hibernate internal implementation got in instead of the configured Spring's one. When the application started, debugger caught a line with the Spring's factory but later in runtime there was Hibernate's one. After some debugging, I came to the conclusion that MethodValidationPostProcessor got the internal one. Therefore I configured it as follows:
#Bean
public Validator validator() {
return new LocalValidatorFactoryBean();
}
#Bean
public MethodValidationPostProcessor methodValidationPostProcessor(Validator validator) {
MethodValidationPostProcessor methodValidationPostProcessor = new MethodValidationPostProcessor();
methodValidationPostProcessor.setValidator(validator);
return methodValidationPostProcessor;
}
Note the setter for validator - it did the job.
I had the same issue. The problem is arises because Hibernate is finding and applying the validators instead of Spring. So you need to set validation mode to NONE when configuring Hibernate:
#Bean(name="entityManagerFactory")
public LocalContainerEntityManagerFactoryBean
localContainerEntityManagerFactoryBean(DataSource dataSource) {
LocalContainerEntityManagerFactoryBean lcemfb =
new LocalContainerEntityManagerFactoryBean();
lcemfb.setDataSource(dataSource);
lcemfb.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
lcemfb.setValidationMode(ValidationMode.NONE);
// Continue configuration...
If you have confgured a LocalValidationFactoryBean Spring will pick up any validators annotated with #Component and autowire them.
This is what worked for me. I had to used the #Inject tag.
public class FooValidator implements ConstraintValidator<FooValid, Integer> {
private ValidationService validationService;
#Inject
public FooValidator(ValidationService validationService){
this.validationService = validationService;
}
#Override
public void initialize(final FooValid constraintAnnotation) {
// TODO Auto-generated method stub
}
#Override
public boolean isValid(final Integer value, final ConstraintValidatorContext context) {
// this.validationService is always NULL!
Assert.notNull(this.validationService, "the validationService must not be null");
return false;
}
}

Resources