SpringBoot入门 - 开发中还有哪些常用注解
创始人
2024-05-28 23:33:03
0
本文主要介绍一些SpringBoot中常用的注解。

Spring Boot 常用注解

@SpringBootApplication

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication {/*** Exclude specific auto-configuration classes such that they will never be applied.* @return the classes to exclude*/Class[] exclude() default {};}

定义在main方法入口类处,用于启动sping boot应用项目

@EnableAutoConfiguration

让spring boot根据类路径中的jar包依赖当前项目进行自动配置

在src/main/resources的META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration若有多个自动配置,用“,”隔开

@ImportResource

加载xml配置,一般是放在启动main类上

@ImportResource("classpath*:/spring/*.xml")  单个@ImportResource({"classpath*:/spring/1.xml","classpath*:/spring/2.xml"})   多个

@Value

application.properties定义属性,直接使用@Value注入即可

public class A{@Value("${push.start:0}")    如果缺失,默认值为0private Long  id;
}

@ConfigurationProperties(prefix="person")

可以新建一个properties文件,ConfigurationProperties的属性prefix指定properties的配置的前缀,通过location指定properties文件的位置

@ConfigurationProperties(prefix="person")
public class PersonProperties {private String name ;private int age;
}

@EnableConfigurationProperties

用 @EnableConfigurationProperties注解使 @ConfigurationProperties生效,并从IOC容器中获取bean。

https://blog.csdn.net/u010502101/article/details/78758330

@RestController

组合@Controller和@ResponseBody,当你开发一个和页面交互数据的控制时,比如bbs-web的api接口需要此注解

@RequestMapping("/api2/copper")

用来映射web请求(访问路径和参数)、处理类和方法,可以注解在类或方法上。注解在方法上的路径会继承注解在类上的路径。

produces属性: 定制返回的response的媒体类型和字符集,或需返回值是json对象

@RequestMapping(value="/api2/copper",produces="application/json;charset=UTF-8",method = RequestMethod.POST)

@RequestParam

获取request请求的参数值

 public List getOpList(HttpServletRequest request,@RequestParam(value = "pageIndex", required = false) Integer pageIndex,@RequestParam(value = "pageSize", required = false) Integer pageSize) {}

@ResponseBody

支持将返回值放在response体内,而不是返回一个页面。比如Ajax接口,可以用此注解返回数据而不是页面。此注解可以放置在返回值前或方法前。

另一个玩法,可以不用@ResponseBody。
继承FastJsonHttpMessageConverter类并对writeInternal方法扩展,在spring响应结果时,再次拦截、加工结果
// stringResult: json返回结果
//HttpOutputMessage outputMessagebyte[] payload = stringResult.getBytes();outputMessage.getHeaders().setContentType(META_TYPE);outputMessage.getHeaders().setContentLength(payload.length);outputMessage.getBody().write(payload);outputMessage.getBody().flush();

@Bean

@Bean(name="bean的名字",initMethod="初始化时调用方法名字",destroyMethod="close")

定义在方法上,在容器内初始化一个bean实例类。

@Bean(destroyMethod="close")
@ConditionalOnMissingBean
public PersonService registryService() {return new PersonService();}

@Service

用于标注业务层组件

@Controller

用于标注控制层组件(如struts中的action)

@Repository

用于标注数据访问组件,即DAO组件

@Component

泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

@PostConstruct

spring容器初始化时,要执行该方法

@PostConstruct  
public void init() {   
}   

@PathVariable

用来获得请求url中的动态参数

@Controller  
public class TestController {  @RequestMapping(value="/user/{userId}/roles/{roleId}",method = RequestMethod.GET)  public String getLogin(@PathVariable("userId") String userId,  @PathVariable("roleId") String roleId){System.out.println("User Id : " + userId);  System.out.println("Role Id : " + roleId);  return "hello";  }  
}

@ComponentScan

注解会告知Spring扫描指定的包来初始化Spring

@ComponentScan(basePackages = "com.bbs.xx")

@EnableZuulProxy

路由网关的主要目的是为了让所有的微服务对外只有一个接口,我们只需访问一个网关地址,即可由网关将所有的请求代理到不同的服务中。Spring Cloud是通过Zuul来实现的,支持自动路由映射到在Eureka Server上注册的服务。Spring Cloud提供了注解@EnableZuulProxy来启用路由代理。

@Autowired

在默认情况下使用 @Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个。当找不到一个匹配的 Bean 时,Spring 容器将抛出 BeanCreationException 异常,并指出必须至少拥有一个匹配的 Bean。

当不能确定 Spring 容器中一定拥有某个类的 Bean 时,可以在需要自动注入该类 Bean 的地方可以使用 @Autowired(required = false),这等于告诉 Spring: 在找不到匹配 Bean 时也不报错

@Autowired注解注入map、list与@Qualifier

@Configuration

@Configuration("name")//表示这是一个配置信息类,可以给这个配置类也起一个名称
@ComponentScan("spring4")//类似于xml中的
public class Config {@Autowired//自动注入,如果容器中有多个符合的bean时,需要进一步明确@Qualifier("compent")//进一步指明注入bean名称为compent的beanprivate Compent compent;@Bean//类似于xml中的public Compent newbean(){return new Compent();}   
}

