[Spring] Properties for project configuration
We might have some project specific configuration need to setup. The good approach to do this in Sprint is using 'Proptries'.
In resouces/applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config />
<!-- Load app.properties file for use -->
<context:property-placeholder location="app.properties" /> <bean name="customerRepository" class="com.pluralsight.repository.HibernateCustomerRepositoryImpl"></bean> <context:component-scan base-package="com.pluralsight" />
</beans>
In xml, we tell Sprint to looking for a file call 'app.properties', which should located in the same folder of applicationContext.xml.
Inside app.properties file we can define some variables which related to the project:
dbUsername=mysqlusername
we can inject those variable into class:
public class HibernateCustomerRepositoryImpl implements CustomerRepository {
@Value("${dbUsername}")
private String dbUsername;
@Override
public List<Customer> findAll() {
system.out.println(dbUsername)
}
}
****
We can also configure the 'properties' by using Java configuration:
in com/pluralsight/AppConfig.java:
package com.pluralsight; import com.pluralsight.repository.CustomerRepository;
import com.pluralsight.repository.HibernateCustomerRepositoryImpl;
import com.pluralsight.service.CustomerService;
import com.pluralsight.service.CustomerServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; @Configuration
@ComponentScan({"com.pluralsight"})
public class AppConfig { @Bean
public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurationn() {
return new PropertySourcesPlaceholderConfigurer();
}
}
[Spring] Properties for project configuration的更多相关文章
- springboot项目war包部署及出现的问题Failed to bind properties under 'mybatis.configuration.mapped-statements[0].
1.修改pom文件 修改打包方式 为war: 添加tomcat使用范围,provided的意思即在发布的时候有外部提供,内置的tomcat就不会打包进去 <groupId>com.scho ...
- Spring Boot @EnableAutoConfiguration和 @Configuration的区别
Spring Boot @EnableAutoConfiguration和 @Configuration的区别 在Spring Boot中,我们会使用@SpringBootApplication来开启 ...
- PhpStorm和WAMP配置调试参数,问题描述Error. Interpreter is not specified or invalid. Press “Fix” to edit your project configuration.
PhpStorm和WAMP配置调试参数 问题描述: Error. Interpreter is not specified or invalid. Press “Fix” to edit your p ...
- Maven异常Type Project configuration is not up-to-date with pom.xml. Run Maven->Update Project or use Quick Fix
eclipse maven错误“Project configuration is not up-to-date with pom.xml. Run proje” 导入maven工程后,出现如下错误: ...
- maven错误:Project configuration is not up-to-date with pom.xml
原因: 1.导入maven工程后,出现如下错误: Description Resource Path Location TypeProject configuration is ...
- Maven Project configuration is not up-to-date with pom.xml错误解决方法
导入一个Maven项目之后发现有一个如下的错误: Project configuration is not up-to-date with pom.xml. Run project configura ...
- Maven for Myeclipse的一个常见错误 Project configuration is not up-to-date with pom.xml
使用Myeclipse开发Maven项目时,经常会发现一个错误提示: Description Resource Path Location Type Project configuration is ...
- Project configuration is not up-to-date with pom.xml错误解决方法
导入一个Maven项目之后发现有一个如下的错误: Project configuration is not up-to-date with pom.xml. Run project configura ...
- PhpStorm和WAMP配置调试参数,问题描述Error. Interpreter is not specified or invalid. Press “Fix” to edit your project configuration.
PhpStorm和WAMP配置调试参数,解决实际问题. 问题描述: Error. Interpreter is not specified or invalid. Press "Fix&qu ...
随机推荐
- CAD交互绘制圆弧(com接口)
在CAD设计时,需要绘制圆弧,用户可以在图面点圆弧起点,圆弧上的一点和圆弧的终点,这样就绘制出圆弧. 主要用到函数说明: _DMxDrawX::DrawArc2 由圆弧上的三点绘制一个圆弧.详细说明如 ...
- 多线程下单例模式的实现_ThreadLocal_ReentrantLock
package threadStudy; public class MultiThreadSingleInstance { // volatile 防止指令重排 private static vola ...
- HR教你面试时怎么谈出高工资
不是任何时候谈钱都会伤感情,比如跟客户谈合同报价,跟房东谈房租,以及面试时和公司HR谈新工作的薪酬待遇. 这事儿一般不需要你先开口.在面试进入尾声的时候,如果HR对你还算满意,通常就会开始问你目前的薪 ...
- win7 命令行禁用开启usb存储
禁用: reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\usbstor" /v Start /t reg ...
- zabbix4.2学习笔记--监控tomcat
zabbix提供了一个java gateway的应用去监控jmx(Java Management Extensions,即Java管理扩展)是一个为应用程序.设备.系统等植入管理功能的框架 环境 主机 ...
- 【C语言】控制台窗口图形界面编程(八):键盘事件
目录 00. 目录 01. INPUT_RECORD结构 02. KEY_EVENT_RECORD结构 03. ReadConsoleInput函数 04. 示例程序 00. 目录 01. INPUT ...
- docker centos7 配置和宿主机同网段IP
docker centos7 配置和宿主机同网段IP 1.安装brctl 命令 # yum -y install bridge-utils 2.编辑网卡配置文件 # vi ifcfg-eno16777 ...
- ERROR 1133 (42000): Can't find any matching row in the user table
环境:操作系统:Redhat 7.5 x86-64 数据库版本MySQL 5.7.25 现象:ERROR 1133 (42000): Can't find any matching row in th ...
- iphone X 的适配
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- CSU——2161: 漫漫上学路 最短路
Description 众所周知,CSU(California State)University) 的上课地点距离学生公寓很远,对于爱睡懒觉的小Z来说,每天去上课就成了一件非常头疼的事,如果有早课的话 ...