@Configuration用在类上面,表明这个是个配置类。如下所示:
@Configuration public class Autoconfiguration { ... }
@EnableAutoConfiguration则是开启Spring Boot的自动配置功能。什么是自动配置功能呢?简单点说就是Spring Boot根据依赖中的jar包,自动选择实例化某些配置。
@SpringBootApplication 和@ EnableAutoConfiguration使用@SpringBootApplication来开启Spring Boot程序。 @SpringBootApplication相当于@EnableAutoConfiguration,@ComponentScan,@Configuration三者的集合。@ComponentScan
ComponentScan的功能:自动扫描并加载符合条件的组件。 类似于SSM中Sping配置文件中的scan,在启动后,会扫描对应包下的注解,如@Componetn,@Autoweite ,@Service,@Controller等。 然后创建对应的bean实例,注入到IOC容器中。@EnableFeignClients 和 @FeginClient 和 @Autowired
通常我们在给要使用Fegin的客户端进行配置@EnableFeignClients。
- 通过@EnableFeignClients启用feign客户端。对于该注解的basepackages属性,basePackage:设置自动扫描带有@FeignClient注解的基础包路径。
@SpringBootApplication @EnableFeignClients public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } }
- 使用注解@FeignClient定义feign客户端,例如将一个远程服务为http://test-service/test/echo映射为一个本地Java方法调用。对于Fegin的url属性,url用于配置指定服务的地址,相当于直接请求这个服务,不经过Ribbon的服务选择。像调试等场景可以使用。
@FeignClient(name = "test-service", path = "/test") //这里可以通过url指定微服务地址 public interface TestService { @RequestMapping(value = "/echo", method = RequestMethod.GET) TestModel echo(@RequestParam("parameter") String parameter); }
- 使用微服务,只需要通过注解方式注入在客户端映射的本地Java方法。一般通过@Autowired进行注入。例如:
@Autowired TestService testService; public void run() { // 这里的使用本地Java API的方式调用远程的Restful接口 TestModel dto = testService.echo("Hello,你好!"); log.info("echo : {}", dto); }小结
当我们使用注解@EnableFeignClients 时,相当于启用了feign客户端定义的扫描和注册机制,从而可以发现开发人员通过注解@FeignClient定义的feign客户端,并最终作为bean定义注册到容器中。而通过@Autowired自动装配注解,这些feign客户端会以ReflectiveFeign$FeignInvocationHandler动态代理的形式被注入到使用方。该feign客户端包含了对每个接口方法的处理器MethodHandler,接口缺省方法对应DefaultMethodHandler,服务功能端点方法对应SynchronousMethodHandler。
@RestController@RestController 是 @Controller 和 @ResponseBody 两个注解的结合体。@InitBinder
-
@InitBinder注解简介
@InitBinder作用于@Controller中的方法,表示为当前控制器注册一个属性编辑器,对WebData Binder进行初始化,且只对当前的Controller有效。 -
@InitBinder执行时机
@InitBinder注解被解析的时机,是其所标注的方法,在该方法被请求执行之前。同时@InitBinder标注的方法是可以多次执行的,也就是说来一次请求就执行一次@InitBinder解析。 -
@InitBinder执行原理
当某个Controller上的第一次请求,由SpringMVC前端控制器匹配到该Controller之后,根据Controller的 class 类型来查找所有标注了@InitBinder注解的方法,并且存入RequestMapping HandlerAdapter里的 initBinderCache 缓存中。等下一次请求执行对应业务方法之前,会先走initBinderCache缓存,而不用再去解析@InitBinder。
//简单示例 @InitBinder public void InitBinder(WebDataBinder binder) { //前端传入的时间格式必须是"yyyy-MM-dd"效果! DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); CustomDateEditor dateEditor = new CustomDateEditor(df, true); binder.registerCustomEditor(Date.class, dateEditor); }@PropertySource
加载指定的属性文件(*.properties)到 Spring 的 Environment 中。可以配合 @Value 和 @ConfigurationProperties 使用。
@PropertySource 和 @Value 组合使用,可以将自定义属性文件中的属性变量值注入到当前类的使用@Value注解的成员变量中。
@PropertySource 和 @ConfigurationProperties 组合使用,可以将属性文件与一个Java类绑定,将属性文件中的变量值注入到该Java类的成员变量中。
@Resource按名字,是jdk的,@Autowired按类型,是Spring的。在java代码中可以使用@Autowire或者@Resource注解方式进行装配
@Intercepts@Intercepts:标识该类是一个拦截器;
@Signature:指明自定义拦截器需要拦截哪一个类型,哪一个方法;
- type:上述四种类型中的一种;
- method:对应接口中的哪类方法(因为可能存在重载方法);
- args:对应哪一个方法的入参;
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)