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
dc6a2f05
Commit
dc6a2f05
authored
Jun 18, 2015
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3069 from eddumelendez/gh-3031
* gh-3031: Add support for FieldNamingStrategy
parents
b31c995b
58ca6cdc
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
77 additions
and
5 deletions
+77
-5
MongoDataAutoConfiguration.java
.../boot/autoconfigure/mongo/MongoDataAutoConfiguration.java
+20
-4
MongoProperties.java
...ngframework/boot/autoconfigure/mongo/MongoProperties.java
+15
-0
MongoDataAutoConfigurationTests.java
.../autoconfigure/mongo/MongoDataAutoConfigurationTests.java
+41
-1
appendix-application-properties.adoc
...cs/src/main/asciidoc/appendix-application-properties.adoc
+1
-0
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoDataAutoConfiguration.java
View file @
dc6a2f05
...
@@ -22,6 +22,10 @@ import java.util.Collections;
...
@@ -22,6 +22,10 @@ import java.util.Collections;
import
java.util.HashSet
;
import
java.util.HashSet
;
import
java.util.Set
;
import
java.util.Set
;
import
com.mongodb.DB
;
import
com.mongodb.Mongo
;
import
com.mongodb.MongoClient
;
import
org.springframework.beans.factory.BeanClassLoaderAware
;
import
org.springframework.beans.factory.BeanClassLoaderAware
;
import
org.springframework.beans.factory.BeanFactory
;
import
org.springframework.beans.factory.BeanFactory
;
import
org.springframework.beans.factory.NoSuchBeanDefinitionException
;
import
org.springframework.beans.factory.NoSuchBeanDefinitionException
;
...
@@ -42,6 +46,7 @@ import org.springframework.core.type.filter.AnnotationTypeFilter;
...
@@ -42,6 +46,7 @@ import org.springframework.core.type.filter.AnnotationTypeFilter;
import
org.springframework.dao.DataAccessException
;
import
org.springframework.dao.DataAccessException
;
import
org.springframework.dao.support.PersistenceExceptionTranslator
;
import
org.springframework.dao.support.PersistenceExceptionTranslator
;
import
org.springframework.data.annotation.Persistent
;
import
org.springframework.data.annotation.Persistent
;
import
org.springframework.data.mapping.model.FieldNamingStrategy
;
import
org.springframework.data.mongodb.MongoDbFactory
;
import
org.springframework.data.mongodb.MongoDbFactory
;
import
org.springframework.data.mongodb.core.MongoTemplate
;
import
org.springframework.data.mongodb.core.MongoTemplate
;
import
org.springframework.data.mongodb.core.SimpleMongoDbFactory
;
import
org.springframework.data.mongodb.core.SimpleMongoDbFactory
;
...
@@ -57,10 +62,6 @@ import org.springframework.util.Assert;
...
@@ -57,10 +62,6 @@ import org.springframework.util.Assert;
import
org.springframework.util.ClassUtils
;
import
org.springframework.util.ClassUtils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.util.StringUtils
;
import
com.mongodb.DB
;
import
com.mongodb.Mongo
;
import
com.mongodb.MongoClient
;
/**
/**
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's mongo support.
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's mongo support.
* <p>
* <p>
...
@@ -136,6 +137,21 @@ public class MongoDataAutoConfiguration implements BeanClassLoaderAware {
...
@@ -136,6 +137,21 @@ public class MongoDataAutoConfiguration implements BeanClassLoaderAware {
throws
ClassNotFoundException
{
throws
ClassNotFoundException
{
MongoMappingContext
context
=
new
MongoMappingContext
();
MongoMappingContext
context
=
new
MongoMappingContext
();
context
.
setInitialEntitySet
(
getInitialEntitySet
(
beanFactory
));
context
.
setInitialEntitySet
(
getInitialEntitySet
(
beanFactory
));
Class
<?
extends
FieldNamingStrategy
>
fieldNamingStrategyClass
=
this
.
properties
.
getFieldNamingStrategy
();
if
(
fieldNamingStrategyClass
!=
null
)
{
try
{
context
.
setFieldNamingStrategy
(
fieldNamingStrategyClass
.
newInstance
());
}
catch
(
InstantiationException
e
)
{
throw
new
IllegalArgumentException
(
"Invalid custom FieldNamingStrategy "
+
"(is it abstract?) '"
+
fieldNamingStrategyClass
.
getName
()
+
"'"
,
e
);
}
catch
(
IllegalAccessException
e
)
{
throw
new
IllegalArgumentException
(
"Invalid custom FieldNamingStrategy "
+
"(is the constructor accessible?)'"
+
fieldNamingStrategyClass
.
getName
()
+
"'"
,
e
);
}
}
return
context
;
return
context
;
}
}
...
...
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java
View file @
dc6a2f05
...
@@ -21,6 +21,7 @@ import java.util.Arrays;
...
@@ -21,6 +21,7 @@ import java.util.Arrays;
import
java.util.List
;
import
java.util.List
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.data.mapping.model.FieldNamingStrategy
;
import
com.mongodb.MongoClient
;
import
com.mongodb.MongoClient
;
import
com.mongodb.MongoClientOptions
;
import
com.mongodb.MongoClientOptions
;
...
@@ -36,6 +37,7 @@ import com.mongodb.ServerAddress;
...
@@ -36,6 +37,7 @@ import com.mongodb.ServerAddress;
* @author Phillip Webb
* @author Phillip Webb
* @author Josh Long
* @author Josh Long
* @author Andy Wilkinson
* @author Andy Wilkinson
* @author Eddú Meléndez
*/
*/
@ConfigurationProperties
(
prefix
=
"spring.data.mongodb"
)
@ConfigurationProperties
(
prefix
=
"spring.data.mongodb"
)
public
class
MongoProperties
{
public
class
MongoProperties
{
...
@@ -82,6 +84,11 @@ public class MongoProperties {
...
@@ -82,6 +84,11 @@ public class MongoProperties {
*/
*/
private
char
[]
password
;
private
char
[]
password
;
/**
* Fully qualified name of the FieldNamingStrategy to use.
*/
private
Class
<?
extends
FieldNamingStrategy
>
fieldNamingStrategy
;
public
String
getHost
()
{
public
String
getHost
()
{
return
this
.
host
;
return
this
.
host
;
}
}
...
@@ -122,6 +129,14 @@ public class MongoProperties {
...
@@ -122,6 +129,14 @@ public class MongoProperties {
this
.
password
=
password
;
this
.
password
=
password
;
}
}
public
Class
<?
extends
FieldNamingStrategy
>
getFieldNamingStrategy
()
{
return
this
.
fieldNamingStrategy
;
}
public
void
setFieldNamingStrategy
(
Class
<?
extends
FieldNamingStrategy
>
fieldNamingStrategy
)
{
this
.
fieldNamingStrategy
=
fieldNamingStrategy
;
}
public
void
clearPassword
()
{
public
void
clearPassword
()
{
if
(
this
.
password
==
null
)
{
if
(
this
.
password
==
null
)
{
return
;
return
;
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoDataAutoConfigurationTests.java
View file @
dc6a2f05
/*
/*
* Copyright 2012-201
4
the original author or authors.
* Copyright 2012-201
5
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -21,6 +21,7 @@ import java.util.Set;
...
@@ -21,6 +21,7 @@ import java.util.Set;
import
org.hamcrest.Matchers
;
import
org.hamcrest.Matchers
;
import
org.junit.After
;
import
org.junit.After
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.boot.autoconfigure.AutoConfigurationPackages
;
import
org.springframework.boot.autoconfigure.AutoConfigurationPackages
;
import
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
;
import
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
;
...
@@ -30,6 +31,9 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
...
@@ -30,6 +31,9 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.convert.converter.Converter
;
import
org.springframework.core.convert.converter.Converter
;
import
org.springframework.data.mapping.model.CamelCaseAbbreviatingFieldNamingStrategy
;
import
org.springframework.data.mapping.model.FieldNamingStrategy
;
import
org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy
;
import
org.springframework.data.mongodb.core.MongoTemplate
;
import
org.springframework.data.mongodb.core.MongoTemplate
;
import
org.springframework.data.mongodb.core.convert.CustomConversions
;
import
org.springframework.data.mongodb.core.convert.CustomConversions
;
import
org.springframework.data.mongodb.core.mapping.MongoMappingContext
;
import
org.springframework.data.mongodb.core.mapping.MongoMappingContext
;
...
@@ -37,6 +41,7 @@ import org.springframework.data.mongodb.gridfs.GridFsTemplate;
...
@@ -37,6 +41,7 @@ import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import
org.springframework.test.util.ReflectionTestUtils
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
com.mongodb.Mongo
;
import
com.mongodb.Mongo
;
import
org.junit.rules.ExpectedException
;
import
static
org
.
hamcrest
.
Matchers
.
hasSize
;
import
static
org
.
hamcrest
.
Matchers
.
hasSize
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
...
@@ -51,6 +56,9 @@ import static org.junit.Assert.assertTrue;
...
@@ -51,6 +56,9 @@ import static org.junit.Assert.assertTrue;
*/
*/
public
class
MongoDataAutoConfigurationTests
{
public
class
MongoDataAutoConfigurationTests
{
@Rule
public
final
ExpectedException
thrown
=
ExpectedException
.
none
();
private
AnnotationConfigApplicationContext
context
;
private
AnnotationConfigApplicationContext
context
;
@After
@After
...
@@ -103,6 +111,38 @@ public class MongoDataAutoConfigurationTests {
...
@@ -103,6 +111,38 @@ public class MongoDataAutoConfigurationTests {
City
.
class
);
City
.
class
);
}
}
@Test
public
void
defaultFieldNamingStrategy
()
{
testFieldNamingStrategy
(
null
,
PropertyNameFieldNamingStrategy
.
class
);
}
@Test
public
void
customFieldNamingStrategy
()
{
testFieldNamingStrategy
(
CamelCaseAbbreviatingFieldNamingStrategy
.
class
.
getName
(),
CamelCaseAbbreviatingFieldNamingStrategy
.
class
);
}
@Test
public
void
interfaceFieldNamingStrategy
()
{
thrown
.
expectMessage
(
"Invalid custom FieldNamingStrategy"
);
testFieldNamingStrategy
(
FieldNamingStrategy
.
class
.
getName
(),
null
);
}
public
void
testFieldNamingStrategy
(
String
strategy
,
Class
<?
extends
FieldNamingStrategy
>
expectedType
)
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
if
(
strategy
!=
null
)
{
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"spring.data.mongodb.field-naming-strategy:"
+
strategy
);
}
this
.
context
.
register
(
PropertyPlaceholderAutoConfiguration
.
class
,
MongoAutoConfiguration
.
class
,
MongoDataAutoConfiguration
.
class
);
this
.
context
.
refresh
();
MongoMappingContext
mappingContext
=
this
.
context
.
getBean
(
MongoMappingContext
.
class
);
FieldNamingStrategy
fieldNamingStrategy
=
(
FieldNamingStrategy
)
ReflectionTestUtils
.
getField
(
mappingContext
,
"fieldNamingStrategy"
);
assertEquals
(
expectedType
,
fieldNamingStrategy
.
getClass
());
}
@SuppressWarnings
({
"unchecked"
,
"rawtypes"
})
@SuppressWarnings
({
"unchecked"
,
"rawtypes"
})
private
static
void
assertDomainTypesDiscovered
(
MongoMappingContext
mappingContext
,
private
static
void
assertDomainTypesDiscovered
(
MongoMappingContext
mappingContext
,
Class
<?>...
types
)
{
Class
<?>...
types
)
{
...
...
spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc
View file @
dc6a2f05
...
@@ -322,6 +322,7 @@ content into your application; rather pick only the properties that you need.
...
@@ -322,6 +322,7 @@ content into your application; rather pick only the properties that you need.
spring.data.mongodb.username=
spring.data.mongodb.username=
spring.data.mongodb.password=
spring.data.mongodb.password=
spring.data.mongodb.repositories.enabled=true # if spring data repository support is enabled
spring.data.mongodb.repositories.enabled=true # if spring data repository support is enabled
spring.data.mongodb.field-naming-strategy= # fully qualified name of the FieldNamingStrategy to use
# JPA ({sc-spring-boot-autoconfigure}/orm/jpa/JpaBaseConfiguration.{sc-ext}[JpaBaseConfiguration], {sc-spring-boot-autoconfigure}/orm/jpa/HibernateJpaAutoConfiguration.{sc-ext}[HibernateJpaAutoConfiguration])
# JPA ({sc-spring-boot-autoconfigure}/orm/jpa/JpaBaseConfiguration.{sc-ext}[JpaBaseConfiguration], {sc-spring-boot-autoconfigure}/orm/jpa/HibernateJpaAutoConfiguration.{sc-ext}[HibernateJpaAutoConfiguration])
spring.jpa.properties.*= # properties to set on the JPA connection
spring.jpa.properties.*= # properties to set on the JPA connection
...
...
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