Skip to content
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

Merged
merged 5 commits into from
Nov 19, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
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>
Copy link
Collaborator

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


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

Choose a reason for hiding this comment

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

junit is added by dozer-parent, so can be removed here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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" :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FactoryBean should be returned. @Bean annotation has the same semantics as <bean> xml tag. The following steps are executed by spring itself.

BTW I have removed ApplicationContext from DozerAutoConfiguration because DozerBeanMapperFactoryBean implements ApplicationContextAware. That means it is not necessary to call factoryBean.setApplicationContext() because it will be injected by spring itself into DozerBeanMapperFactoryBean instance.

}

}
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[] {};
Copy link
Contributor

@orange-buffalo orange-buffalo Oct 31, 2017

Choose a reason for hiding this comment

The 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

"properties": [
    {
      "sourceType": "com.github.dozermapper.springboot.autoconfigure.DozerConfigurationProperties",
      "defaultValue": [],
      "name": "dozer.mapping-files",
      "type": "org.springframework.core.io.Resource[]"
    }
  ]

to the one if field has a javadoc:

"properties": [
    {
      "sourceType": "com.github.dozermapper.springboot.autoconfigure.DozerConfigurationProperties",
      "defaultValue": [],
      "name": "dozer.mapping-files",
      "description": "Mapping files configuration.\n For example <code>classpath:*.dozer.xml<\/code>.",
      "type": "org.springframework.core.io.Resource[]"
    }
  ]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree. But to avoid unnecessary duplication maven-checkstyle-plugin configuration should be updated. Are you fine with that?

Copy link
Contributor

Choose a reason for hiding this comment

The 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 @SuppressWarnings("checkstyle:xxx") in code + enabling SuppressWarningsFilter in CheckStyle config.


/**
* 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
Loading