★@ConfigurationProperties和@EnableConfigurationProperties配合使用

@ConfigurationProperties注解主要用来把properties配置文件转化为bean来使用的,而@EnableConfigurationProperties注解的作用是@ConfigurationProperties注解生效。如果只配置@ConfigurationProperties注解,在IOC容器中是获取不到properties配置文件转化的bean的。使用如下:

step1、spring boot启动时默认是加载application.properties配置文件的,假设该配置文件中内容为:
  local.host=127.0.0.1

  local.port=8080

step2、创建一个类ComponentProperties,把配置文件转化为bean来使用。@ConfigurationProperties注解可以把properties文件转化为bean,然后使用@Component注解把该bean注入到IOC容器中。

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; @Component
/*prefix定义配置文件中属性*/
@ConfigurationProperties(prefix="local")
public class ComponentProperties { /*host和port属性必须保持与application.properties中的属性一致*/
private String host;
private String port; public void setHost(String host) {
this.host = host;
}
public void setPort(String port) {
this.port = port;
} @Override
public String toString() {
return "ComponentProperties [host=" + host + ", port=" + port + "]";
} }

step3、用@EnableConfigurationProperties注解使@ConfigurationProperties生效,并从IOC容器中获取bean。

import org.springframework.boot.SpringApplication;
import
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan; //@SpringBootApplication
@ComponentScan
/* @EnableConfigurationProperties注解是用来开启对@ConfigurationProperties注解配置Bean的支持。
也就是@EnableConfigurationProperties注解告诉Spring Boot 使能支持@ConfigurationProperties*/
@EnableConfigurationProperties
public class Springboot3Application { public static void main(String[] args) throws Exception { ConfigurableApplicationContext context = SpringApplication.run(Springboot3Application.class, args);
/*@ConfigurationProperties注解和@EnableConfigurationProperties配合使用*/
System.out.println(context.getBean(ComponentProperties.class));
context.close();
}
}

启动类如上,@ComponentScan注解默认扫描启动类所在的包,该包下的类如果注入到了IOC容器中,那么在该启动类就能获取注入的bean。然后用@EnableConfigurationProperties注解使@ConfigurationProperties注解生效。因此在该启动类中就可以获取刚才application.properties配置文件转化的bean了。另外,只使用@SpringBootApplication一个注解也是可以的,因为@SpringBootApplication注解中已经包含了@ComponentScan和@EnableAutoConfiguration注解,@EnableAutoConfiguration注解最终使ConfigurationPropertiesAutoConfiguration类上的@EnableAutoConfiguration生效。

输出结果如下:ComponentProperties [host=127.0.0.1, port=8080]
原文:https://blog.csdn.net/u010502101/article/details/78758330

  EnableAsync注解的意思是可以异步执行,就是开启多线程的意思。可以标注在方法、类上。

@Component
public class Task { @Async
public void doTaskOne() throws Exception {
// 同上内容,省略
}
}

为了让@Async注解能够生效,还需要在Spring Boot的主程序中配置@EnableAsync,如下所示:

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

注: @Async所修饰的函数不要定义为static类型,这样异步调用不会生效

★  @EnableTransactionManagement   // 启注解事务管理,等同于xml配置方式的 <tx:annotation-driven />

关于事务管理器,不管是JPA还是JDBC等都实现自接口 PlatformTransactionManager 如果你添加的是 spring-boot-starter-jdbc 依赖,框架会默认注入 DataSourceTransactionManager 实例。如果你添加的是 spring-boot-starter-data-jpa 依赖,框架会默认注入 JpaTransactionManager 实例。你可以在启动类中添加如下方法,Debug测试,就能知道自动注入的是 PlatformTransactionManager 接口的哪个实现类。

@EnableTransactionManagement // 启注解事务管理,等同于xml配置方式的 <tx:annotation-driven />
@SpringBootApplication
public class ProfiledemoApplication { @Bean
public Object testBean(PlatformTransactionManager platformTransactionManager){
System.out.println(">>>>>>>>>>" + platformTransactionManager.getClass().getName());
return new Object();
} public static void main(String[] args) {
SpringApplication.run(ProfiledemoApplication.class, args);
}
}

这些SpringBoot为我们自动做了,这些对我们并不透明,如果你项目做的比较大,添加的持久化依赖比较多,我们还是会选择人为的指定使用哪个事务管理器。 
代码如下:

@EnableTransactionManagement
@SpringBootApplication
public class ProfiledemoApplication { // 其中 dataSource 框架会自动为我们注入
@Bean
public PlatformTransactionManager txManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
} @Bean
public Object testBean(PlatformTransactionManager platformTransactionManager) {
System.out.println(">>>>>>>>>>" + platformTransactionManager.getClass().getName());
return new Object();
} public static void main(String[] args) {
SpringApplication.run(ProfiledemoApplication.class, args);
}
}

在Spring容器中,我们手工注解@Bean 将被优先加载,框架不会重新实例化其他的 PlatformTransactionManager 实现类。

然后在Service中,被 @Transactional 注解的方法,将支持事务。如果注解在类上,则整个类的所有方法都默认支持事务。

对于同一个工程中存在多个事务管理器要怎么处理,请看下面的实例,具体说明请看代码中的注释。

@EnableTransactionManagement // 开启注解事务管理,等同于xml配置文件中的 <tx:annotation-driven />
@SpringBootApplication
public class ProfiledemoApplication implements TransactionManagementConfigurer { @Resource(name="txManager2")
private PlatformTransactionManager txManager2; // 创建事务管理器1
@Bean(name = "txManager1")
public PlatformTransactionManager txManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
} // 创建事务管理器2
@Bean(name = "txManager2")
public PlatformTransactionManager txManager2(EntityManagerFactory factory) {
return new JpaTransactionManager(factory);
} // 实现接口 TransactionManagementConfigurer 方法,其返回值代表在拥有多个事务管理器的情况下默认使用的事务管理器
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
return txManager2;
} public static void main(String[] args) {
SpringApplication.run(ProfiledemoApplication.class, args);
} }
@Component
public class DevSendMessage implements SendMessage { // 使用value具体指定使用哪个事务管理器
@Transactional(value="txManager1")
@Override
public void send() {
System.out.println(">>>>>>>>Dev Send()<<<<<<<<");
send2();
} // 在存在多个事务管理器的情况下,如果使用value具体指定
// 则默认使用方法 annotationDrivenTransactionManager() 返回的事务管理器
@Transactional
public void send2() {
System.out.println(">>>>>>>>Dev Send2()<<<<<<<<");
} }

注:   如果Spring容器中存在多个 PlatformTransactionManager 实例,并且没有实现接口 TransactionManagementConfigurer 指定默认值,在我们在方法上使用注解 @Transactional 的时候,就必须要用value指定,如果不指定,则会抛出异常。

对于系统需要提供默认事务管理的情况下,实现接口 TransactionManagementConfigurer 指定。

对有的系统,为了避免不必要的问题,在业务中必须要明确指定 @Transactional 的 value 值的情况下。不建议实现接口 TransactionManagementConfigurer,这样控制台会明确抛出异常,开发人员就不会忘记主动指定。

来源: https://www.cnblogs.com/xingzc/p/6029483.html

