springboot注解相关

@SpringBootApplication注解整合了三个注解:

  • @ComponentScan
  • @EnableAutoConfiguration
  • @Configuration
    其中Component的扫描包括如下注解:
    在这里插入图片描述

有一些注解是用来指定bean的属性,需要在@Configuration类上配合@Enable*注解来生成bean:

  • @ConfigurationProperties
  • @FeignClient

有一些需要通过@Configuration加入容器:

  • @ControllerEndpoint(id = “test”)
    可以认为@ControllerEndpoint相当于@RequestMapping,需要通过@Configuration定义bean并且经过Spring Factories进制才能正确地引入被容器加载为controller。(注意还需要将EndPoint的id加入到WebEndpointPropertie这个bean的暴露集合,如下:)
@Component
public class EndpointInitPostProcessor implements BeanPostProcessor {

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		if (bean instanceof WebEndpointProperties) {
			WebEndpointProperties endpointProperties = (WebEndpointProperties) bean;
			if (endpointProperties.getExposure().getInclude().size() == 0) {
				endpointProperties.getExposure().getInclude().add("health");
				endpointProperties.getExposure().getInclude().add("info");
			}
			endpointProperties.getExposure().getInclude().add("test");
		}
		return bean;
	}
}

如上,id为test的Endpoint在新的项目中可见。

有一些注解不属于@ComponentScan的扫描范围,需要其它扫描:

  • @WebFilter
    仅加这个注解无效,需要通过@ServletComponentScan注解扫描到才可以,生成过滤器。
发布了28 篇原创文章 · 获赞 11 · 访问量 1524

猜你喜欢

转载自blog.csdn.net/weixin_42881755/article/details/102488297
今日推荐