{"id":3085,"date":"2019-02-25T07:27:14","date_gmt":"2019-02-25T07:27:14","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=3085"},"modified":"2019-02-25T11:51:44","modified_gmt":"2019-02-25T11:51:44","slug":"configuration","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/configuration\/","title":{"rendered":"@Configuration, @Configurble"},"content":{"rendered":"<p><strong>@Configuration<\/strong> &#8211; @Configuration as a replacement to the XML based configuration for configuring spring beans.So instead of an xml file we write a class and annotate that with @Configuration and define the beans in it using @Bean annotation on the methods.It is just another way of configuration Indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.<\/p>\n<p>Use @Configuration annotation on top of any class to declare that this class provides one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.<\/p>\n<p><strong>AppConfig.java<\/strong> <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Configuration\r\npublic class AppConfig {\r\n \r\n    @Bean(name=&quot;demoService&quot;)\r\n    public DemoClass service()\r\n    {\r\n        \r\n    }\r\n}\r\n<\/pre>\n<p><strong>pom.xml<\/strong><br \/>\nThe following dependency should be added to pom.xml before using @configuration annotation to get the bean from the context<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;dependency&gt;\r\n\t\t&lt;groupId&gt;org.springframework&lt;\/groupId&gt;\r\n\t\t&lt;artifactId&gt;spring-context&lt;\/artifactId&gt;\r\n\t\t&lt;version&gt;5.0.6.RELEASE&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class VerifySpringCoreFeature\r\n{\r\n    public static void main(String&#x5B;] args)\r\n    {\r\n        ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfiguration.class); \r\n        DemoManager  obj = (DemoManager) context.getBean(&quot;demoService&quot;); \r\n        System.out.println( obj.getServiceName() );\r\n    }\r\n}\r\n<\/pre>\n<p>What if I want to ensure the beans are already loaded even before requested and fail-fast<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport org.springframework.context.annotation.AnnotationConfigApplicationContext;\r\npublic class MySpringApp {\r\n\tpublic static void main(String&#x5B;] args) {\r\n\t\tAnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();\r\n\t\tctx.register(MyConfiguration.class);\r\n\t\tctx.refresh();\r\n\t\tMyBean mb1 = ctx.getBean(MyBean.class);\r\n\t\tMyBean mb2 = ctx.getBean(MyBean.class);\r\n\t\tctx.close();\r\n\t}\r\n}\r\n<\/pre>\n<p>In the above example Spring loads beans into it\u2019s context before we have even requested it. This is to make sure all the beans are properly configured and application fail-fast if something goes wrong.<\/p>\n<p>Now what will happen if we uncomment the @Configuration annotation in the above class. In this case, if we make a call to myBean() method then it will be a plain java method call and we will get a new instance of MyBean and it won\u2019t remain singleton.<\/p>\n<p><strong>@Configurble<\/strong> &#8211; How to inject dependencies into objects that are not created by Spring<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class CarSalon {\r\n    \/\/...\r\n    public void testDrive() {\r\n        Car car = new Car();\r\n        car.startCar();\r\n    }\r\n}\r\n \r\n@Component\r\npublic class Car {\r\n    @Autowired\r\n    private Engine engine;\r\n    @Autowired\r\n    private Transmission transmission;\r\n \r\n    public void startCar() {\r\n        transmission.setGear(1);\r\n        engine.engineOn();\r\n        System.out.println(&quot;Car started&quot;);\r\n    }\r\n}\r\n \r\n@Component\r\npublic class Engine {\r\n\/\/...\r\n}\r\n \r\n@Component\r\npublic class Transmission {\r\n\/\/...\r\n}\r\n<\/pre>\n<p>In the above example<\/p>\n<ol>\n<li>We try to create a Object for Car class using new operator<\/li>\n<li>How ever objects created using new operator are not container managed rather Java Runtime Managed<\/li>\n<li>Trying to access the startCar method using car object will throw <strong>NullPointerException<\/strong><\/li>\n<li>However using @Configurable annotation will tell Spring to inject dependencies into the object before the constructor is run<\/li>\n<li>You need to have these JAR files in pom.xml inorder to make it work.aspectj-x.x.x.jar, aspectjrt.jar, aspectjveawer-x.x.x.jar<\/li>\n<\/ol>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Configurable(preConstruction = true)\r\n@Component\r\npublic class Car {\r\n \r\n    @Autowired\r\n    private Engine engine;\r\n    @Autowired\r\n    private Transmission transmission;\r\n \r\n    public void startCar() {\r\n        transmission.setGear(1);\r\n        engine.engineOn();\r\n \r\n        System.out.println(&quot;Car started&quot;);\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>@Configuration &#8211; @Configuration as a replacement to the XML based configuration for configuring spring beans.So instead of an xml file we write a class and annotate that with @Configuration and define the beans in it using @Bean annotation on the methods.It is just another way of configuration Indicates that a class declares one or more&hellip; <a href=\"https:\/\/codethataint.com\/blog\/configuration\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[262],"tags":[263],"class_list":["post-3085","post","type-post","status-publish","format-standard","hentry","category-annotations","tag-annotations"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3085","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=3085"}],"version-history":[{"count":3,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3085\/revisions"}],"predecessor-version":[{"id":3092,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3085\/revisions\/3092"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}