摘要:配置文件去除硬编码大致有三种方式使用使用占位符使用表达式引入属性文件注入,使用系列方法获取属性由于个人觉得占位符的方法是中的一种,所以只展示这一种引入属性文件使用注入属性相当于注入值的,但是更为强大,还有其他用处占位符关于引用属性文件使用
spring配置文件去除硬编码 大致有三种方式
使用 org.springframework.core.env.Environment
使用占位符
使用 spring 表达式(SpEL)
application.properties</>复制代码
datasource.url=jdbc:mysql://localhost:3306/spring_test?useUnicode=true&characterEncoding=utf-8
datasource.driverClassName=com.mysql.jdbc.Driver
datasource.username=root
datasource.password=123456
datasource.initialSize=5
datasource.maxActive=10
datasource.maxWait=6000
Environment
</>复制代码
@Configuration
@PropertySource("classpath:application.properties")
@EnableTransactionManagement
public class DataConfig {
@Autowired
private Environment env;
@Bean
public DruidDataSource dataSource() {
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(env.getProperty("datasource.driverClassName"));
ds.setUrl(env.getProperty("datasource.url"));
ds.setUsername(env.getProperty("datasource.username"));
ds.setPassword(env.getProperty("datasource.password"));
ds.setInitialSize(5);
ds.setMaxActive(10);
ds.setMaxWait(60000);
return ds;
}
...
}
@PropertySource 引入属性文件
注入 Environment,使用 getProperty 系列方法获取属性
SpEL</>复制代码
@Configuration
@PropertySource("classpath:application.properties")
@EnableTransactionManagement
public class DataTestConfig {
@Value("${datasource.driverClassName}")
private String driverClassName;
@Value("${datasource.url}")
private String url;
@Value("${datasource.username}")
private String username;
@Value("${datasource.password}")
private String password;
@Value("${datasource.initialSize}")
private int initialSize;
@Value("${datasource.maxActive}")
private int maxActive;
@Value("${datasource.maxWait}")
private int maxWait;
@Bean
public DruidDataSource dataSource() {
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(driverClassName);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
ds.setInitialSize(initialSize);
ds.setMaxActive(maxActive);
ds.setMaxWait(maxWait);
return ds;
}
...
}
由于个人觉得占位符的方法是SpEL中的一种,所以只展示这一种
@PropertySource 引入属性文件
使用 @Value 注入属性(相当于注入值的 bean,但是 SpEL 更为强大,还有其他用处)
占位符 ${...}
关于引用属性文件使用 @PropertySource 引入,如上
javaconfig,使用 PropertySourcesPlaceholderConfigurer 引入
</>复制代码
@Bean
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
PropertySourcesPlaceholderConfigurer placeholderConfigurer =
new PropertySourcesPlaceholderConfigurer();
placeholderConfigurer.setLocations(new ClassPathResource("application.properties"));
return placeholderConfigurer;
}
xml 方式(这个没写,其实就是将javaconfig转换成xml配置即可)
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/67174.html
摘要:约束的位置可以在约束的位置在中找到开启注解扫描器在配置文件中开启注解扫描器开启注解扫描器包含自己以及自己下面的所有子包告知框架,在读取配置文件,创建容器时,依据注解创建对象,并存入容器中使用注解要创建对象,在类上使用注解。 1. Spring整合连接池 1.1 Spring整合C3P0 在工程中导入c3p0连接池需要的包com.springsource.com.mchange.v2...
摘要:利用这种方式就将或者和业务对象的依赖关系用来进行管理,并且不用在中硬编码要引用的对象名字。配置的的配置完成。推荐使用,应为配置上更简单。 在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象 的创建。如果要在filter或者servlet中使用spring容器管理业务对象,通常需要使用WebApplic...
摘要:一级缓存值得注意的地方默认就是支持一级缓存的,并不需要我们配置和整合后进行代理开发,不支持一级缓存,和整合,按照的模板去生成代理对象,模板中在最后统一关闭。总结的一级缓存是级别的。 前言 本文主要讲解Mybatis的以下知识点: Mybatis缓存 一级缓存 二级缓存 与Ehcache整合 Mapper代理 使用Mapper代理就不用写实现类了 逆向工程 自动生成代码 ...
摘要:配置之痛与解决之道当有很多项目需要你来不停的向集成测试和生产环境发时配置文件的差异性会造成很大的困挠一方面你不希望把敏感信息到处保存另一方面会大量的地址信息需要配置每个引入的第三方都需要配置往往不同环境的还都不一样一旦搞错了轻则项目起不来 配置之痛与解决之道 当有很多项目需要你来不停的向集成,测试和生产环境发时, 配置文件的差异性会造成很大的困挠.一方面你不希望把敏感信息到处保存. 另...
摘要:原文地址运行时注入与硬编码注入是相对的。硬编码注入在编译时就已经确定了,运行时注入则可能需要一些外部的参数来解决。提供的两种在运行时求值的方式属性占位符表达式语言注入外部的值使用注解可以引入文件,使用其中的值。 原文地址:http://blog.gaoyuexiang.cn/Sp... 运行时注入与硬编码注入是相对的。硬编码注入在编译时就已经确定了,运行时注入则可能需要一些外部的参数来...
阅读 2848·2021-11-25 09:43
阅读 2257·2021-11-24 09:39
阅读 2216·2021-11-17 09:33
阅读 2872·2021-09-27 14:11
阅读 2041·2019-08-30 15:54
阅读 3325·2019-08-26 18:27
阅读 1335·2019-08-23 18:00
阅读 1896·2019-08-23 17:53