栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot注解记录日记

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot注解记录日记

Spring中的注解包含以下内容:

1、@SpringCloudApplication 微服务启动类
@SpringCloudApplication时一个注解的集合,其中包含@SpringBootApplication。

注解@SpringCloudApplication包括:@SpringBootApplication、@EnableDiscoveryClient、@EnableCircuitBreaker,分别是SpringBoot注解、注册服务中心Eureka注解、断路器注解。对于SpringCloud来说,这是每一微服务必须应有的三个注解,所以才推出了@SpringCloudApplication这一注解集合。

2、@SpringBootApplication 启动类注解
@SpringBootApplication注解一般放在项目的一个启动类上,用来把启动类注入到容器中,用来定义容器扫描的范围,用来加载classpath环境中一些bean。

  • @SpringBootApplication注解默认只扫描该注解注解类所在的包,如果扫描上层目录的,需要配置注解中的scanBasePackages属性显示指定要扫描的包的范围。
@SpringBootApplication     
public class AgricultureApplication {
    public static void main(String[] args) {
        SpringApplication.run(AgricultureApplication.class, args);
    }
}

因为,从@SpringBootApplication的源码中可以看出@SpringBootApplication注解主要包装了@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan注解。

@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 {
  • @SpringBootConfiguration 主要用来把bean注入到容器中,因为该注解又封装了@Configuration注解。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {

}

而@Configuration注解又封装了@Component注解,@Component注解主要用来把一个bean注入到容器中。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {

}

因此,@SpringBootConfiguration注解就拥有了@Component注解的功能,用来把一个bean注入到容器中。

  • @EnableAutoConfiguration 这个注释告SpringBoot“猜”你将如何想配置Spring,基于你已经添加jar依赖项。如果spring-boot-starter-web已经添加Tomcat和Spring MVC,这个注释自动将假设您正在开发一个web应用程序并添加相应的spring设置。自动配置被设计用来和“Starters”一起更好的工作,但这两个概念并不直接相关。您可以自由挑选starter依赖项以外的jar包,springboot仍将尽力自动配置您的应用程序。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {}

其中最关键的要属@Import(AutoConfigurationImportSelector.class),借助AutoConfigurationImportSelector,@EnableAutoConfiguration可以帮助SpringBoot应用将所有符合条件的@Configuration配置都加载到当前SpringBoot创建并使用的IoC容器。

详细介绍SpringBoot之@EnableAutoConfiguration注解

  • @ComponentScan 注解主要用来指定扫描容器的范围。

@ComponentScan注解默认就会装配标识了@Controller,@Service,@Repository,@Component注解的类到spring容器中

3、@RestController
常见的Controller层写法

@Api(tags = {"Controller层"})
@RestController
@RequestMapping("/farmer")
public class FarmerController {}

@RestController 是@Controller 和@ResponseBody

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {}
  • @Controller是一个@Component注解。

  • @ResponseBody的作用其实是将java对象转为json格式的数据。@responseBody注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回【xml格式 或者 json等】数据。

  • 而 @RequestBody是作用在方法参数上,用于将前台发送过来固定格式的数据【xml格式 或者 json等】封装为对应的 JavaBean 对象,封装时使用到的一个对象是系统默认配置的 HttpMessageConverter进行解析,然后封装到形参上。

	@PostMapping("/add")
    public Object addCrop(@RequestBody CropVo cropVo) {
    }

4、@Component
@Component它的作用就是实现bean的注入。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
}

可以看出通过Component衍生出来 @Service(“名称”) 、@Controller (“名称”)、 @Repository(“名称”)三个注解,这三个也是通常使用比较多的,通过点击这几个注解进去都会发现,这个注解的类型是Component.Class 的别名
@Controller 使用在web层
@Service 使用在实现层
@Repository 使用在DAO层

如果在注解中带有名称,则注入的Bean是有注入ID的,也有类型,类型如下:
 
如果在注解中不带名称,则注入的Bean只有类型,类型如下:
 

举例说明:
@Service("userService")
 	
@Service
 	

5、@Configuration

@Configuration
public class BeanConfig {
    
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        // 需要定义一个convert转换消息的对象;
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        // 处理中文乱码问题
        fastJsonHttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON_UTF8));
        // 添加fastJson的配置信息;
        fastJsonHttpMessageConverter.setFastJsonConfig(new FastJsonConfig());
        return new HttpMessageConverters(fastJsonHttpMessageConverter);
    }

    
    @Bean
    public FilterRegistrationBean corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        source.registerCorsConfiguration("
    ElementType[] value();
}

  • @Retention:注解的保留位置

    RetentionPolicy.SOURCE 这种类型的Annotations只在源代码级别保留,编译时就会被忽略,在class字节码文件中不包含。
    RetentionPolicy.CLASS :这种类型的Annotations编译时被保留,默认的保留策略,在class文件中存在,但JVM将会忽略,运行时无法获得。
    RetentionPolicy.RUNTIME:这种类型的Annotations将被JVM保留,所以他们能在运行时被JVM或其他使用反射机制的代码所读取和使用。

  • @Documented:说明该注解将被包含在javadoc中

  • @Inherited:说明子类可以继承父类中的该注解

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/964721.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号