一些注释笔记

本文详细介绍了SpringBoot中常用的注解,包括@EnableFeignClients、@Slf4j、@Component、@Scheduled、@PostConstruct、@PreDestroy、@SpringBootApplication等,涵盖了微服务调用、日志记录、组件注册、定时任务、生命周期管理、启动配置等功能。

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

1. @FeignClient

在入口程序通过@EnableFeignClients开启
https://www.cnblogs.com/moonandstar08/p/7565442.html

2. @Slf4j

可直接使用log对象输出日志,代替
备注:如果找不到log对象,可能是idea没有安装lombok plugins引起的。>

 private  final Logger logger = LoggerFactory.getLogger(LoggerTest.class);
3. @component

把普通pojo实例化到spring容器中,相当于配置文件中的

<bean id="" class=""/>

泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类。

4. @Scheduled

定时任务,使用Cron表达式。
在入口程序添加注解
@EnableScheduling告诉springboot我们需要用定时任务完成业务逻辑。
@Scheduled拥有四个属性:cron、fixedRate、fixedDelay、initialDelay。
参考:https://www.jianshu.com/p/c7492aeb35a1

5. @PostConstruct、@PreDestroy

1.@PostConstruct说明

被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。

2.@PreDestroy说明

被@PreDestroy修饰的方法会在服务器卸载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的destroy()方法。被@PreDestroy修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载之前。

参考: https://www.cnblogs.com/landiljy/p/5764515.html
https://www.jianshu.com/p/98cf7d8b9ec3

6. @SpringBootApplication

@SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan
参考:https://www.cnblogs.com/MaxElephant/p/8108140.html

7. @NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor

基于lambok,分别表示:无参构造器、部分参数构造器、全参构造器。
Lombok没法实现多种参数构造器的重载。

8. @CrossOrigin

@CrossOrigin允许跨域使用在@RequestMapping注解中指定的源和HTTP方法

9. @Controller 与 @RestController

使用@Controller注解,视图解析器可以解析return的html和jsp页面,并跳转
使用@Controller+@Response,解析器可以返回json内容到页面
@RestController相当于@Controller+@Response,但视图解析器无法解析html和jsp页面。

10. @Reference 与 @Service

springboot提供:
dubbo中的@Reference 用在消费端,表明使用的是服务端的什么服务(等价于@autowired + consumer.xml)
dubbo中的@Service 用在服务提供者中,在类或者接口中声明。服务提供者实现相关的服务接口,当消费端调用相关的类时,最终会调用提供者的实现方法。
需要通过@EnableDubbo注释启动。
参考:https://www.cnblogs.com/lgjlife/p/10225271.html
dubbo在controller中reference注解为空的问题深度解析
参考:https://blog.youkuaiyun.com/zhou_java_hui/article/details/53039491

11. @EnableDubbo 与 @EnableDubboConfig

@EnableDubbo = @EnableDubboConfig + @DubboComponentScan

//eg.
@EnableDubbo (scanBasePackages = "com.alibaba.dubbo.samples.impl")
//等价于
@EnableDubboConfig
@DubboComponentScan("com.alibaba.dubbo.samples.impl")

参考:http://dubbo.apache.org/zh-cn/docs/user/demos/multi-versions.html

12. @ConfigurationPropertis

将类与.yml文件绑定
@ConfigurationPropertis要生效,一定要是spring组件,所以要加@Component注解
当通过@Autowired自动注入使用时,就会携带yml配置文件中的值。
在这里插入图片描述

13. @Value

单一属性的配置
与@ConfigurationPropertis区别

@ConfigurationPropertis@Value说明
功能批量注入属性注入一条属性
松散绑定支持不支持
SpEL不支持支持Spring表达式
JSR303数据校验支持不支持如@NotNull, @Email
复杂类型支持不支持如object.list

在这里插入图片描述

14. @PropertySource

导入指定的.properties文件

@PropertySource(value = {"classpath:person.properties"}) //例子
15. @ImportResource

导入指定的.xml文件

@ImportResource(locations= {"classpath:beans.xml"}) //例子
16. @Bean

将方法返回值加入Spring容器中,用于配置类。

@Configuration
public class DemoConfig{
	// 将方法返回值加入容器中,容器中组件的默认id就是方法名。
	@Bean
	public MyService myService(){
		return new MyService();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值