SpringBoot注解

@Autowird

@Autowired是一个注解,它可以对类成员变量、方法及构造函数进行标注,让Spring完成bean自动装配的工作
@Autowired默认是按照类去匹配的,配合@Qualifier指定按照名称去装配bean

@Qualifier

@Qualifier是一个注解,通过这个标识可以让我们调用实现类。
@Qualifier的参数名必须为我们定义的@service注解的名称之一

例子:

@Service("a")
public class EmployeeServiceImpl implements EmployeeService {
    public EmployeeDto getEmployeeById(Long id) {
        return new EmployeeDto();
    }
}

@Service("b")
public class EmployeeServiceImpl1 implements EmployeeService {
    public EmployeeDto getEmployeeById(Long id) {
        return new EmployeeDto();
    }
}


@Controller
@RequestMapping("/emplayee.do")
public class EmployeeInfoControl {

    @Autowired
    @Qualifier("b")
    EmployeeService employeeService;

    @RequestMapping(params = "method=showEmplayeeInfo")
    public void showEmplayeeInfo(HttpServletRequest request, HttpServletResponse response, EmployeeDto dto) {
        #略
    }
}

@Schedule

@Schedule是一个注解,是一个做定时任务的标签,@Schedule要配合@EnableScheduling使用

例子:

@EnableScheduling
@Service
public class main{
    //@Scheduled(cron = "*/2 * * * * ?") 2秒执行一次
    @Scheduled(cron = "*/2 * * * * ?")
    public void test(){
        System.out.println(new Date());
    }
}

@Slf4j

@Slf4j是一个注解

用作日志输出

需要使用Lombok插件(Idea直接从插件库安装)

一般代替

private final Logger logger = LoggerFactory.getLogger(当前类名.class);
引用的包
import lombok.extern.slf4j.Slf4j;

打印日志一般采用

log.info("日志内容");

例子

package com.space.slf4j;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class TestController {

    @RequestMapping("/log")
    public String testLog(){
        log.info("#########  info  #########");
        log.debug("#########  debug  #########");
        log.error("#########  error  #########");
        return null;
    }

@Component

@Component是一个注解

把普通的pojo(Plain Ordinary Java Object)即简单的Java对象纳入Spring容器的管理中

一般代替xml中

<context:component-scan base-package=”com.mmnc”> 

一般放置在class文件的上方

@Component
public class UserInfo {
}

@Configuration

@Configuration是一个注解,用于定义配置类,可替换xml配置文件

被注解的类内部包含一个或多个@Bean注解的方法,它们将会AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,用于构建bean定义,初始化Spring容器

@Configuration
public class AppConfig {

    @Bean
    public MyBean myBean() {
        // instantiate, configure and return bean ...
    }
}

@Configuration与xml对比

例子:

Java

package com.dxz.demo.configuration;

import org.springframework.context.annotation.Configuration;

@Configuration
public class TestConfiguration {
    public TestConfiguration() {
        System.out.println("TestConfiguration容器启动初始化。。。");
    }
}

XML

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd" default-lazy-init="false">


</beans>
posted @ 2020-12-09 21:41  墨柯  阅读(61)  评论(0编辑  收藏  举报