@Import(Config1.class)

导入Config1配置类里实例化的bean

@Configuration
public class CDConfig {@Bean   // 将SgtPeppers注册为 SpringContext中的beanpublic CompactDisc compactDisc() {return new CompactDisc();  // CompactDisc类型的}
}@Configuration
@Import(CDConfig.class)  //导入CDConfig的配置
public class CDPlayerConfig {@Bean(name = "cDPlayer")public CDPlayer cdPlayer(CompactDisc compactDisc) {  // 这里会注入CompactDisc类型的bean// 这里注入的这个bean是CDConfig.class中的CompactDisc类型的那个beanreturn new CDPlayer(compactDisc);}
}

@Order

@Order(1),值越小优先级超高,越先运行

@ConditionalOnExpression

@Configuration
@ConditionalOnExpression("${enabled:false}")
public class BigpipeConfiguration {@Beanpublic OrderMessageMonitor orderMessageMonitor(ConfigContext configContext) {return new OrderMessageMonitor(configContext);}
}

开关为true的时候才实例化bean

@ConditionalOnProperty

这个注解能够控制某个 @Configuration 是否生效。具体操作是通过其两个属性name以及havingValue来实现的,其中name用来从application.properties中读取某个属性值,如果该值为空,则返回false;如果值不为空,则将该值与havingValue指定的值进行比较,如果一样则返回true;否则返回false。如果返回值为false,则该configuration不生效;为true则生效。

https://blog.csdn.net/dalangzhonghangxing/article/details/78420057

@ConditionalOnClass

该注解的参数对应的类必须存在,否则不解析该注解修饰的配置类

@Configuration
@ConditionalOnClass({Gson.class})
public class GsonAutoConfiguration {public GsonAutoConfiguration() {}@Bean@ConditionalOnMissingBeanpublic Gson gson() {return new Gson();}
}

@ConditionalOnMisssingClass({ApplicationManager.class})

如果存在它修饰的类的bean,则不需要再创建这个bean;

@ConditionOnMissingBean(name = "example")

表示如果name为“example”的bean存在,该注解修饰的代码块不执行。

相关内容

热门资讯

户外活动的英语作文 户外活动的英语作文(通用24篇)  各位同学们,在学习的过程中要懂得劳逸结合,户外活动是个不错的选择...
英语作文:我的弟弟My Br... 英语作文:我的弟弟My Brother 篇一My Brother's Kindness and Cr...
粗心惹的祸作文350字(精选... 粗心惹的祸作文350字 篇一粗心惹的祸一天,小明放学回家的路上,心不在焉地走着。他一边走一边玩手机,...
我的家教作文 我的家教作文  我有个家教老师,她教英语,我叫她鲍老师。我认识她是从一个星期六,那天,我学钢琴回来,...
英语作文秋天范文200字(实... 英语作文秋天范文200字 篇一:The Beauty of AutumnAutumn is my f...
我的家教(精简3篇) 我的家教 篇一我从小就有一位非常特别的家教,她就是我的外婆。外婆是一个非常智慧和善良的人,她对我的教...
五年级英语作文my holi... 五年级英语作文my holiday 篇一My Holiday in the CountrysideD...
少年宫英语日记带翻译【优质3... 少年宫英语日记带翻译 篇一A Day at the Youth Center在少年宫的一天Dear ...
online shoppin... online shopping英语作文60词 篇一Title: The Convenience of...
我的爱好英语作文(通用6篇) 我的爱好英语作文 篇一我的爱好英语英语是我最喜欢的学科,也是我最热爱的爱好。每天,我都会在学校的英语...
列举多个句子英语【精简3篇】 篇一:列举多个句子英语In today's globalized world, being able...
期末考试的英文作文【推荐3篇... 期末考试的英文作文 篇一The Importance of Preparing for Final ...
人物介绍英语范文带翻译5篇【... 人物介绍英语范文带翻译5篇 篇一Title: Introduction to a Famous Sc...
伤感爱情英语说说句子(优选6... 伤感爱情英语说说句子 篇一Heartbroken Love QuotesWhen love turn...
高中英语名词语法【优选5篇】 高中英语名词语法 篇一在高中英语中,名词是一个非常重要的语法要素。名词可以用来表示人、动物、事物或抽...
中考英语作文范文【通用6篇】 中考英语作文范文 篇一标题:My Favorite HobbyMy favorite hobby i...
习惯的英语作文(推荐3篇) 习惯的英语作文 篇一:My Daily RoutineEvery person has their ...
成人高考英语作文(最新6篇) 成人高考英语作文 篇一The Importance of Lifelong LearningIn t...
高级英语作文【精选6篇】 高级英语作文 篇一:The Importance of Critical Thinking in E...
最新关于介绍美丽的深圳的英语... 最新关于介绍美丽的深圳的英语作文 篇一Title: Exploring the Breathtaki...