{"id":2390,"date":"2014-02-16T09:11:20","date_gmt":"2014-02-16T08:11:20","guid":{"rendered":"http:\/\/sahits.ch\/blog\/?p=2390"},"modified":"2014-02-16T09:11:20","modified_gmt":"2014-02-16T08:11:20","slug":"package-private-beans","status":"publish","type":"post","link":"http:\/\/sahits.ch\/blog\/blog\/2014\/02\/16\/package-private-beans\/","title":{"rendered":"Package private beans"},"content":{"rendered":"<p>Thinking about separation in my code the question of package private beans popped up. The general pattern here is to have interfaces defined. The implementing classes however are package private. Instances are generated through a factory. I looked around to find some documentation how this can be done with a Spring context, but found none, so here it is. Special interest is the Spring configuration with Java.<\/p>\n<p>The package private beans are annotated and the classpath is scanned. These classes are discovered, as this basically involves reflection. The beans however cannot be instantiated in the configuration, as the beans are not visible in the package of the configuration class. This is where the factory comes in. The factory resides in the same package as the beans however is a public @Service and so is available in the application context.<\/p>\n<p>I tried this out with two beans:<\/p>\n<pre class=\"brush:java\">package ch.sahits.spring.bean.internal;\r\n\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.stereotype.Component;\r\n\r\nimport java.util.Random;\r\n\r\n@Component\r\nclass PackagePrivateBean {\r\n    @Autowired\r\n    private Random rnd;\r\n\r\n    public Random getRnd() {\r\n        return rnd;\r\n    }\r\n}<\/pre>\n<pre class=\"brush:java\">package ch.sahits.spring.bean.internal;\r\n\r\nimport org.springframework.context.annotation.Scope;\r\nimport org.springframework.stereotype.Component;\r\n\r\n@Component\r\n@Scope(value = \"prototype\")\r\nclass PackagePrivatePrototype {\r\n    private final int a;\r\n\r\n    public PackagePrivatePrototype(int a) {\r\n        this.a = a;\r\n    }\r\n\r\n    public int getA() {\r\n        return a;\r\n    }\r\n}<\/pre>\n<p>The configuration looks like this:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"brush:java\">package ch.sahits.spring;\r\n\r\nimport org.springframework.context.annotation.Bean;\r\nimport org.springframework.context.annotation.ComponentScan;\r\nimport org.springframework.context.annotation.Configuration;\r\n\r\nimport java.util.Random;\r\n\r\n@Configuration\r\n@ComponentScan(basePackages = {\"ch.sahits.spring.bean.internal\"})\r\npublic class PackagePrivateSpringConfiguration {\r\n    @Bean\r\n    public Random rnd() {\r\n        return new Random();\r\n    }\r\n\r\n}<\/pre>\n<p>The factory:<\/p>\n<pre class=\"brush:java\">package ch.sahits.spring.bean.internal;\r\n\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.context.ApplicationContext;\r\nimport org.springframework.stereotype.Service;\r\n\r\n@Service\r\npublic class PackagePrivateBeanFactory {\r\n\r\n    @Autowired\r\n    private ApplicationContext context;\r\n\r\n    public PackagePrivateBean getBean() {\r\n        return context.getBean(PackagePrivateBean.class);\r\n    }\r\n\r\n    public PackagePrivatePrototype getBean(int i) {\r\n        Object[] args = new Object[]{Integer.valueOf(i)};\r\n        return (PackagePrivatePrototype)context.getBean(\"packagePrivatePrototype\", args);\r\n    }\r\n}<\/pre>\n<p>I tested this with a Test-Case:<\/p>\n<pre class=\"brush:java\">package ch.sahits.spring;\r\n\r\nimport ch.sahits.spring.bean.internal.PackagePrivateBeanFactory;\r\nimport org.junit.Test;\r\nimport org.junit.runner.RunWith;\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.test.context.ContextConfiguration;\r\nimport org.springframework.test.context.junit4.SpringJUnit4ClassRunner;\r\nimport org.springframework.test.context.support.AnnotationConfigContextLoader;\r\n\r\nimport static org.junit.Assert.assertNotNull;\r\n\r\n@RunWith(SpringJUnit4ClassRunner.class)\r\n@ContextConfiguration(classes=PackagePrivateSpringConfiguration.class, loader=AnnotationConfigContextLoader.class)\r\npublic class PrivatePackageConfigurationTest {\r\n    @Autowired\r\n    private PackagePrivateBeanFactory factory;\r\n\r\n    @Test\r\n    public void factoryShouldNotBeNull() {\r\n        assertNotNull(factory);\r\n    }\r\n\r\n    @Test\r\n    public void packagePrivateComponentShouldNotBeNull() {\r\n        Object o = factory.getBean();\r\n        assertNotNull(o);\r\n    }\r\n\r\n    @Test\r\n    public void packagePrivatePrototypeShouldNotBeNull() {\r\n        Object o = factory.getBean(47);\r\n        assertNotNull(o);\r\n    }\r\n}<\/pre>\n<p>The curious thing is that the <code>PackagePrivateBean <\/code>is by default of scope Singleton and thereby has to be created on application context startup. As this happens through reflection of the default constructor this is even possible even though the bean itself is not visible and can therefore not be defined through the configuration class. This is a case, where you get an instance through the package scan that cannot be done through explicit definition. This applies in the same manner for XML configuration. Defining a package private bean in XML would cause a runtime exception when reading the XML application context.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Thinking about separation in my code the question of package private beans popped up. The general pattern here is to have interfaces defined. The implementing classes however are package private. Instances are generated through a factory. I looked around to find some documentation how this can be done with a Spring context, but found none, &hellip; <a href=\"http:\/\/sahits.ch\/blog\/blog\/2014\/02\/16\/package-private-beans\/\" class=\"more-link\"><span class=\"screen-reader-text\">\u201ePackage private beans\u201c <\/span>weiterlesen<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[293,294,301,269],"class_list":["post-2390","post","type-post","status-publish","format-standard","hentry","category-java","tag-bean","tag-instanciation","tag-java","tag-spring"],"_links":{"self":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/2390","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/comments?post=2390"}],"version-history":[{"count":1,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/2390\/revisions"}],"predecessor-version":[{"id":2391,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/2390\/revisions\/2391"}],"wp:attachment":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/media?parent=2390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/categories?post=2390"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/tags?post=2390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}