-
Notifications
You must be signed in to change notification settings - Fork 481
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
PR #382: Added spring-boot autoconfiguration support. #526
Changes from 3 commits
13772a7
94a10fc
ba10036
ff62844
f015d77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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>Spring Boot AutoConfiguration :: Dozer AutoConfiguration</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-configuration-processor</artifactId> | ||
<optional>true</optional> | ||
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. why is this optional? 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. Spring boot documentation recommends to do like this. I think this is just for exclude transitive dependency from 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. ok |
||
</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> | ||
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. junit is added by dozer-parent, so can be removed here. 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. ok |
||
<groupId>com.github.dozermapper</groupId> | ||
<artifactId>dozer-core</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
</dependencies> | ||
</project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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.ApplicationContext; | ||
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; | ||
|
||
private final ApplicationContext applicationContext; | ||
|
||
/** | ||
* Constructor for creating auto configuration. | ||
* @param configurationProperties properties | ||
* @param applicationContext context | ||
*/ | ||
@Autowired | ||
public DozerAutoConfiguration(DozerConfigurationProperties configurationProperties, | ||
ApplicationContext applicationContext) { | ||
this.configurationProperties = configurationProperties; | ||
this.applicationContext = applicationContext; | ||
} | ||
|
||
/** | ||
* 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()); | ||
factoryBean.setApplicationContext(applicationContext); | ||
return factoryBean; | ||
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. Just wondering if the Mapper instance should be returned, instead of the spring factory. Or does spring wire together the Mapper by its "magic" :) 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.
BTW I have removed |
||
} | ||
|
||
} |
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[] {}; | ||
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. It would be nice to have a javadoc on this field (and maybe event skipping javadocs for getter and setter, to avoid unnecessary duplication). The reason to have a javadoc on the field is to generate a better meta-data file to help IDE to provide richer support for these properties, compare currently generated file
to the one if field has a javadoc:
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. I agree. But to avoid unnecessary duplication 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. In general I do not see any problems in tuning tools to suite the project needs. In this particular case I would recommend to use |
||
|
||
/** | ||
* 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; | ||
} | ||
} |
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; |
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 |
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(); | ||
} | ||
} | ||
} |
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; | ||
} | ||
} |
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# | ||
# 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.main.banner-mode=off |
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.
Can you update name to match other modules, i.e.: Dozer :: Module :: Sub Module