-
Notifications
You must be signed in to change notification settings - Fork 486
PR #382: Added spring-boot autoconfiguration support. #526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
garethahealy
merged 5 commits into
DozerMapper:master
from
vadeg:feature/spring-boot-support
Nov 19, 2017
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
13772a7
Added spring-boot autoconfiguration support.
vadeg 94a10fc
Some review comments fixes.
vadeg ba10036
Fix review comments.
vadeg ff62844
Removed useless ApplicationContext dependency from DozerAutoConfigura…
vadeg f015d77
Move "spring-boot-dependencies" into dozer "bom-dependencies".
vadeg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
dozer-spring-boot-auto-configuration/dozer-spring-boot-autoconfigure/pom.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
|
||
Copyright 2005-2017 Dozer Project | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>com.github.dozermapper</groupId> | ||
<artifactId>dozer-spring-boot-auto-configuration</artifactId> | ||
<version>6.2.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>dozer-spring-boot-autoconfigure</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<name>Dozer :: Spring Boot AutoConfiguration :: AutoConfiguration</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-configuration-processor</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-autoconfigure</artifactId> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't all Spring dependencies be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made all of them |
||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-core</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-logging</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.github.dozermapper</groupId> | ||
<artifactId>dozer-spring</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.github.dozermapper</groupId> | ||
<artifactId>dozer-core</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
</dependencies> | ||
</project> |
63 changes: 63 additions & 0 deletions
63
...src/main/java/com/github/dozermapper/springboot/autoconfigure/DozerAutoConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2005-2017 Dozer Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.dozermapper.springboot.autoconfigure; | ||
|
||
import java.io.IOException; | ||
|
||
import com.github.dozermapper.spring.DozerBeanMapperFactoryBean; | ||
import org.dozer.Mapper; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
|
||
/** | ||
* Dozer spring auto configuration. | ||
* | ||
*/ | ||
@Configuration | ||
@ConditionalOnClass({DozerBeanMapperFactoryBean.class, Mapper.class}) | ||
@ConditionalOnMissingBean(Mapper.class) | ||
@EnableConfigurationProperties(DozerConfigurationProperties.class) | ||
public class DozerAutoConfiguration { | ||
|
||
private final DozerConfigurationProperties configurationProperties; | ||
|
||
/** | ||
* Constructor for creating auto configuration. | ||
* @param configurationProperties properties | ||
*/ | ||
@Autowired | ||
public DozerAutoConfiguration(DozerConfigurationProperties configurationProperties) { | ||
this.configurationProperties = configurationProperties; | ||
} | ||
|
||
/** | ||
* Creates default Dozer mapper | ||
* @return Dozer mapper | ||
* @throws IOException if there is an exception during initialization. | ||
*/ | ||
@Bean | ||
public DozerBeanMapperFactoryBean dozerMapper() throws IOException { | ||
DozerBeanMapperFactoryBean factoryBean = new DozerBeanMapperFactoryBean(); | ||
factoryBean.setMappingFiles(configurationProperties.getMappingFiles()); | ||
return factoryBean; | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
...in/java/com/github/dozermapper/springboot/autoconfigure/DozerConfigurationProperties.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2005-2017 Dozer Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.dozermapper.springboot.autoconfigure; | ||
|
||
import java.util.Arrays; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.core.io.Resource; | ||
|
||
|
||
|
||
/** | ||
* Dozer configuration properties. | ||
*/ | ||
@ConfigurationProperties(prefix = "dozer") | ||
public class DozerConfigurationProperties { | ||
|
||
/** | ||
* Mapping files configuration. | ||
* For example <code>classpath:*.dozer.xml</code>. | ||
*/ | ||
private Resource[] mappingFiles = new Resource[] {}; | ||
|
||
/** | ||
* Mapping files configuration. | ||
* | ||
* @return mapping files | ||
*/ | ||
public Resource[] getMappingFiles() { | ||
return Arrays.copyOf(mappingFiles, mappingFiles.length); | ||
} | ||
|
||
/** | ||
* Set mapping files configuration. For example <code>classpath:*.dozer.xml</code>. | ||
* | ||
* @param mappingFiles dozer mapping files | ||
* @return dozer properties | ||
*/ | ||
public DozerConfigurationProperties setMappingFiles(Resource[] mappingFiles) { | ||
this.mappingFiles = Arrays.copyOf(mappingFiles, mappingFiles.length); | ||
return this; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...configure/src/main/java/com/github/dozermapper/springboot/autoconfigure/package-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright 2005-2017 Dozer Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/** | ||
* Spring boot auto configuration classes. | ||
*/ | ||
package com.github.dozermapper.springboot.autoconfigure; |
2 changes: 2 additions & 0 deletions
2
...onfiguration/dozer-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ | ||
com.github.dozermapper.springboot.autoconfigure.DozerAutoConfiguration |
97 changes: 97 additions & 0 deletions
97
.../src/test/java/com/github/dozermaper/springboot/autoconfigure/AutoConfigurationTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright 2005-2017 Dozer Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.dozermaper.springboot.autoconfigure; | ||
|
||
import com.github.dozermaper.springboot.autoconfigure.vo.Dest; | ||
import com.github.dozermaper.springboot.autoconfigure.vo.Source; | ||
import com.github.dozermapper.springboot.autoconfigure.DozerAutoConfiguration; | ||
import com.github.dozermapper.spring.DozerBeanMapperFactoryBean; | ||
import org.dozer.Mapper; | ||
import org.dozer.metadata.ClassMappingMetadata; | ||
import org.junit.Test; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.SpringBootConfiguration; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class AutoConfigurationTests { | ||
|
||
@Test | ||
public void testDefaultMapperCreated() throws Exception { | ||
ConfigurableApplicationContext context = SpringApplication.run(Application.class); | ||
Mapper mapper = context.getBean(Mapper.class); | ||
assertNotNull(mapper); | ||
Map<String, Mapper> beansMap = context.getBeansOfType(Mapper.class); | ||
assertTrue(beansMap.containsKey("dozerMapper")); | ||
assertEquals(1, beansMap.keySet().size()); | ||
} | ||
|
||
@Test | ||
public void overrideDefaultMapper() throws Exception { | ||
ConfigurableApplicationContext context = SpringApplication.run(ConfigWithCustomMapper.class); | ||
Map<String, Mapper> beansMap = context.getBeansOfType(Mapper.class); | ||
assertEquals(1, beansMap.keySet().size()); | ||
assertTrue(beansMap.containsKey("customDozerMapper")); | ||
assertFalse(beansMap.containsKey("dozerMapper")); | ||
} | ||
|
||
@Test | ||
public void autoConfigurationDisabled() throws Exception { | ||
ConfigurableApplicationContext context = SpringApplication.run(DisabledAutoDozerConfiguration.class); | ||
Map<String, Mapper> beansMap = context.getBeansOfType(Mapper.class); | ||
assertEquals(0, beansMap.keySet().size()); | ||
} | ||
|
||
@Test | ||
public void testWithMappingFilesConfiguration() throws Exception { | ||
ConfigurableApplicationContext context = SpringApplication | ||
.run(Application.class, "--dozer.mappingFiles=classpath:/sample_mapping/sample_mapping.xml"); | ||
Mapper mapper = context.getBean(Mapper.class); | ||
assertNotNull(mapper); | ||
ClassMappingMetadata mapping = mapper.getMappingMetadata().getClassMapping(Source.class, Dest.class); | ||
assertNotNull("Mapping configuration not loaded", mapping); | ||
} | ||
|
||
@SpringBootConfiguration | ||
@EnableAutoConfiguration | ||
public static class Application { | ||
|
||
} | ||
|
||
@SpringBootConfiguration | ||
@EnableAutoConfiguration(exclude = DozerAutoConfiguration.class) | ||
public static class DisabledAutoDozerConfiguration { | ||
|
||
} | ||
|
||
@SpringBootConfiguration | ||
@EnableAutoConfiguration | ||
public static class ConfigWithCustomMapper { | ||
|
||
@Bean | ||
public DozerBeanMapperFactoryBean customDozerMapper() { | ||
return new DozerBeanMapperFactoryBean(); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...t-autoconfigure/src/test/java/com/github/dozermaper/springboot/autoconfigure/vo/Dest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2005-2017 Dozer Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.dozermaper.springboot.autoconfigure.vo; | ||
|
||
public class Dest { | ||
|
||
private String text; | ||
|
||
public String getText() { | ||
return text; | ||
} | ||
|
||
public Dest setText(String text) { | ||
this.text = text; | ||
return this; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...autoconfigure/src/test/java/com/github/dozermaper/springboot/autoconfigure/vo/Source.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2005-2017 Dozer Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.dozermaper.springboot.autoconfigure.vo; | ||
|
||
public class Source { | ||
|
||
private String text; | ||
|
||
public String getText() { | ||
return text; | ||
} | ||
|
||
public Source setText(String text) { | ||
this.text = text; | ||
return this; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this optional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spring boot documentation recommends to do like this. I think this is just for exclude transitive dependency from
spring-boot-configuration-processor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok