Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
80754d5c
Commit
80754d5c
authored
Jul 23, 2015
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish
Closes gh-3534
parent
bf303b02
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
92 additions
and
128 deletions
+92
-128
README.adoc
spring-boot-samples/README.adoc
+1
-1
application.properties
...ng-boot-sample-property-validation/application.properties
+0
-2
build.gradle
...mples/spring-boot-sample-property-validation/build.gradle
+0
-49
pom.xml
...ot-samples/spring-boot-sample-property-validation/pom.xml
+5
-3
SampleProperties.java
...main/java/sample/propertyvalidation/SampleProperties.java
+8
-2
SamplePropertiesValidator.java
.../sample/propertyvalidation/SamplePropertiesValidator.java
+8
-25
SamplePropertyValidationApplication.java
...opertyvalidation/SamplePropertyValidationApplication.java
+26
-10
application.properties
...erty-validation/src/main/resources/application.properties
+2
-2
SamplePropertyValidationApplicationTests.java
...yvalidation/SamplePropertyValidationApplicationTests.java
+42
-32
application.properties
...erty-validation/src/test/resources/application.properties
+0
-2
No files found.
spring-boot-samples/README.adoc
View file @
80754d5c
...
...
@@ -71,7 +71,7 @@
* link:spring-boot-sample-profile[spring-boot-sample-profile]
-- example showing Spring's `@profile` support
* link:spring-boot-sample-property-validation[spring-boot-sample-property-validation]
-- example showing the usage of
@ConfigurationProperties` with a Spring's
`Validator`
-- example showing the usage of
`@ConfigurationProperties` with a Spring
`Validator`
* link:spring-boot-sample-parent-context[spring-boot-sample-parent-context]
-- example showing an `ApplicationContext` with a parent
* link:spring-boot-sample-aop[spring-boot-sample-aop]
...
...
spring-boot-samples/spring-boot-sample-property-validation/application.properties
deleted
100644 → 0
View file @
bf303b02
host
:
192.168.0.1
port
:
8080
\ No newline at end of file
spring-boot-samples/spring-boot-sample-property-validation/build.gradle
deleted
100644 → 0
View file @
bf303b02
buildscript
{
ext
{
springBootVersion
=
'1.3.0.BUILD-SNAPSHOT'
}
repositories
{
// NOTE: You should declare only repositories that you need here
mavenLocal
()
mavenCentral
()
maven
{
url
"http://repo.spring.io/release"
}
maven
{
url
"http://repo.spring.io/milestone"
}
maven
{
url
"http://repo.spring.io/snapshot"
}
}
dependencies
{
classpath
(
"org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
)
}
}
apply
plugin:
'java'
apply
plugin:
'eclipse'
apply
plugin:
'idea'
apply
plugin:
'spring-boot'
jar
{
baseName
=
'spring-boot-sample-property-validation'
version
=
'0.0.0'
}
run
{
systemProperties
=
System
.
properties
}
repositories
{
// NOTE: You should declare only repositories that you need here
mavenLocal
()
mavenCentral
()
maven
{
url
"http://repo.spring.io/release"
}
maven
{
url
"http://repo.spring.io/milestone"
}
maven
{
url
"http://repo.spring.io/snapshot"
}
}
dependencies
{
compile
(
"org.springframework.boot:spring-boot-starter"
)
compile
(
"org.hibernate:hibernate-validator"
)
testCompile
(
"org.springframework.boot:spring-boot-starter-test"
)
}
task
wrapper
(
type:
Wrapper
)
{
gradleVersion
=
'1.6'
}
spring-boot-samples/spring-boot-sample-property-validation/pom.xml
View file @
80754d5c
...
...
@@ -23,11 +23,13 @@
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter
</artifactId>
</dependency>
<dependency>
<groupId>
org.
hibernate
</groupId>
<artifactId>
hibernate-validat
or
</artifactId>
<
version>
5.1.3.Final
</version
>
<groupId>
org.
springframework.boot
</groupId>
<artifactId>
spring-boot-configuration-process
or
</artifactId>
<
optional>
true
</optional
>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
...
...
spring-boot-samples/spring-boot-sample-property-validation/src/main/java/sample/propertyvalidation/SampleProperties.java
View file @
80754d5c
...
...
@@ -20,12 +20,18 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import
org.springframework.stereotype.Component
;
@Component
@ConfigurationProperties
@ConfigurationProperties
(
prefix
=
"sample"
)
public
class
SampleProperties
{
/**
* Sample host.
*/
private
String
host
;
private
Integer
port
;
/**
* Sample port.
*/
private
Integer
port
=
8080
;
public
String
getHost
()
{
return
host
;
...
...
spring-boot-samples/spring-boot-sample-property-validation/src/main/java/sample/propertyvalidation/
Configuration
PropertiesValidator.java
→
spring-boot-samples/spring-boot-sample-property-validation/src/main/java/sample/propertyvalidation/
Sample
PropertiesValidator.java
View file @
80754d5c
...
...
@@ -16,46 +16,29 @@
package
sample
.
propertyvalidation
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.core.annotation.AnnotationUtils
;
import
org.springframework.stereotype.Component
;
import
java.util.regex.Pattern
;
import
org.springframework.validation.Errors
;
import
org.springframework.validation.ValidationUtils
;
import
org.springframework.validation.Validator
;
import
java.util.HashSet
;
import
java.util.Set
;
import
java.util.regex.Pattern
;
@Component
(
value
=
"configurationPropertiesValidator"
)
public
class
ConfigurationPropertiesValidator
implements
Validator
{
public
static
final
String
IP_REGEX
=
"^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$"
;
public
class
SamplePropertiesValidator
implements
Validator
{
final
Pattern
pattern
=
Pattern
.
compile
(
IP_REGEX
);
private
Set
<
String
>
validatedClasses
=
new
HashSet
<
String
>()
{{
add
(
SampleProperties
.
class
.
getName
());
}};
final
Pattern
pattern
=
Pattern
.
compile
(
"^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$"
);
@Override
public
boolean
supports
(
Class
<?>
aClass
)
{
return
AnnotationUtils
.
findAnnotation
(
aClass
,
ConfigurationProperties
.
class
)
!=
null
;
public
boolean
supports
(
Class
<?>
type
)
{
return
type
==
SampleProperties
.
class
;
}
@Override
public
void
validate
(
Object
o
,
Errors
errors
)
{
if
(
validatedClasses
.
contains
(
o
.
getClass
().
getName
()))
{
doValidation
(
o
,
errors
);
}
}
private
void
doValidation
(
Object
o
,
Errors
errors
)
{
ValidationUtils
.
rejectIfEmpty
(
errors
,
"host"
,
"host.empty"
);
ValidationUtils
.
rejectIfEmpty
(
errors
,
"port"
,
"port.empty"
);
SampleProperties
properties
=
(
SampleProperties
)
o
;
if
(!
pattern
.
matcher
(
properties
.
getHost
()).
matches
())
{
if
(
properties
.
getHost
()
!=
null
&&
!
pattern
.
matcher
(
properties
.
getHost
()).
matches
())
{
errors
.
rejectValue
(
"host"
,
"Invalid host"
);
}
}
...
...
spring-boot-samples/spring-boot-sample-property-validation/src/main/java/sample/propertyvalidation/SamplePropertyValidationApplication.java
View file @
80754d5c
...
...
@@ -18,26 +18,42 @@ package sample.propertyvalidation;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.CommandLineRunner
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.boot.builder.SpringApplicationBuilder
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Profile
;
import
org.springframework.stereotype.Service
;
import
org.springframework.validation.Validator
;
@SpringBootApplication
@EnableConfigurationProperties
public
class
SamplePropertyValidationApplication
implements
CommandLineRunner
{
public
class
SamplePropertyValidationApplication
{
@Bean
public
Validator
configurationPropertiesValidator
()
{
return
new
SamplePropertiesValidator
();
}
@Service
@Profile
(
"app"
)
static
class
Startup
implements
CommandLineRunner
{
@Autowired
private
SampleProperties
properties
;
@Autowired
private
SampleProperties
properties
;
@Override
public
void
run
(
String
...
args
)
{
System
.
out
.
println
(
"host: "
+
this
.
properties
.
getHost
());
System
.
out
.
println
(
"port:"
+
this
.
properties
.
getPort
());
@Override
public
void
run
(
String
...
args
)
{
System
.
out
.
println
(
"========================================="
);
System
.
out
.
println
(
"Sample host: "
+
this
.
properties
.
getHost
());
System
.
out
.
println
(
"Sample port: "
+
this
.
properties
.
getPort
());
System
.
out
.
println
(
"========================================="
);
}
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
SpringApplication
.
run
(
SamplePropertyValidationApplication
.
class
,
args
);
new
SpringApplicationBuilder
(
SamplePropertyValidationApplication
.
class
)
.
profiles
(
"app"
).
run
(
args
);
}
}
spring-boot-samples/spring-boot-sample-property-validation/src/main/resources/application.properties
View file @
80754d5c
host
:
192.168.0.1
port
:
8080
\ No newline at end of file
sample.host
=
192.168.0.1
sample.port
=
7070
\ No newline at end of file
spring-boot-samples/spring-boot-sample-property-validation/src/test/java/sample/propertyvalidation/SamplePropertyValidationApplicationTests.java
View file @
80754d5c
...
...
@@ -16,15 +16,15 @@
package
sample
.
propertyvalidation
;
import
org.junit.After
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.rules.ExpectedException
;
import
org.springframework.beans.factory.BeanCreationException
;
import
org.springframework.boot.autoconfigure.web.ServerProperties
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.boot.test.EnvironmentTestUtils
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.validation.Validator
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
...
...
@@ -32,54 +32,64 @@ import static org.junit.Assert.assertEquals;
* Tests for {@link SamplePropertyValidationApplication}.
*
* @author Lucas Saldanha
* @author Stephane Nicoll
*/
public
class
SamplePropertyValidationApplicationTests
{
@Rule
public
final
ExpectedException
thrown
=
ExpectedException
.
none
();
private
final
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
();
@After
public
void
closeContext
()
{
context
.
close
();
}
@Test
public
void
testBinding
ValidProperties
()
{
this
.
context
.
register
(
TestConfigur
ation
.
class
);
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"host:192.168.0.1"
);
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"port:808
0"
);
public
void
bind
ValidProperties
()
{
this
.
context
.
register
(
SamplePropertyValidationApplic
ation
.
class
);
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"sample.host:192.168.0.1"
,
"sample.port:909
0"
);
this
.
context
.
refresh
();
assertEquals
(
1
,
this
.
context
.
getBeanNamesForType
(
SampleProperties
.
class
).
length
);
SampleProperties
properties
=
this
.
context
.
getBean
(
SampleProperties
.
class
);
assertEquals
(
"192.168.0.1"
,
properties
.
getHost
());
assertEquals
(
8080
,
(
int
)
properties
.
getPort
());
assertEquals
(
Integer
.
valueOf
(
9090
),
properties
.
getPort
());
}
@Test
(
expected
=
BeanCreationException
.
class
)
public
void
testBindingInvalidProperties
()
{
this
.
context
.
register
(
TestConfiguration
.
class
);
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"host:xxxxxx"
);
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"port:8080"
);
@Test
public
void
bindInvalidHost
()
{
this
.
context
.
register
(
SamplePropertyValidationApplication
.
class
);
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"sample.host:xxxxxx"
,
"sample.port:9090"
);
thrown
.
expect
(
BeanCreationException
.
class
);
thrown
.
expectMessage
(
"xxxxxx"
);
this
.
context
.
refresh
();
}
@Test
public
void
testBindingValidPropertiesWithMultipleConfigurationPropertiesClasses
()
{
this
.
context
.
register
(
TestConfiguration
.
class
);
this
.
context
.
register
(
ServerProperties
.
class
);
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"host:192.168.0.1"
);
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"port:8080"
);
this
.
context
.
refresh
();
public
void
bindNullHost
()
{
this
.
context
.
register
(
SamplePropertyValidationApplication
.
class
);
assertEquals
(
1
,
this
.
context
.
getBeanNamesForType
(
SampleProperties
.
class
).
length
);
SampleProperties
properties
=
this
.
context
.
getBean
(
SampleProperties
.
class
);
assertEquals
(
"192.168.0.1"
,
properties
.
getHost
()
);
assertEquals
(
8080
,
(
int
)
properties
.
getPort
()
);
thrown
.
expect
(
BeanCreationException
.
class
);
thrown
.
expectMessage
(
"null"
);
thrown
.
expectMessage
(
"host"
);
this
.
context
.
refresh
(
);
}
@Configuration
@EnableConfigurationProperties
(
SampleProperties
.
class
)
protected
static
class
TestConfiguration
{
@Test
public
void
validatorOnlyCalledOnSupportedClass
()
{
this
.
context
.
register
(
SamplePropertyValidationApplication
.
class
);
this
.
context
.
register
(
ServerProperties
.
class
);
// our validator will not apply here
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"sample.host:192.168.0.1"
,
"sample.port:9090"
);
this
.
context
.
refresh
();
@Bean
public
Validator
configurationPropertiesValidator
()
{
return
new
ConfigurationPropertiesValidator
();
}
SampleProperties
properties
=
this
.
context
.
getBean
(
SampleProperties
.
class
);
assertEquals
(
"192.168.0.1"
,
properties
.
getHost
());
assertEquals
(
Integer
.
valueOf
(
9090
),
properties
.
getPort
());
}
}
spring-boot-samples/spring-boot-sample-property-validation/src/test/resources/application.properties
deleted
100644 → 0
View file @
bf303b02
host
:
192.168.0.1
port
:
8080
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment