Spring Boot常用注解

1.Spring Boot常用注解

分类注解位置对应的XML标签作用
读取配置@ConfigurationProperties通过一次性读取到一个 Java 配置类,相当于一次性使用多个@Value注解
@PropertySource<context:property-placeholder>用来指定读取自定义的配置文件的
@Import<import>用来导入配置类或者一些需要前置加载的类
@Value属性<property>注入普通字符,操作系统属性,表达式结果,
其它bean属性,文件资源,网站资源,配置文件等
配置启动@SpringBootApplication此注解相当于@Configuration@EnableAutoConfiguration@ComponentScan的组合
@Configuration 声明当前类为配置类,相当于xml形式的Spring配置
@EnableAutoConfiguration启用SpringBoot的自动化配置
@ComponentScan<context:component-scan>启用SpringBoot的组件扫描功能
全局异常处理@ControllerAdvice常与@ExceptionHandler注解一起使用,用于捕获全局异常,能作用于所有controller中。
@ExceptionHandler类/方法修饰方法时,表示该方法为处理全局异常的方法
数据库事务相关注解@EnableTransactionManagement启用Spring基于注解的事务管理功能,需要和@Configuration注解一起使用
@Transactional类/方法表示方法和类需要开启事务

Spring相关注解可参考:Spring常用注解

2.读取配置

@Import

@Import注解是用来导入配置类或者一些需要前置加载的类。作用和在spring的xml配置文件中的:<import resource=""></import>是一样.

value[]:用于指定其他配置类的字节码。

@Configuration
@ComponentScan(basePackages = "com.by")
@Import({Configuration_Other.class})
public class SpringConfiguration {

}

@PropertySource("classpath:config.properties")
public class Configuration_Other {

}

@value

可以在任意 Spring 管理的 Bean 中通过这个注解获取任何来源配置的属性值。
比如 application.properties 或者 application.yml 配置文件中配置的属性值,当然也可以是 JVM 属性配置或者系统环境变量配置的值.

@Component
public class MyComponent {
 
    @Value("${my.property}")
    private String myProperty;
 
    // 其他方法
}

在属性级别上,@Value注解指定了需要注入的属性值,这个属性值可以通过${...}的方式引用配置文件中的属性值。

@ConfigurationProperties

上面 @Value 在每个类中获取属性配置值的做法其实是不推荐的,一般在企业项目开发中,不会使用那么杂乱无章的写法而且维护也麻烦。

通过一次性读取到一个 Java 配置类,相当于一次性使用多个@Value注解,需要使用的地方直接引用这个类就可以多次访问了,方便维护。

@Component
@Data
@ConfigurationProperties(prefix = "servers")
public class ServerConfig {
    private String ipAddress;
    private int port;
    private long timeout;
}

@PropertySource

这个注解是用来指定读取自定义的配置文件的。

@Component
@ConfigurationProperties(prefix= "my" )
@PropertySource(value = {"classpath:my.properties"})
@Data
public class MyProperties {
    private int maxValue= 0;
}

搭配@Bean使用:

@PropertySource("classpath:db.properties")
public class JdbcConfig {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String userName;
    @Value("${jdbc.password}")
    private String password;

    @Bean("dataSource")
    public DataSource getDatasource() {
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(userName);
        ds.setPassword(password);
        return ds;
    }
}

3.配置启动

@SpringBootApplication

@SpringBootApplication注解是一个快捷的配置注解,在被它标注的类中,可以定义一个或多个Bean,并自动触发自动配置Bean和自动扫描组件。此注解相当于@Configuration@EnableAutoConfiguration@ComponentScan的组合。

在Spring Boot应用程序的主类中,就使用了此注解。示例代码如下:

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

@Configuration

用于声明一个Java形式的配置类,SpringBoot推荐使用Java配置,在该类中声明的Bean等配置将被SpringBoot的组件扫描功能扫描到。

@Configuration
@MapperScan({"com.by.mapper"})
public class MyBatisConfig {
}

@EnableAutoConfiguration

启用SpringBoot的自动化配置,会根据你在pom.xml添加的依赖和application-dev.yml中的配置自动创建你需要的配置。

 @Configuration
@EnableAutoConfiguration
public class AppConfig {
}

@ComponentScan

启用SpringBoot的组件扫描功能,标注哪些路径下的类需要被Spring扫描。将自动装配和注入指定包下的Bean实例。

@Configuration
@ComponentScan({"com.by.mapper"})
public class EruptConfig {
}

4.全局异常处理

@ControllerAdvice

包含@Component。可以被扫描到。统一处理异常。

常与@ExceptionHandler注解一起使用,用于捕获全局异常,能作用于所有controller中。

@ExceptionHandler

修饰方法时,表示该方法为处理全局异常的方法。

@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = ApiException.class)
    public CommonResult handle(ApiException e) {
        if (e.getErrorCode() != null) {
            return CommonResult.failed(e.getErrorCode());
        }
        return CommonResult.failed(e.getMessage());
    }
}

5.数据库事务相关注解

@EnableTransactionManagement

启用Spring基于注解的事务管理功能,需要和@Configuration注解一起使用。

@Configuration
@EnableTransactionManagement
@MapperScan({"com.by.mapper"})
public class MyBatisConfig {
}

@Transactional

表示方法和类需要开启事务

当作用与类上时,类中所有方法均会开启事务

当作用于方法上时,方法开启事务,方法上的注解无法被子类所继承。

@Service
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;
    @Override
    public void add(CheckItem checkItem) {
        checkItemMapper.add(checkItem);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

这河里吗l

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值