Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Programmers need to know which annotations in SpringBoot

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about which annotations programmers need to master in SpringBoot. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Preface

Spring has gone through the following stages:

Phase I: xml configuration

In the Spring 1.x era, using Spring to develop Bean full of xml configurations, as the project expanded, we needed to put xml configuration files in different configuration files, and then we needed to frequently switch between developed classes and configuration files.

Phase 2: annotation configuration

In the Spring 2.x era, with the annotation support brought by JDK1.5, Spring provided annotations that declared Bean (such as @ Component, @ Service), greatly reducing the amount of configuration. The main way to use is to use xml for the basic configuration of the application (such as database configuration) and annotations for business configuration.

Phase 3: java configuration

Spring 3.0 introduces Java-based configuration capabilities, which is a type-safe reconfigurable configuration that can replace XML. We happen to be in this era, and both Spring4.x and Spring Boot recommend Java configuration.

All of these xml configurations represent wear and tear during development. Because there is a mental switch between thinking about Spring feature configuration and solving business problems, writing configuration takes up time to write application logic. Spring Boot made it all a thing of the past. Spring Boot simplifies Spring-based application development, requiring only "run" to create an independent, production-level Spring application. Spring Boot provides out-of-the-box settings for the Spring platform and third-party libraries (default settings are provided) so we can get started easily. Most Spring Boot applications require very little Spring configuration. We can use SpringBoot to create a java application and launch it using java-jar, or we can deploy it in the traditional way of war. This is one of the main reasons why SpringBoot is being used by more and more developers. Let's explain in detail several important annotations of SpringBoot: @ Configuration, @ Bean,@SpringBootApplication,@ComponentScan

@ Configuration and @ Bean annotations

Spring's Java configuration is implemented through @ Configuration and @ Bean annotations, which have been available since Spring3.0:

1. @ Configuration acts on the class, which is equivalent to a xml configuration file

2. @ Bean acts on the method, which is equivalent to the one in the xml configuration

Case

@ Configuration

Public class DemoConfigure {

@ Bean

Public User userBean () {

User user=new User ()

User.setAge (100)

User.setName ("jack")

User.setPwd ("123")

Return user

}

}

The tests are as follows:

Package com.example.springboot01

Import com.example.springboot01.pojo.User

Import org.junit.Test

Import org.junit.runner.RunWith

Import org.springframework.beans.factory.annotation.Autowired

Import org.springframework.boot.test.context.SpringBootTest

Import org.springframework.test.context.junit4.SpringRunner

@ RunWith (SpringRunner.class)

@ SpringBootTest

Public class Springboot01ApplicationTests {

@ Autowired

Private User user

@ Test

Public void contextLoads () {

System.out.println (user)

}

}

Print:

User {name='jack', pwd='123', age=100}

@ SpringBootApplication

The @ SpringBootApplication annotation source code is as follows:

Package org.springframework.boot.autoconfigure

@ Target (ElementType.TYPE)

@ Retention (RetentionPolicy.RUNTIME)

@ Documented

@ Inherited

@ SpringBootConfiguration

@ EnableAutoConfiguration

@ ComponentScan (excludeFilters = {

@ Filter (type = FilterType.CUSTOM, classes = TypeExcludeFilter.class)

Filter (type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class)})

Public @ interface SpringBootApplication {

}

@ SpringBootApplication is a composite annotation, including @ ComponentScan, and @ SpringBootConfiguration,@EnableAutoConfiguration.

-@ SpringBootConfiguration inherits from @ Configuration, and the two functions are the same, indicating that the current class is a configuration class, and will include one or more instances of methods marked with @ Bean annotations in the current class into the srping container, and the instance name is the method name.

The function of-@ EnableAutoConfiguration starts automatic configuration. @ EnableAutoConfiguration annotation means that Springboot configures the default configuration of your project according to the jar package you add. For example, if you use spring-boot-starter-web to determine whether your project needs to add webmvc and tomcat, it will automatically help you configure the default configuration required in the web project.

-@ ComponentScan, scan the classes marked by the @ Component,@Controller,@Service,@Repository annotation under the current package and its subpackages and incorporate them into the spring container for management. Is previous (the tags used in xml were used to scan packages).

@ SpringBootApplication specifies to scan some packages

The @ SpringBootApplication annotation scans the packages and subpackages where the current class resides by default. You can set the properties to scan other packages, and once set, the default values are no longer useful.

@ SpringBootApplication (scanBasePackageClasses = {TestConfig.class,TestController.class})

Set the scanBasePackageClasses property to determine which configuration classes are scanned only (@ Configuration annotated classes).

Or set as follows:

@ SpringBootApplication (scanBasePackageClasses = {Springboot01Application.class,TestConfig.class})

Specify the packages and subpackages where these classes are scanned.

Set not to automatically assemble

Automatic configuration of springboot can save us a lot of time, but sometimes if we don't want to automatically configure it in the case of introducing dependent packages, we can cancel it through relevant settings.

Inside the @ SpringBootApplication (exclude = {JpaRepositoriesAutoConfiguration.class, RedisAutoConfiguration.class}) annotation, the dependencies that do not need to be automatically configured can be specified through the exclude parameter, and multiple classes can be specified.

All the automatically configured classes are under the: org.springframework.boot.autoconfigure package. Only automatically configured classes can be discharged.

@ ComponentScan

The @ ComponentScan annotation also plays an important role in Spring annotations. It can customize the packages scanned by Spring, that is, it scans classes marked with @ Controller, @ Service, @ Component and @ Repository annotations by default, and instantiates these components to the SpringIOC container. It has a configuration attribute: basePackages, that is, the specified scanned package. If it does not know, it will scan the path (including subpackages) of the packages configured with this annotation by default. Let's see that there is a piece of code in the source code of the @ SpringBootConfiguration annotation:

@ AliasFor (

Annotation = ComponentScan.class

Attribute = "basePackages"

)

String [] scanBasePackages () default {}

The scanBasePackages attribute is specified to the basePackages attribute of the @ ComponentScan annotation. In SpringBoot, we can also specify the path of the package scan through the scanBasePackages attribute (if not specified, the package path of the main program class and the classes under the subpackage will be scanned by default):

@ SpringBootApplication (scanBasePackages = "com.seagetech.springbootdemo")

Thank you for reading! This is the end of this article on "programmers need to master the notes in SpringBoot". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report