Springboot常用注解

Spring Boot 常用注解

1、启动注解 @SpringBootApplication

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableTransactionManagement
@EnableCaching
@ServletComponentScan
@MapperScan("com.winter.dao")
@SpringBootApplication
public class AnnotationApplication {

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

}

@SpringBootApplication是spring boot最重要的一个注解,用于快捷配置启动类。

@EnableTransactionManagement声明在主配置类上,表示开启声明式事务,其原理是通过@Import导入TransactionManagementConfigurationSelector组件,然后又通过TransactionManagementConfigurationSelector导入组件AutoProxyRegistrar和ProxyTransactionManagementConfiguration;

@EnableCaching注解是spring framework中的注解驱动的缓存管理功能。

@ServletComponentScan注解,Servlet、Filter、Listener可以直接通过@WebServlet、@WebFilter、@WebListener注解自动注册,无需其他代码。

@MapperScan(“com.winter.dao”)注解,com.winter.dao包下面的接口类,在编译之后都会生成相应的实现类

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication 

前四个注解:是元注解,用来修饰当前注解,没有实际功能,如果不打算写自定义注解,不需要了解。

@SpringBootConfiguration:当前类是一个配置类

@EnableAutoConfiguration:启用 SpringBoot 的自动配置机制;帮助SpringBoot应用将所有符合条件的@Configuration配置都加载到当前SpringBoot,并创建对应配置类的Bean,并把该Bean实体交给IoC容器进行管理。

@ComponentScan:扫描被@Component (@Service,@Controller)注解的 bean,注解默认会扫描该类所在的包下所有的类。

2、Spring Bean相关

2.1、@Autowired
  • 首先了解一下IOC操作Bean管理,bean管理是指
  • (1)spring创建对象
  • (2)spring注入属性。当我们在将一个类上标注@Service或者@Controller或@Component或@Repository注解之后,spring的组件扫描就会自动发现它,并且会将其初始化为spring应用上下文中的bean。 当需要使用这个bean的时候,例如加上@Autowired注解的时候,这个bean就会被创建。而且初始化是根据无参构造函数。看如下代码
  • (Data为所有字段生成 getter,一个有用的 toString 方法,以及检查所有非瞬态字段的 hashCode 和 equals 实现。 还将为所有非最终字段以及构造函数生成 setter。相当于@Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode; @ConfigurationProperties 是 Spring Boot 中的标签,它可以让开发者将整个配置文件,映射到对象中,比@Value 效率更高。)
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Repository;

@ConfigurationProperties(prefix = "hello")
@Data
@Repository
public class EmployeeService {
    private String name;
    private int age;
    private double salary;

    public EmployeeService() {
        System.out.println("无参构造函数");
    }

    public EmployeeService(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
        System.out.println("有参构造函数");
    }
}
  • 测试代码
import com.example.annotation.com.service.EmployeeService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class AnnotationApplicationTests {
    @Autowired
    private EmployeeService employeeService;
    @Test
    void contextLoads() {
        System.out.println(employeeService.toString());
    }

}
  • 输出结果image-20220325112719668
注意:使用@Autowired注解基于字段的依赖注入时,IDEA会出现弱警告

image-20220325113017224

不定时更新其它注解

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值