diff --git a/inject-java/src/main/java/io/micronaut/annotation/processing/BeanDefinitionInjectProcessor.java b/inject-java/src/main/java/io/micronaut/annotation/processing/BeanDefinitionInjectProcessor.java index e0a39b54b87..b32e89358d3 100644 --- a/inject-java/src/main/java/io/micronaut/annotation/processing/BeanDefinitionInjectProcessor.java +++ b/inject-java/src/main/java/io/micronaut/annotation/processing/BeanDefinitionInjectProcessor.java @@ -961,7 +961,7 @@ public Object visitExecutable(ExecutableElement method, Object o) { writer.setValidated(validatedMethod); } } - } else if (validatedMethod) { + } else if (validatedMethod && isDeclaredBean) { if (isPrivate) { error(method, "Method annotated with constraints but is declared private. Change the method to be non-private in order for AOP advice to be applied."); return null; diff --git a/inject-java/src/test/groovy/io/micronaut/inject/validation/Account1.java b/inject-java/src/test/groovy/io/micronaut/inject/validation/Account1.java new file mode 100644 index 00000000000..40d7ed1e007 --- /dev/null +++ b/inject-java/src/test/groovy/io/micronaut/inject/validation/Account1.java @@ -0,0 +1,43 @@ +package io.micronaut.inject.validation; + +import javax.annotation.Nullable; +import javax.persistence.Entity; +import javax.validation.constraints.NotBlank; + +@Entity +public class Account1 { + + private Long id; + + @Nullable + @NotBlank + private String username; + + @Nullable + @NotBlank + private String password; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } +} diff --git a/inject-java/src/test/groovy/io/micronaut/inject/validation/Account2.java b/inject-java/src/test/groovy/io/micronaut/inject/validation/Account2.java new file mode 100644 index 00000000000..b6c8cdb633d --- /dev/null +++ b/inject-java/src/test/groovy/io/micronaut/inject/validation/Account2.java @@ -0,0 +1,39 @@ +package io.micronaut.inject.validation; + +import javax.persistence.Entity; +import javax.validation.constraints.NotBlank; + +@Entity +public class Account2 { + + private Long id; + + private String username; + + private String password; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + @NotBlank + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } +} diff --git a/inject-java/src/test/groovy/io/micronaut/inject/validation/Account3.java b/inject-java/src/test/groovy/io/micronaut/inject/validation/Account3.java new file mode 100644 index 00000000000..07b9fb9db5d --- /dev/null +++ b/inject-java/src/test/groovy/io/micronaut/inject/validation/Account3.java @@ -0,0 +1,41 @@ +package io.micronaut.inject.validation; + +import javax.annotation.Nullable; +import javax.persistence.Entity; +import javax.validation.constraints.NotBlank; + +@Entity +@Nullable +public class Account3 { + + private Long id; + + private String username; + + private String password; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + @NotBlank + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } +} diff --git a/inject-java/src/test/groovy/io/micronaut/inject/validation/BeanWithValidationSpec.groovy b/inject-java/src/test/groovy/io/micronaut/inject/validation/BeanWithValidationSpec.groovy new file mode 100644 index 00000000000..4d6aad66be8 --- /dev/null +++ b/inject-java/src/test/groovy/io/micronaut/inject/validation/BeanWithValidationSpec.groovy @@ -0,0 +1,18 @@ +package io.micronaut.inject.validation + +import io.micronaut.context.ApplicationContext +import spock.lang.AutoCleanup +import spock.lang.Shared +import spock.lang.Specification + +class BeanWithValidationSpec extends Specification { + + @Shared @AutoCleanup ApplicationContext context = ApplicationContext.run() + + void 'test bean definition is not created for a bean with validation'() { + expect: + context.getBeanDefinitions(Account1.class).isEmpty() + context.getBeanDefinitions(Account2.class).isEmpty() + context.getBeanDefinitions(Account3.class).isEmpty() + } +}