在SpringBoot框架中,注解作为一种隐式配置,极大的简化了以前xml文件的配置方式。SpringBoot中包含许多种类的注解,这里对在SpingBoot项目中常用到的一些注解的进行大体的概括总结;html
一、@SpringBootApplication前端
在SpirngBoot启动类里面,都加入了此启动注解,此注解是个组合注解,包括了@SpringBootConfiguration 、@EnableAutoConfiguration和@ComponentScan注解。web
,其实两种功能一致,都是标注该类为配置类
spring
容器中。注意事项:spring
exclude
进行排除@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@Configuration @ComponentScan(basePackages = {"com.test.service"}) //引入第三方jar包下的类
二、@Import数据库
@Import注解可用来导入一个或者多个 Spring 配置文件,特别是第三方jar包下的一些配置类,须要经过Import注解进行加载,代码以下所示json
@Import({KafkaConfig.class, JedisConfig.class}) //引入第三方jar包里配置类
以通常的的配置类中用到的注解为例bootstrap
一、@Configuration后端
@Configuration专门用来标注配置类,它通常会配合api
二、@Beanspringboot
使用@Bean注解拿到配置返回相关实例,并放入sping容器中统一管理
三、@PropertySource
目的是加载指定路径下的属性文件
五、@Value
配合@PropertySource注解使用,指定该字段对应的配置文件中的内容
四、@Order
利用@Order控制配置类的加载顺序
结合以上注解对kafka进行配置示例代码以下
@Configuration @PropertySource("classpath:spring-kafka.properties") @Order(2) public class KafkaConfig { @Value("${spring.kafka.bootstrap-servers}") private String bootstrapServers; @Bean public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<Object, Object>> kafkaListenerContainerFactory() { ConcurrentKafkaListenerContainerFactory<Object, Object> factory = new ConcurrentKafkaListenerContainerFactory<Object, Object>(); factory.setConsumerFactory(consumerFactory()); factory.setConcurrency(1); // factory .getContainerProperties().setPollTimeout(1000); return factory; } }
一、@Service
用于标注服务层,主要用来进行业务的逻辑处理
二、@Repository
用于标注持久层,主要用来进行数据库相关操做
三、@Component
一个通用的注解,能够注解各类组件,就是说当咱们须要注入sping容器中bean类没有明确分类时(不属于@service、@Repository等的时候),咱们就可使用@Component来标注这个类。
四、@Scope
spring容器管理bean默认是单例模式,若是你须要使用多例模式能够经过@Scope("prototype")注解来实现。
五、@Autowired
这个就很简单了,用于Spring容器中Bean类实例的注入
六、@PostConstruct
在Bean初始化以后(构造方法和@Autowired以后)执行指定操做。若是在项目中有些操做须要在Bean类构造后执行,可使用@PostConstruct注解,实例代码以下
@Component public class demo { @PostConstruct public void start() { //在构造方法和@Autowired注入实例后执行 } }
一、@Controller 和 @RestController
控制器的注解,处理http请求的入口,这两个注解的主要区别在于@Controller中若是须要返回json数据须要使用@ResponseBody注解来配合,直接用@RestController则直接返回json数据
二、@RequestMapping
RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的全部响应请求的方法都是以该地址做为父路径。
RequestMapping注解有六大属性
value, method;
value: 指定请求的实际地址,指定的地址能够是URI Template 模式(后面将会说明);
method: 指定请求的method类型, GET、POST、PUT、DELETE等;
consumes,produces;
consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
params,headers;
params: 指定request中必须包含某些参数值是,才让该方法处理。
headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求;
RequestMapping的简化注解有:
@GetMapping 等同于 @RequestMapping(method = RequestMethod.GET) @PostMapping 等同于 @RequestMapping(method = RequestMethod.POST) @PutMapping 等同于 @RequestMapping(method = RequestMethod.PUT) @DeleteMapping 等同于 @RequestMapping(method = RequestMethod.DELETE) @PatchMapping 等同于 @RequestMapping(method = RequestMethod.PATCH)
三、@RequestBody
RequestBody注解容许request的参数在reqeust体中,也就是后端以实体的方式接收前端发送的数据
四、@RequestParam
RequestParam注解则是以不一样参数之间用&分隔的方式接收前端发送的数据
结合以上注解的常规示例代码以下
@RestController @RequestMapping("/api") public class ApiController extends BaseController { @PostMapping("/login") public User login(@RequestBody User user){ //代码 } @GetMapping("/getUser") public User getUser(@RequestParam String userName, @RequestParam String userPhone){ //代码 } }
以上就是在SpirngBoot项目中常用到的一些注解进行的总结,固然还有不少其余注解在这里就不一一赘述了。