{"id":57367,"date":"2023-05-19T12:41:18","date_gmt":"2023-05-19T07:11:18","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=57367"},"modified":"2023-09-06T13:22:35","modified_gmt":"2023-09-06T07:52:35","slug":"top-10-spring-boot-annotations","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/top-10-spring-boot-annotations\/","title":{"rendered":"Top 10 Spring Boot Annotations."},"content":{"rendered":"<div class=\"gz re vm vn vo\">\n<p>&nbsp;<\/p>\n<div class=\"n p\">\n<div class=\"cz bm da db dc dd\">\n<p id=\"6f16\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" style=\"text-align: left;\" data-selectable-paragraph=\"\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-57366 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/05\/Springboot_header-1024x538.png\" alt=\"\" width=\"625\" height=\"328\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/05\/Springboot_header-1024x538.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/05\/Springboot_header-300x158.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/05\/Springboot_header-768x403.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/05\/Springboot_header-624x328.png 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/05\/Springboot_header.png 1200w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p><strong>SpringBootApplication<\/strong><strong class=\"mj gl\">:<\/strong> The primary annotation used in Spring Boot applications. It combines three other annotations: \u00a0@Configuration, @EnableAutoConfiguration, and @ComponentScan.<\/p>\n<ul class=\"\" style=\"text-align: left;\">\n<li id=\"e98e\" class=\"zm zn vq mj b zo zp zq zr zs zt zu zv abk zw zx zy abl zz aba abb abm abc abd abe abf abn abo abp bp\" data-selectable-paragraph=\"\">@Configuration: Indicates that the class is a source of bean definitions. This is used to define Spring Beans and other configuration settings.<\/li>\n<li class=\"zm zn vq mj b zo zp zq zr zs zt zu zv abk zw zx zy abl zz aba abb abm abc abd abe abf abn abo abp bp\" data-selectable-paragraph=\"\">@EnableAutoConfiguration:\u00a0Enables Spring Boot&#8217;s auto-configuration feature, which automatically configures Spring Beans based on the dependencies included in the project.<\/li>\n<li id=\"3fc9\" class=\"zm zn vq mj b zo abq zq zr zs abr zu zv abk abs zx zy abl abt aba abb abm abu abd abe abf abn abo abp bp\" data-selectable-paragraph=\"\">@ComponentScan: Scans the specified packages for Spring components, such as controllers, services, and repositories.<\/li>\n<\/ul>\n<p id=\"495a\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" style=\"text-align: left;\" data-selectable-paragraph=\"\">By combining these three annotations,\u00a0<code class=\"cn abg abh abi abj b\">@SpringBootApplication<\/code>\u00a0provides a convenient way to bootstrap a Spring Boot application. It is typically used to annotate the main class of a Spring Boot application, which contains the\u00a0<code class=\"cn abg abh abi abj b\">main<\/code>\u00a0method. This annotation enables auto-configuration, component scanning, and other Spring features that make it easy to develop and deploy Spring Boot applications.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"n p abv fb ie abw\" style=\"text-align: left;\" role=\"separator\"><\/div>\n<div class=\"gz re vm vn vo\" style=\"text-align: left;\">\n<div class=\"n p\">\n<div class=\"cz bm da db dc dd\">\n<p id=\"daed\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\"><code class=\"cn abg abh abi abj b\">@Autowired<\/code> is an annotation used in Spring Framework to inject dependencies into a Spring-managed bean. It is used to automatically wire the dependencies of a bean at runtime, without the need for manual configuration.<\/p>\n<p id=\"9169\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">When a bean is annotated with\u00a0<code class=\"cn abg abh abi abj b\">@Autowired<\/code>, Spring will look for other beans in the container that match the type of the dependency and automatically inject them into the bean. If multiple beans match the type, Spring will use a specified qualifier to determine which bean to inject.<\/p>\n<p id=\"6a1b\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">The\u00a0<code class=\"cn abg abh abi abj b\">@Autowired<\/code>\u00a0annotation can be used to inject dependencies into fields, setter methods, and constructor parameters. Here are some examples:<\/p>\n<ol class=\"\">\n<li id=\"a8c0\" class=\"zm zn vq mj b zo zp zq zr zs zt zu zv abk zw zx zy abl zz aba abb abm abc abd abe abf aca abo abp bp\" data-selectable-paragraph=\"\">Field injection:<\/li>\n<\/ol>\n<pre class=\"acb acc acd ace acf acg abj ach kc aci acj ack\"><span id=\"bdc9\" class=\"pw acl vq abj b bl acm acn s aco acp\" data-selectable-paragraph=\"\"><span class=\"hljs-meta\">@Component<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title.class\">MyService<\/span> {\r\n    <span class=\"hljs-meta\">@Autowired<\/span>\r\n    <span class=\"hljs-keyword\">private<\/span> MyRepository myRepository;\r\n}<\/span><\/pre>\n<p id=\"3a47\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">2. Setter injection:<\/p>\n<pre class=\"acb acc acd ace acf acg abj ach kc aci acj ack\"><span id=\"8df6\" class=\"pw acl vq abj b bl acm acn s aco acp\" data-selectable-paragraph=\"\"><span class=\"hljs-meta\">@Component<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title.class\">MyService<\/span> {\r\n    <span class=\"hljs-keyword\">private<\/span> MyRepository myRepository;\r\n\r\n<span class=\"hljs-meta\">@Autowired<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title.function\">setMyRepository<\/span><span class=\"hljs-params\">(MyRepository myRepository)<\/span> {\r\n        <span class=\"hljs-built_in\">this<\/span>.myRepository = myRepository;\r\n    }\r\n}<\/span><\/pre>\n<p id=\"dafd\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">3. Constructor injection:<\/p>\n<pre class=\"acb acc acd ace acf acg abj ach kc aci acj ack\"><span id=\"8182\" class=\"pw acl vq abj b bl acm acn s aco acp\" data-selectable-paragraph=\"\"><span class=\"hljs-meta\">@Component<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title.class\">MyService<\/span> {\r\n    <span class=\"hljs-keyword\">private<\/span> MyRepository myRepository;\r\n\r\n<span class=\"hljs-meta\">@Autowired<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title.function\">MyService<\/span><span class=\"hljs-params\">(MyRepository myRepository)<\/span> {\r\n        <span class=\"hljs-built_in\">this<\/span>.myRepository = myRepository;\r\n    }\r\n}<\/span><\/pre>\n<p id=\"eca9\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">In addition to\u00a0<code class=\"cn abg abh abi abj b\">@Autowired<\/code>, there are other annotations, such as <code class=\"cn abg abh abi abj b\">@Qualifier<\/code>,\u00a0<code class=\"cn abg abh abi abj b\">@Resource<\/code>, and\u00a0<code class=\"cn abg abh abi abj b\">@Inject<\/code>\u00a0that can also be used for dependency injection in Spring.<\/p>\n<p id=\"5b32\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">When a bean is annotated with\u00a0<code class=\"cn abg abh abi abj b\">@Autowired<\/code>, Spring will look for other beans in the container that match the type of dependency and automatically inject them into the bean. If multiple beans match the type, Spring will use a specified qualifier to determine which bean to inject.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"n p abv fb ie abw\" style=\"text-align: left;\" role=\"separator\"><\/div>\n<div class=\"gz re vm vn vo\" style=\"text-align: left;\">\n<div class=\"n p\">\n<div class=\"cz bm da db dc dd\">\n<p id=\"0a54\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">@Component\u00a0is an annotation used in Spring Framework to mark a class as a Spring-managed bean. It is a generic stereotype annotation and can be used to annotate any class, indicating that it is a Spring component.<\/p>\n<p id=\"c7df\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">When a class is annotated with @Component, Spring will automatically detect and instantiate the class as a bean. The bean will be added to the application context, making it available for use by other parts of the application.<\/p>\n<p id=\"338f\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">Here\u2019s an example of a class annotated with @Component:<\/p>\n<pre class=\"acb acc acd ace acf acg abj ach kc aci acj ack\"><span id=\"7456\" class=\"pw acl vq abj b bl acm acn s aco acp\" data-selectable-paragraph=\"\"><span class=\"hljs-meta\">@Component<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title.class\">MyService<\/span> {\r\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-built_in\">void<\/span> <span class=\"hljs-title.function\">doSomething<\/span>() {\r\n        <span class=\"hljs-comment\">\/\/ ...<\/span>\r\n    }\r\n}<\/span><\/pre>\n<p id=\"95a0\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">In the above example, MyService is a Spring component, and Spring will create an instance of the class and manage its lifecycle. Other parts of the application can be used MyService by injecting it as a dependency using\u00a0<code class=\"cn abg abh abi abj b\">@Autowired<\/code> other dependency injection annotations.<\/p>\n<p id=\"1212\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">Component is a base annotation for other Spring stereotype annotations, such as @Controller, @Service, and @Repository. These annotations provide additional semantics and can be used to indicate the specific role of a Spring-managed bean within the application. For example, a class annotated with @Controller\u00a0is typically used to handle HTTP requests in a Spring MVC application.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"n p abv fb ie abw\" style=\"text-align: left;\" role=\"separator\"><\/div>\n<div class=\"gz re vm vn vo\" style=\"text-align: left;\">\n<div class=\"n p\">\n<div class=\"cz bm da db dc dd\">\n<p id=\"19f8\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">@Configuration is an annotation used in Spring Framework to indicate that a class defines one or more Spring beans. It is used to create beans in the Spring container that other parts of the application can use.<\/p>\n<p id=\"5ae0\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">When a class is annotated with @Configuration, Spring will treat it as a source of bean definitions and create and manage the beans defined in the class. The beans are created using the @Beans annotation, which is used to mark a method that returns an object as a bean.<\/p>\n<p id=\"c85f\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">Here\u2019s an example of a class annotated with @Configuration:<\/p>\n<pre class=\"acb acc acd ace acf acg abj ach kc aci acj ack\"><span id=\"33a4\" class=\"pw acl vq abj b bl acm acn s aco acp\" data-selectable-paragraph=\"\"><span class=\"hljs-meta\">@Configuration<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title.class\">MyConfiguration<\/span> {\r\n    <span class=\"hljs-meta\">@Bean<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title.class\">MyService<\/span> <span class=\"hljs-title.function\">myService<\/span>() {\r\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title.class\">MyService<\/span>();\r\n    }\r\n}<\/span><\/pre>\n<p id=\"1d66\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">In the above example,\u00a0<code class=\"cn abg abh abi abj b\">MyConfiguration<\/code>\u00a0is a Spring configuration class that defines a bean of type\u00a0<code class=\"cn abg abh abi abj b\">MyService<\/code>. The\u00a0<code class=\"cn abg abh abi abj b\">myService()<\/code>\u00a0method is annotated with\u00a0<code class=\"cn abg abh abi abj b\">@Bean<\/code>, indicating that it returns a Spring bean. When the Spring container starts up, it will create an instance of\u00a0<code class=\"cn abg abh abi abj b\">MyService<\/code>\u00a0and add it to the application context.<\/p>\n<p id=\"b022\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\"><code class=\"cn abg abh abi abj b\">@Configuration<\/code>\u00a0is a powerful annotation that enables flexible and extensible bean configuration in Spring. It can be used to define beans with complex dependencies, externalize configuration settings, and create conditional beans based on certain conditions. It is commonly used in conjunction with other Spring annotations, such as\u00a0<code class=\"cn abg abh abi abj b\">@ComponentScan<\/code>\u00a0and\u00a0<code class=\"cn abg abh abi abj b\">@PropertySource<\/code>.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"n p abv fb ie abw\" style=\"text-align: left;\" role=\"separator\"><\/div>\n<div class=\"gz re vm vn vo\" style=\"text-align: left;\">\n<div class=\"n p\">\n<div class=\"cz bm da db dc dd\">\n<p id=\"d0b4\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\"><code class=\"cn abg abh abi abj b\">@Bean<\/code>\u00a0is an annotation used in Spring Framework to mark a method as a provider of a Spring-managed bean. It is used in conjunction with\u00a0<code class=\"cn abg abh abi abj b\">@Configuration<\/code> annotation to create beans in the Spring container that other parts of the application can use.<\/p>\n<p id=\"4412\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">When a method is annotated with\u00a0<code class=\"cn abg abh abi abj b\">@Bean<\/code>, Spring will treat it as a factory method for creating a bean. The return value of the method is the object that will be managed by Spring as a bean. The name of the method determines the name of the bean.<\/p>\n<p id=\"0199\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">Here\u2019s an example of a method annotated with\u00a0<code class=\"cn abg abh abi abj b\">@Bean<\/code>:<\/p>\n<pre class=\"acb acc acd ace acf acg abj ach kc aci acj ack\"><span id=\"c209\" class=\"pw acl vq abj b bl acm acn s aco acp\" data-selectable-paragraph=\"\"><span class=\"hljs-meta\">@Configuration<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title.class\">MyConfiguration<\/span> {\r\n    <span class=\"hljs-meta\">@Bean<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> MyService <span class=\"hljs-title.function\">myService<\/span><span class=\"hljs-params\">()<\/span> {\r\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title.class\">MyService<\/span>();\r\n    }\r\n}<\/span><\/pre>\n<p id=\"6c42\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">In the above example <code class=\"cn abg abh abi abj b\">myService()<\/code>\u00a0is a factory method that creates a bean of type\u00a0<code class=\"cn abg abh abi abj b\">MyService<\/code>. When the Spring container starts up, it will call the\u00a0<code class=\"cn abg abh abi abj b\">myService()<\/code> method and create an instance of, which will be added to the application context. Other parts of the application can be used <code class=\"cn abg abh abi abj b\">MyService<\/code>\u00a0by injecting it as a dependency using\u00a0<code class=\"cn abg abh abi abj b\">@Autowired<\/code> other dependency injection annotations.<\/p>\n<p id=\"023f\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\"><code class=\"cn abg abh abi abj b\">@Bean<\/code>\u00a0is a powerful annotation that enables flexible and extensible bean configuration in Spring. It can be used to create beans with complex dependencies, externalize configuration settings, and create conditional beans based on certain conditions. It is commonly used in conjunction with other Spring annotations, such as\u00a0<code class=\"cn abg abh abi abj b\">@Configuration<\/code>\u00a0and\u00a0<code class=\"cn abg abh abi abj b\">@Autowired<\/code>.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"n p abv fb ie abw\" style=\"text-align: left;\" role=\"separator\"><\/div>\n<div class=\"gz re vm vn vo\" style=\"text-align: left;\">\n<div class=\"n p\">\n<div class=\"cz bm da db dc dd\">\n<p id=\"2f26\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\"><code class=\"cn abg abh abi abj b\">@Controller<\/code> is an annotation used in Spring Framework to mark a class as a Spring MVC controller. It is used to handle HTTP requests in a Spring MVC web application.<\/p>\n<p id=\"dc81\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">When a class is annotated with\u00a0<code class=\"cn abg abh abi abj b\">@Controller<\/code>, Spring will treat it as a controller and map incoming requests to methods in the controller based on the URL pattern. The methods are typically annotated with\u00a0<code class=\"cn abg abh abi abj b\">@RequestMapping<\/code>\u00a0annotation to specify the URL pattern that they handle.<\/p>\n<p id=\"fd25\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">Here\u2019s an example of a class annotated with\u00a0<code class=\"cn abg abh abi abj b\">@Controller<\/code>:<\/p>\n<pre class=\"acb acc acd ace acf acg abj ach kc aci acj ack\"><span id=\"4db6\" class=\"pw acl vq abj b bl acm acn s aco acp\" data-selectable-paragraph=\"\"><span class=\"hljs-meta\">@Controller<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title.class\">MyController<\/span> {\r\n    <span class=\"hljs-meta\">@RequestMapping(<span class=\"hljs-string\">\"\/hello\"<\/span>)<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> String hello() {\r\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"hello\"<\/span>;\r\n    }\r\n}<\/span><\/pre>\n<p id=\"3093\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">In the above example <code class=\"cn abg abh abi abj b\">MyController<\/code> is a Spring MVC controller that handles requests to the URL &#8220;\/hello.&#8221; The\u00a0<code class=\"cn abg abh abi abj b\">hello()<\/code> method returns a string &#8220;hello,&#8221; used as the view name to render the response. The view is typically resolved by a view resolver configured in the Spring application context.<\/p>\n<p id=\"0a23\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\"><code class=\"cn abg abh abi abj b\">@Controller<\/code>\u00a0is a powerful annotation that enables flexible and extensible web application development in Spring. It can be used to handle different types of requests, including HTTP GET, POST, PUT, DELETE, and others. It is commonly used in conjunction with other Spring MVC annotations, such as\u00a0<code class=\"cn abg abh abi abj b\">@RequestMapping<\/code>,\u00a0<code class=\"cn abg abh abi abj b\">@RequestParam<\/code>, and\u00a0<code class=\"cn abg abh abi abj b\">@ModelAttribute<\/code>.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"n p abv fb ie abw\" style=\"text-align: left;\" role=\"separator\"><\/div>\n<div class=\"gz re vm vn vo\" style=\"text-align: left;\">\n<div class=\"n p\">\n<div class=\"cz bm da db dc dd\">\n<p id=\"a1ea\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\"><code class=\"cn abg abh abi abj b\">@Service<\/code> is an annotation used in Spring Framework to mark a class as a Spring-managed service. It indicates that a class provides some business logic or service to other parts of the application.<\/p>\n<p id=\"76ad\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">When a class is annotated with\u00a0<code class=\"cn abg abh abi abj b\">@Service<\/code>, Spring will treat it as a service and create an instance of it in the Spring container. The service can be used by other parts of the application by injecting it as a dependency using\u00a0<code class=\"cn abg abh abi abj b\">@Autowired<\/code> other dependency injection annotations.<\/p>\n<p id=\"0b75\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">Here\u2019s an example of a class annotated with\u00a0<code class=\"cn abg abh abi abj b\">@Service<\/code>:<\/p>\n<pre class=\"acb acc acd ace acf acg abj ach kc aci acj ack\"><span id=\"8f18\" class=\"pw acl vq abj b bl acm acn s aco acp\" data-selectable-paragraph=\"\"><span class=\"hljs-meta\">@Service<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title.class\">MyService<\/span> {\r\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-built_in\">void<\/span> <span class=\"hljs-title.function\">doSomething<\/span>() {\r\n        <span class=\"hljs-comment\">\/\/ some business logic here<\/span>\r\n    }\r\n}<\/span><\/pre>\n<p id=\"b963\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">In the above example,\u00a0<code class=\"cn abg abh abi abj b\">MyService<\/code> is a Spring-managed service that provides some business logic or service. When the Spring container starts up, it will create an instance of, which can be used by other parts of the application by injecting it as a dependency.<\/p>\n<p id=\"999a\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\"><code class=\"cn abg abh abi abj b\">@Service<\/code>\u00a0is a powerful annotation that enables flexible and extensible application development in Spring. It can be used to provide various types of services, such as data access, security, transaction management, and others. It is commonly used in conjunction with other Spring annotations, such as\u00a0<code class=\"cn abg abh abi abj b\">@Autowired<\/code>,\u00a0<code class=\"cn abg abh abi abj b\">@Transactional<\/code>, and\u00a0<code class=\"cn abg abh abi abj b\">@Component<\/code>.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"n p abv fb ie abw\" style=\"text-align: left;\" role=\"separator\"><\/div>\n<div class=\"gz re vm vn vo\" style=\"text-align: left;\">\n<div class=\"n p\">\n<div class=\"cz bm da db dc dd\">\n<p id=\"4ed1\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\"><code class=\"cn abg abh abi abj b\">@Repository<\/code> is an annotation used in Spring Framework to mark a class as a Spring-managed repository. It indicates that a class provides data access to a particular data source, such as a database, using Spring Data.<\/p>\n<p id=\"5fc5\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">When a class is annotated with\u00a0<code class=\"cn abg abh abi abj b\">@Repository<\/code>, Spring will treat it as a repository and create an instance in the Spring container. The repository can be used by other parts of the application by injecting it as a dependency using <code class=\"cn abg abh abi abj b\">@Autowired<\/code>\u00a0or other dependency injection annotations.<\/p>\n<p id=\"3e43\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">Here\u2019s an example of a class annotated with\u00a0<code class=\"cn abg abh abi abj b\">@Repository<\/code>:<\/p>\n<pre class=\"acb acc acd ace acf acg abj ach kc aci acj ack\"><span id=\"d671\" class=\"pw acl vq abj b bl acm acn s aco acp\" data-selectable-paragraph=\"\"><span class=\"hljs-meta\">@Repository<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title.class\">MyRepository<\/span> {\r\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title.class\">List<\/span>&lt;<span class=\"hljs-title.class\">User<\/span>&gt; <span class=\"hljs-title.function\">findAll<\/span>() {\r\n        <span class=\"hljs-comment\">\/\/ some data access logic here<\/span>\r\n    }\r\n}<\/span><\/pre>\n<p id=\"b0e8\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">In the above example,\u00a0<code class=\"cn abg abh abi abj b\">MyRepository<\/code>\u00a0is a Spring-managed repository that provides data access to a particular data source. When the Spring container starts up, it will create an instance of\u00a0<code class=\"cn abg abh abi abj b\">MyRepository<\/code>, which can be used by other parts of the application by injecting it as a dependency.<\/p>\n<p id=\"5993\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\"><code class=\"cn abg abh abi abj b\">@Repository<\/code>\u00a0is a powerful annotation that enables flexible and extensible data access in Spring. It can be used to provide data access to various data sources, such as databases, NoSQL stores, and web services. It is commonly used in conjunction with other Spring annotations, such as\u00a0<code class=\"cn abg abh abi abj b\">@Autowired<\/code>,\u00a0<code class=\"cn abg abh abi abj b\">@Transactional<\/code>, and\u00a0<code class=\"cn abg abh abi abj b\">@Component<\/code>.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"n p abv fb ie abw\" style=\"text-align: left;\" role=\"separator\"><\/div>\n<div class=\"gz re vm vn vo\" style=\"text-align: left;\">\n<div class=\"n p\">\n<div class=\"cz bm da db dc dd\">\n<p id=\"3d7e\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\"><code class=\"cn abg abh abi abj b\">@RequestMapping<\/code>\u00a0is an annotation used in Spring Framework to map a URL request to a particular method or controller in a Spring MVC web application. It is used to handle HTTP requests, such as GET, POST, PUT, DELETE, and others.<\/p>\n<p id=\"2ad4\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">When a method or controller is annotated with\u00a0<code class=\"cn abg abh abi abj b\">@RequestMapping<\/code>, Spring will map incoming requests to that method or controller based on the URL pattern specified in the annotation. The URL pattern can include variable placeholders and regular expressions for flexible request mapping.<\/p>\n<p id=\"1cf2\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">Here\u2019s an example of a method annotated with\u00a0<code class=\"cn abg abh abi abj b\">@RequestMapping<\/code>:<\/p>\n<pre class=\"acb acc acd ace acf acg abj ach kc aci acj ack\"><span id=\"293d\" class=\"pw acl vq abj b bl acm acn s aco acp\" data-selectable-paragraph=\"\"><span class=\"hljs-meta\">@Controller<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title.class\">MyController<\/span> {\r\n    <span class=\"hljs-meta\">@RequestMapping(\"\/hello\")<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> String <span class=\"hljs-title.function\">hello<\/span><span class=\"hljs-params\">()<\/span> {\r\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">\"hello\"<\/span>;\r\n    }\r\n}<\/span><\/pre>\n<p id=\"e65e\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">In the above example,\u00a0<code class=\"cn abg abh abi abj b\">MyController<\/code>\u00a0is a Spring MVC controller that handles requests to the URL &#8220;\/hello&#8221;. The\u00a0<code class=\"cn abg abh abi abj b\">hello()<\/code> method returns a string &#8220;hello&#8221;, used as the view name to render the response. The view is typically resolved by a view resolver configured in the Spring application context.<\/p>\n<p id=\"bad7\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\"><code class=\"cn abg abh abi abj b\">@RequestMapping<\/code> is a powerful annotation that enables flexible and extensible web application development in Spring. It can handle different types of requests, including HTTP GET, POST, PUT, DELETE, and others. It supports various options, such as request method, headers, content type, and others, to provide fine-grained request mapping. It is commonly used in conjunction with other Spring MVC annotations, such as\u00a0<code class=\"cn abg abh abi abj b\">@Controller<\/code>,\u00a0<code class=\"cn abg abh abi abj b\">@RequestParam<\/code>, and\u00a0<code class=\"cn abg abh abi abj b\">@ModelAttribute<\/code>.@PathVariable: Extracts a variable from the URL path and passes it as a method parameter. This can be used to provide dynamic behavior in a controller.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"n p abv fb ie abw\" style=\"text-align: left;\" role=\"separator\"><\/div>\n<div class=\"gz re vm vn vo\" style=\"text-align: left;\">\n<div class=\"n p\">\n<div class=\"cz bm da db dc dd\">\n<p id=\"f1f3\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\"><code class=\"cn abg abh abi abj b\">@Value<\/code>\u00a0is an annotation used in Spring Framework to inject values from external sources, such as properties files, environment variables, and command line arguments, into a Spring-managed bean. It is used to externalize configuration and make it easier to change values without modifying the code.<\/p>\n<p id=\"c4e4\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">When a field or method parameter is annotated with\u00a0<code class=\"cn abg abh abi abj b\">@Value<\/code>, Spring will resolve the value from the external source and inject it into the bean at runtime. The value can be a string or a SpEL expression that evaluates to a string or a primitive type.<\/p>\n<p id=\"31ec\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">Here\u2019s an example of a field annotated with\u00a0<code class=\"cn abg abh abi abj b\">@Value<\/code>:<\/p>\n<pre class=\"acb acc acd ace acf acg abj ach kc aci acj ack\"><span id=\"e986\" class=\"pw acl vq abj b bl acm acn s aco acp\" data-selectable-paragraph=\"\"><span class=\"hljs-meta\">@Service<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title.class\">MyService<\/span> {\r\n    <span class=\"hljs-meta\">@Value(\"${my.property}\")<\/span>\r\n    <span class=\"hljs-keyword\">private<\/span> String myProperty;\r\n    \r\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title.function\">doSomething<\/span><span class=\"hljs-params\">()<\/span> {\r\n        <span class=\"hljs-comment\">\/\/ use myProperty here<\/span>\r\n    }\r\n}<\/span><\/pre>\n<p id=\"bf55\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\">In the above example,\u00a0<code class=\"cn abg abh abi abj b\">MyService<\/code>\u00a0is a Spring-managed service that uses a property value named &#8220;my.property&#8221;. The property value is resolved from an external source, such as a properties file or a system property, and injected into the\u00a0<code class=\"cn abg abh abi abj b\">myProperty<\/code>\u00a0field using\u00a0<code class=\"cn abg abh abi abj b\">@Value<\/code>. The value of\u00a0<code class=\"cn abg abh abi abj b\">myProperty<\/code>\u00a0can be used in the\u00a0<code class=\"cn abg abh abi abj b\">doSomething()<\/code>\u00a0method.<\/p>\n<p id=\"7af3\" class=\"pw-post-body-paragraph zm zn vq mj b zo zp zq zr zs zt zu zv lt zw zx zy ly zz aba abb md abc abd abe abf gz bp\" data-selectable-paragraph=\"\"><code class=\"cn abg abh abi abj b\">@Value<\/code> is a powerful annotation that enables flexible and extensible configuration in Spring. It can be used to inject values from various external sources, such as property files, environment variables, and command line arguments. It supports various options, such as default values, placeholders, and SpEL expressions, to provide fine-grained configuration. It is commonly used in conjunction with other Spring annotations, such as\u00a0<code class=\"cn abg abh abi abj b\">@Autowired<\/code>,\u00a0<code class=\"cn abg abh abi abj b\">@Configuration<\/code>, and\u00a0<code class=\"cn abg abh abi abj b\">@Component.<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>&nbsp; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SpringBootApplication: The primary annotation used in Spring Boot applications. It combines three other annotations: \u00a0@Configuration, [&hellip;]<\/p>\n","protected":false},"author":1507,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":62},"categories":[446],"tags":[5223],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57367"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1507"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=57367"}],"version-history":[{"count":3,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57367\/revisions"}],"predecessor-version":[{"id":58338,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57367\/revisions\/58338"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=57367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=57367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=57367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}