Skip to content

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
merged 5 commits into from
Nov 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions bom-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<protobuf-java.version>3.3.0</protobuf-java.version>
<osgi.version>6.0.0</osgi.version>
<slf4j.version>1.7.25</slf4j.version>
<spring-boot.version>1.5.8.RELEASE</spring-boot.version>
<spring.version>4.3.10.RELEASE</spring.version>
<woodstox-core-asl.version>4.4.1</woodstox-core-asl.version>
<xmlbeans.version>2.6.0</xmlbeans.version>
Expand Down Expand Up @@ -265,6 +266,13 @@
<artifactId>xmlbeans</artifactId>
<version>${xmlbeans.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
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>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this optional?

Copy link
Contributor Author

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

Copy link
Collaborator

Choose a reason for hiding this comment

The 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>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't all Spring dependencies be provided?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made all of them optional.

<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>
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;
}

}
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;
}
}
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;
}
}
Loading