spring注解详情---下篇

我要一步一步往上爬,等待阳光静静看着它的脸

@RestController

@ResponseBody + @Controller的注解组合

@GetMapping

从命名约定我们可以看到每个注释都是为了处理各自的传入请求方法类型,即@GetMapping用于处理请求方法的GET类型,@ PostMapping用于处理请求方法的POST类型等。
如果我们想使用传统的@RequestMapping注释实现URL处理程序,那么它应该是这样的:
@RequestMapping(value = “/get/{id}”, method = RequestMethod.GET)
新方法可以简化为:
@GetMapping("/get/{id}")

@RestController
@RequestMapping("/consumer")
public class UserController {
    @Autowired
    private RestTemplate restTemplate;
    @GetMapping("/{id}")
    public User queryById(@PathVariable(value = "id")Integer id){
        String uri = "http://localhost:18081/user/find/"+id;
        System.out.println("金坛");
        return restTemplate.getForObject(uri, User.class);
    }

@SpringBootApplication

//启动类
@SpringBootApplication
public class SpringDemo01Application {
    public static void main(String[] args) {
        SpringApplication.run(SpringDemo01Application.class, args);
    }
}

@SpringBootConfiguration

Spring Boot中的 @SpringBootConfiguration注释是一个类级别的注释,它指示此类提供了应用程序配置。通常,具有main()方法的类最适合此注释。 @SpringBootApplication批注包括@SpringBootConfiguration批注。
当我们使用@SpringBootConfiguration标记一个类时,这意味着该类提供了@Bean定义方法。 Spring容器处理配置类以为我们的应用实例化和配置bean。

@SpringBootConfiguration
public class DemoApp {
 
    public static void main(String[] args) {
        SpringApplication.run(DemoApp.class, args);
    }
    @Bean
    public Course course() {
        return new Course();
    }
    @Bean
    public Student student() {
        return new Student();
    }
}

@SpringCloudApplication

如果启动类包含了:@SpringBootApplication,@EnableDiscoveryClient,@EnableCircuitBreaker(断路器使用)这三个,可以直接使用@SpringCloudApplication注解。

@Configuration

表示将该类作用springboot配置文件类;

@ComponentScan

表示程序启动时,自动扫描当前包及子包下所有类;

@Scheduled("")

@Scheduled注解是spring boot提供的用于定时任务控制的注解,主要用于控制任务在某个指定时间执行,或者每隔一段时间执行.注意需要配合@EnableScheduling使用,配置@Scheduled主要有三种配置执行时间的方式,cron,fixedRate,fixedDelay.

/**
 * Created with IntelliJ IDEA.
 * User: xia wei xuan.
 * Date: 2021/08/22.
 * Time: 下午 10:25.
 * Explain:计划任务执行类
 */
@Service
public class ScheduledTaskService {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
 
    @Scheduled(fixedRate = 5000) //通过@Scheduled声明该方法是计划任务,使用fixedRate属性每隔固定时间执行
    public void reportCurrentTime(){
        System.out.println("每隔5秒执行一次 "+dateFormat.format(new Date()));
    }
    @Scheduled(cron = "0 07 20 ? * *" ) //使用cron属性可按照指定时间执行,本例指的是每天20点07分执行;
    //cron是UNIX和类UNIX(Linux)系统下的定时任务
    public void fixTimeExecution(){
        System.out.println("在指定时间 "+dateFormat.format(new Date())+" 执行");
    }
}

@EnableScheduling

/**
 * Created with IntelliJ IDEA.
 * User: xia wei xuan.
 * Date: 2021/08/22.
 * Time: 下午 10:32.
 * Explain:配置类
 */
@Configuration
@ComponentScan("cn.hncu.p3.p3_taskscheduler")
@EnableScheduling //通过@EnableScheduling注解开启对计划任务的支持
public class TaskScheduleConfig {
}

@EnableAutoConfiguration

表示程序启动时,自动加载springboot默认的配置;

@EnableDiscoveryClient

@EnableDiscoveryClient开启eureka客户端发现功能,非eureka注册中心。

@EnableEurekaClient

@EnableEurekaClient的注册中心只能是Eureka。

@SpringBootApplication
@EnableEurekaClient
@MapperScan(basePackages = {"com.changgou.goods.dao"})
public class GoodsApplication {

    @Value("${workerId}")
    private Integer workerId;

    @Value("${datacenterId}")
    private Integer datacenterId;

    public static void main(String[] args) {
        SpringApplication.run( GoodsApplication.class);
    }

    @Bean
    public IdWorker idWorker(){
        return new IdWorker(workerId,datacenterId);
    }
}

@EnableEurekaServer

eureka注册中心的注解

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class);
    }

}

@feignclient

属于Spring Cloud技术架构体系中的一个注解,其作用是可以让当前服务调用其它应用服务的接口,相比于RestTemplate使用起来更加灵活;开启openfeign时在启动类中使用。

@DefaultProperties

服务熔断和降级的默认兜底方法

@Transactional

事务注解

	@Transactional
    @Override
    public void add(Goods goods) {
        Spu spu = goods.getSpu();
        long spuId = idWorker.nextId();
        spu.setId(String.valueOf(spuId));
        spu.setIsDelete("0");
        spu.setStatus("0");
        spuMapper.insertSelective(spu);
        //保存sku 集合数据到数据库
        saveSkuList(goods);
    }

@GlobalTransactional

@Transactional在我们的开发中因为分布式原因,这个事务的接口已经不能够满足我们使用了,所以我们需要使用@GlobalTransactional注解来实现一个分布式事务。

@LoadBalanced

作用 在使用 RestTemplate 的时候 如果 RestTemplate 上面有 这个注解,那么 这个 RestTemplate 调用的 远程地址,会走负载均衡器.

@Bean
@LoadBalanced
RestTemplate restTemplate() {
    return new RestTemplate();
}

你一定能够成为你想成为的人

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卢卢在路上

人生苦短,及时行乐

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值