{"id":13301,"date":"2018-01-18T19:00:00","date_gmt":"2018-01-18T18:00:00","guid":{"rendered":"http:\/\/touk.pl\/blog\/?guid=611480407d3cf9e06e5000265989eca5"},"modified":"2022-08-01T09:38:52","modified_gmt":"2022-08-01T07:38:52","slug":"mapstruct-mapper-injection-in-osgi-blueprint","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2018\/01\/18\/mapstruct-mapper-injection-in-osgi-blueprint\/","title":{"rendered":"MapStruct mapper injection in OSGi Blueprint"},"content":{"rendered":"<h2 id=\"what-is-mapstruct\">What is MapStruct?<\/h2>\n<p>According to <a href=\"http:\/\/mapstruct.org\/\">MapStruct<\/a> website:<\/p>\n<blockquote><p>MapStruct is a code generator that greatly simplifies the implementation of mappings between Java bean types based on a convention over configuration approach. The generated mapping code uses plain method invocations and thus is fast, type-safe and easy to understand.<\/p><\/blockquote>\n<h2 id=\"inject-mapstruct-mapper-in-blueprint-osgi\">Inject MapStruct mapper in Blueprint OSGi<\/h2>\n<p>Such mappings are sometimes necessary in our integration projects. We also use OSGi to create our applications and <a href=\"http:\/\/aries.apache.org\/modules\/blueprint.html\">Blueprint<\/a> for dependency injection. <a href=\"http:\/\/aries.apache.org\/modules\/blueprint-maven-plugin.html\">Blueprin Maven Plugin<\/a> makes it very easy to use, providing annotation support.<\/p>\n<p>MapStruct supports component models like <code>cdi<\/code>, <code>spring<\/code> and <code>jsr330<\/code>, so generated classes could be used as beans. Fortunately, Blueprint Maven Plugin uses annotations from <a href=\"https:\/\/www.jcp.org\/en\/jsr\/detail?id=330\">JSR 330<\/a>, such as <code>Singleton<\/code> or <code>Named<\/code>.<\/p>\n<p>The only thing we have to do is to add property <code>componentModel<\/code> with value <code>jsr330<\/code> to a mapping interface:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">@Mapper(componentModel = \"jsr330\")\r\npublic interface PersonMapper {\r\n    Person toDomain(PersonDto personDto);\r\n}\r\n<\/pre>\n<p>and now we can inject <code>PersonMapper<\/code> to our beans:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">@Singleton\r\n@AllArgsConstructor\r\npublic class CreatePersonHandler {\r\n    private final PersonRepository personRepository;\r\n    private final PersonMapper personMapper;\r\n\r\n    \/\/ ...\r\n}\r\n<\/pre>\n<p>Blueprint Maven Plugin will generate an <code>XML<\/code> file with bean <code>PersonMapperImpl<\/code> and inject it to <code>CreatePersonHandler<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;&lt;blueprint xmlns=\"http:\/\/www.osgi.org\/xmlns\/blueprint\/v1.0.0\"&gt;\r\n    &lt;bean id=\"createPersonHandler\" class=\"com.github.alien11689.osgi.mapstructblueprint.CreatePersonHandler\"&gt;\r\n        &lt;argument ref=\"personRepository\"\/&gt;\r\n        &lt;argument ref=\"personMapperImpl\"\/&gt;\r\n    &lt;\/bean&gt;\r\n    &lt;bean id=\"personMapperImpl\" class=\"com.github.alien11689.osgi.mapstructblueprint.PersonMapperImpl\"\/&gt;\r\n    &lt;bean id=\"personRepository\" class=\"com.github.alien11689.osgi.mapstructblueprint.PersonRepository\"\/&gt;\r\n&lt;\/blueprint&gt;\r\n<\/pre>\n<h2 id=\"generate-all-mappers-with-jsr-330-annotations\">Generate all mappers with JSR 330 annotations<\/h2>\n<p>If you have multiple mappers and all of them should be beans, then you can simply add one compiler argument in configuration and all the mappers will have <code>@Singleton<\/code> and <code>@Named<\/code> annotations by default.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\"\r\n         xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n         xsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;\r\n    ...\r\n    &lt;build&gt;\r\n        &lt;plugins&gt;\r\n            ...\r\n            &lt;plugin&gt;\r\n                &lt;groupId&gt;org.apache.maven.plugins&lt;\/groupId&gt;\r\n                &lt;artifactId&gt;maven-compiler-plugin&lt;\/artifactId&gt;\r\n                &lt;version&gt;${maven-compiler-plugin.version}&lt;\/version&gt;\r\n                &lt;configuration&gt;\r\n                    &lt;source&gt;1.8&lt;\/source&gt;\r\n                    &lt;target&gt;1.8&lt;\/target&gt;\r\n                    &lt;compilerArgs&gt;\r\n                        &lt;compilerArg&gt;\r\n                             -Amapstruct.defaultComponentModel=jsr330\r\n                        &lt;\/compilerArg&gt;\r\n                    &lt;\/compilerArgs&gt;\r\n                &lt;\/configuration&gt;\r\n            &lt;\/plugin&gt;\r\n            ...\r\n        &lt;\/plugins&gt;\r\n    &lt;\/build&gt;\r\n&lt;\/project&gt;\r\n<\/pre>\n<h2 id=\"try-it-on-your-own\">Try it on your own<\/h2>\n<p>The code is available at <a href=\"https:\/\/github.com\/alien11689\/mapstruct-and-blueprint\">Github<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"What is MapStruct?According to MapStruct website:MapStruct is a code generator that greatly simplifies the implementation of mappings between Java bean types based on a convention over configuration approach. The generated mapping code uses plain metho&#8230;\n","protected":false},"author":54,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[68,633,44,107],"class_list":{"0":"post-13301","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design","7":"tag-java","8":"tag-mapstruct","9":"tag-osgi","10":"tag-xml"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/13301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/54"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=13301"}],"version-history":[{"count":9,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/13301\/revisions"}],"predecessor-version":[{"id":14739,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/13301\/revisions\/14739"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=13301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=13301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=13301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}