@EnableConfigurationProperties、@EnableAsync、 @EnableTransactionManagement的更多相关文章

  1. 007-Spring Boot-@Enable*注解的工作原理-EnableConfigurationProperties、ImportSelector、ImportBeanDefinitionRegistrar

    一.@Enable* 启用某个特性的注解 1.EnableConfigurationProperties 回顾属性装配 application.properties中添加 tomcat.host=19 ...

  2. Spring 和 SpringMVC 常用注解和配置(@Autowired、@Resource、@Component、@Repository、@Service、@Controller的区别)

    Spring 常用注解 总结内容 一.Spring部分 1.声明bean的注解 2.注入bean的注解 3.java配置类相关注解 4.切面(AOP)相关注解 5.事务注解 6.@Bean的属性支持 ...

  3. Spring Boot -- Spring Boot之@Async异步调用、Mybatis、事务管理等

    这一节将在上一节的基础上,继续深入学习Spring Boot相关知识,其中主要包括@Async异步调用,@Value自定义参数.Mybatis.事务管理等. 本节所使用的代码是在上一节项目代码中,继续 ...

  4. 006-优化web请求二-应用缓存、异步调用【Future、ListenableFuture、CompletableFuture】、ETag、WebSocket【SockJS、Stomp】

    四.应用缓存 使用spring应用缓存.使用方式:使用@EnableCache注解激活Spring的缓存功能,需要创建一个CacheManager来处理缓存.如使用一个内存缓存示例 package c ...

  5. Spring Boot多数据源配置(一)durid、mysql、jpa整合

    目前在做一个统计项目.需要多数据源整合,其中包括mysql和mongo.本节先讲mysql.durid.jpa与spring-boot的整合. 引入Durid包 <dependency> ...

  6. Feign、httpclient、OkHttp3 结合使用

    疯狂创客圈 Java 高并发[ 亿级流量聊天室实战]实战系列 [博客园总入口 ] 疯狂创客圈 正在进行分布式和高并发基础原理 的研习,比如下面的一些基础性的内容: 一.Netty Redis 亿级流量 ...

  7. SpringBoot官方文档学习(三)配置文件、日志、国际化和JSON

    一.Profiles Spring配置文件提供了一种方法来隔离应用程序配置的各个部分,并使其仅在某些环境中可用.任何@Component.@Configuration或@ConfigurationPr ...

  8. Mybatis Plus带多条件的多表联合、分页、排序查询

    目录 一.现有表 student学生表: facultylist学院表: 二.同时满足以下需求: 1.多表联合查询出学院名字 2.可以带多条件查询 3.指定页码,页数据大小进行物理分页查询 三.解决步 ...

  9. SpringBoot与缓存、消息、检索、任务、安全与监控

    一.Spring抽象缓存 Spring从3.1开始定义了org.springframework.cache.Cache和org.springframework.cache.CacheManager接口 ...

随机推荐

  1. 图解前序遍历线索化二叉树,前序线索二叉树遍历,C\C++描述

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  2. 字符与字符串3——char 的大小

    字符变量占用内存的大小,也就是char类型声明的变量,这个变量占多少字节. 一字节 char c = 'A'; printf("%d,%d\n", sizeof(c),sizeof ...

  3. IDEA使用GitHub托管代码

    该方法基本也适用于JetBrains公司的其他IDE产品,如phpStorm,PyCharm等. 首先,在github官网注册一个账号,参考:http://stormzhang.com/github/ ...

  4. :工厂模式1:方法模式--Pizza

    #ifndef __PIZZA_H__ #define __PIZZA_H__ class Pizza { public: Pizza(){} virtual ~Pizza(){} virtual c ...

  5. 开发框架DevExtreme发布v18.2.4|附下载

    DevExtreme Complete Subscription是性能最优的 HTML5,CSS 和 JavaScript 移动.Web开发框架,可以直接在Visual Studio集成开发环境,构建 ...

  6. 201621123001 《java程序设计》第2周学习总结

    1. 本周学习总结 学会String 类的一些用法,比如用subString()截取字符串,频繁对字符串进行修改应使用StringBuilder()等. 学会Array 类的一些用法,比如sort() ...

  7. python 高级语言特性

    装饰器decorator的使用 在某公司的一次笔试中面试官出了一道题,使用python 的decorator实现一个函数的执行时间的计算. 分析:关于函数执行时间的计算,那么肯定是执行之前得到一个时间 ...

  8. delete CDU

    function DeletePDU(){ global $person; $this->MakeSafe(); // Do not attempt anything else if the l ...

  9. slaac

    https://zhidao.baidu.com/question/460186176.html slaac是IPv6中的术语.Stateless address autoconfiguration, ...

  10. 使用solr报错

    2017-11-15  20:15:18 错误介绍: 错误原因:url错误 错误解决:换成正确