{"id":3933,"date":"2020-07-06T16:31:07","date_gmt":"2020-07-06T11:01:07","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3933"},"modified":"2020-07-06T16:31:08","modified_gmt":"2020-07-06T11:01:08","slug":"spring-annotations-configuration","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/spring-annotations-configuration\/","title":{"rendered":"Spring Annotations Configuration"},"content":{"rendered":"\n<p>It is a Java-based configuration option that enables you to write the most of your Spring configuration without using XML but with the help of few Java-based annotations. The Spring annotation wiring is not turned on in <a href=\"https:\/\/www.h2kinfosys.com\/blog\/what-is-the-spring-framework-in-java\/\">Spring Framework<\/a>. Therefore, you need to enable it before you can use the Spring annotation-based wiring in the Spring Configuration file.\u00a0<\/p>\n\n\n\n<p>Below is the configuration file in case you want to use Annotation in your application:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>@Configuration and @Bean Annotations<\/li><\/ul>\n\n\n\n<p>By <a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/java\/annotations\/\" rel=\"nofollow noopener\" target=\"_blank\">using annotations<\/a>, Annotating a class using @Configuration, which will indicate that the class is managed by the Spring IoC container as a source of bean definitions.\u00a0<\/p>\n\n\n\n<p>The other one @Bean Annotation which inform Spring that a method which annotated herewith @Bean<\/p>\n\n\n<p><\/p>\n<pre><span style=\"font-weight: 400;\">@Configuration<\/span><br><br><span style=\"font-weight: 400;\">public class HelloWorldDemo {<\/span><br><br><span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;@Bean&nbsp;<\/span><br><br><span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;public Hello hello(){<\/span><br><br><span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return new Hello();<\/span><br><br><span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;}<\/span><br><br><span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><\/p>\n\n\n<h3 class=\"wp-block-heading\"><strong>Here, @Bean it works as bean ID<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;beans&gt;\n&nbsp;&nbsp;&nbsp;&lt;bean id = \"hello\" class = \"com.demo.Hello\" \/&gt;\n&lt;\/beans&gt;&nbsp;\n\npublic static void main(String[] arg) {\n&nbsp;&nbsp;&nbsp;ApplicationContext cnt = new AnnotationConfigApplicationContext(HelloWorldDemo.class);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hello hello= cnt.getBean(Hello.class);\n&nbsp;&nbsp;&nbsp;hello.setMessage(\"Hello !\");\n&nbsp;&nbsp;&nbsp;hello.getMessage();\n}\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Here, we can load various configuration classes as follows<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">public static void main(String[] args) {\n&nbsp;&nbsp;&nbsp;AnnotationConfigApplicationContext cnt = new AnnotationConfigApplicationContext();\n&nbsp;&nbsp;&nbsp;cnt.register(AppConfig.class, OtherConfig.class);\n&nbsp;&nbsp;&nbsp;cnt.register(AdditionalConfig.class);\n&nbsp;&nbsp;&nbsp;cnt.refresh();\n\n&nbsp;&nbsp;&nbsp;MyService myService = cnt.getBean(MyService.class);\n&nbsp;&nbsp;&nbsp;myService.doStuff();\n}\n\n@Configuration\npublic class HelloWorldDemo {\n&nbsp;&nbsp;&nbsp;@Bean&nbsp;\n&nbsp;&nbsp;&nbsp;public Hello hello(){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return new Hello();\n&nbsp;&nbsp;&nbsp;}\n}\n<\/pre>\n\n\n\n<p><strong><em>Here is the content of HelloDemo.java file<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class HelloDemo{\n&nbsp;&nbsp;&nbsp;private String message;\n&nbsp;&nbsp;&nbsp;public void setMessage(String message){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.message&nbsp; = message;\n&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;public void getMessage(){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Your Message : \" + message);\n&nbsp;&nbsp;&nbsp;}\n}\nFollowing is the content of the MainDemo.java file\npublic class MainDemo {\n&nbsp;&nbsp;&nbsp;public static void main(String[] args) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ApplicationContext cnt =&nbsp; new AnnotationConfigApplicationContext(HelloWorldDemo.class);\n&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hello hello= cnt.getBean(Hello.class);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hello.setMessage(\"Hello!\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hello.getMessage();\n&nbsp;&nbsp;&nbsp;}\n}<\/pre>\n\n\n\n<p><strong>Your Message: Hello!<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Now, Injecting the Bean Dependencies<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">@Configuration\npublic class AppConfigDemo {\n&nbsp;&nbsp;&nbsp;@Bean\n&nbsp;&nbsp;&nbsp;public Fun fun() {\n&nbsp;&nbsp;&nbsp;return new Fun(bar());\n&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;@Bean\n&nbsp;&nbsp;&nbsp;public Foo far() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return new Foo();\n&nbsp;&nbsp;&nbsp;}\n}\n<\/pre>\n\n\n\n<p>Here, the fun bean receives a reference to foo via the constructor injection.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is a Java-based configuration option that enables you to write the most of your Spring configuration without using XML but with the help of few Java-based annotations. The Spring annotation wiring is not turned on in Spring Framework. Therefore, you need to enable it before you can use the Spring annotation-based wiring in the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3954,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[1060,1061,1059],"class_list":["post-3933","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","tag-annotations-configuration","tag-injecting-the-bean-dependencies","tag-spring-annotations"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3933","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=3933"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3933\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3954"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}