{"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\"><?xml version=\"1.0\" encoding=\"UTF-8\"?><blueprint xmlns=\"http:\/\/www.osgi.org\/xmlns\/blueprint\/v1.0.0\">\r\n    <bean id=\"createPersonHandler\" class=\"com.github.alien11689.osgi.mapstructblueprint.CreatePersonHandler\">\r\n        <argument ref=\"personRepository\"\/>\r\n        <argument ref=\"personMapperImpl\"\/>\r\n    <\/bean>\r\n    <bean id=\"personMapperImpl\" class=\"com.github.alien11689.osgi.mapstructblueprint.PersonMapperImpl\"\/>\r\n    <bean id=\"personRepository\" class=\"com.github.alien11689.osgi.mapstructblueprint.PersonRepository\"\/>\r\n<\/blueprint>\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\"><?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<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\">\r\n    ...\r\n    <build>\r\n        <plugins>\r\n            ...\r\n            <plugin>\r\n                <groupId>org.apache.maven.plugins<\/groupId>\r\n                <artifactId>maven-compiler-plugin<\/artifactId>\r\n                <version>${maven-compiler-plugin.version}<\/version>\r\n                <configuration>\r\n                    <source>1.8<\/source>\r\n                    <target>1.8<\/target>\r\n                    <compilerArgs>\r\n                        <compilerArg>\r\n                             -Amapstruct.defaultComponentModel=jsr330\r\n                        <\/compilerArg>\r\n                    <\/compilerArgs>\r\n                <\/configuration>\r\n            <\/plugin>\r\n            ...\r\n        <\/plugins>\r\n    <\/build>\r\n<\/project>\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}]}}