Spring boot注解

@SpringBootApplication包含@ComponentScan、@Configuration和@EnableAutoConfiguration。@EnableAutoConfiguration实现自动配置,可根据依赖的jar包自动配置Spring应用。@ComponentScan扫描并注册带有特定注解的类为Bean。@Configuration类作为配置主类,可通过@ImportResource加载xml配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@SpringBootApplication:

包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解。其中@ComponentScan让spring Boot扫描到Configuration类并把它加入到程序上下文。


@Configuration

 相当于传统的xml配置文件,如果有些第三方库需要用到xml文件,建议仍然通过@Configuration类作为项目的配置主类——可以使用@ImportResource注解加载xml配置文件


@EnableAutoConfiguration 

Spring Boot自动配置(auto-configuration):尝试根据你添加的jar依赖自动配置你的Spring应用。例如,如果你的classpath下存在HSQLDB,并且你没有手动配置任何数据库连接beans,那么我们将自动配置一个内存型(in-memory)数据库”。你可以将@EnableAutoConfiguration或者@SpringBootApplication注解添加到一个@Configuration类上来选择自动配置。如果发现应用了你不想要的特定自动配置类,你可以使用@EnableAutoConfiguration注解的排除属性来禁用它们。


@ComponentScan 

表示将该类自动发现扫描组件。个人理解相当于,如果扫描到有@Component、@Controller、@Service等这些注解的类,并注册为Bean,可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。如果没有配置的话,Spring Boot会扫描启动类所在包下以及子包下的使用了@Service,@Repository等注解的类


@Component 

可配合CommandLineRunner使用,在程序启动后执行一些基础任务。

@RestController

注解是@Controller和@ResponseBody的合集,表示这是个控制器bean,并且是将函数的返回值直 接填入HTTP响应体中,是REST风格的控制器。

@Autowired 自动导入。

@PathVariable 获取参数。

@JsonBackReference 解决嵌套外链问题。

@RepositoryRestResourcepublic 配合spring-boot-starter-data-rest使用。


@Import:用来导入其他配置类。

@ImportResource:用来加载xml配置文件。

@Autowired:自动导入依赖的bean

@Service:一般用于修饰service层的组件

@Repository:使用@Repository注解可以确保DAO或者repositories提供异常转译,这个注解修饰的DAO或者repositories类会被ComponetScan发现并配置,同时也不需要为它们提供XML配置项。

@Bean:用@Bean标注方法等价于XML中配置的bean。

@Value:注入Spring boot application.properties配置的属性的值。

### Spring Boot 注解使用指南 在 Spring Boot 中,注解是实现自动配置和组件管理的核心机制。通过合理的注解使用,可以简化配置、提高代码可读性,并提升开发效率。以下是一些关键注解及其用途。 #### 核心组件注解 Spring Boot 提供了一系列注解,用于标记类为 Spring 容器中的组件,并赋予其特定的语义角色。这些注解包括: - `@Component`:通用的组件注解,用于标记一个类为 Spring 的组件,使其能够被自动扫描并注册为 Spring 容器中的 Bean。 - `@Service`:用于标记业务逻辑层的服务类,通常用于服务层组件。 - `@Repository`:用于标记数据访问层的 DAO 类,通常用于与数据库交互的组件。 - `@Controller`:用于标记 Web 层的控制器类,通常用于返回视图名称。 - `@RestController`:结合了 `@Controller` 和 `@ResponseBody`,用于直接返回 JSON 数据的 REST 控制器[^3]。 ```java import org.springframework.stereotype.Component; @Component public class MyComponent { // 业务逻辑 } ``` ```java import org.springframework.stereotype.Service; @Service public class MyService { // 业务逻辑 } ``` ```java import org.springframework.stereotype.Repository; @Repository public class MyRepository { // 数据访问逻辑 } ``` ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/hello") public String sayHello() { return "Hello, Spring Boot!"; } } ``` #### 自动配置相关注解 Spring Boot 的自动配置机制依赖于条件注解来决定是否应用某个配置。常见的条件注解包括: - `@ConditionalOnClass`:只有在类路径上存在指定类时,才启用该配置。 - `@ConditionalOnProperty`:只有在指定的属性存在并具有特定值时,才启用该配置[^2]。 ```java import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Configuration; @Configuration @ConditionalOnClass(name = "com.example.SomeClass") public class MyAutoConfiguration { // 自动配置逻辑 } ``` ```java import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Configuration; @Configuration @ConditionalOnProperty(name = "my.feature.enabled", havingValue = "true") public class MyFeatureConfiguration { // 特性启用时的配置逻辑 } ``` #### 应用启动注解 `@SpringBootApplication` 是 Spring Boot 应用的主注解,它结合了 `@EnableAutoConfiguration` 和 `@ComponentScan`。如果不想使用 `@SpringBootApplication`,可以分别使用这两个注解来定义相同的行为[^1]。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @EnableAutoConfiguration @ComponentScan public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ``` #### 调试自动配置 Spring Boot 的自动配置定义在 `spring-boot-autoconfigure` 包下的 `META-INF/spring.factories` 文件中。可以通过查看该文件了解 Spring Boot 是如何加载自动配置类的[^2]。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值