[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 ...
随机推荐
- 泛型术语:占位类型placeholder
Here’s a generic version of the same code: struct Stack<Element> { var items = [Element]() mut ...
- h5混编问题总结
h5混编总结: 1.fragment 格式错误导致跳转混乱的问题:修改格式: 2.有缓存回退js不执行问题:未解决: 3.无缓存跨域回退白屏问题:解决跨域问题. 4.
- Jmeter中的参数化常用的几种方式
Jmeter中的参数化常用的几种方式,这里讲一下前两个方式,最后一个在csv参数化里已详细讲解. 1.用户参数 2.函数助手 3.CSV Data Set Config 一.用户参数 位置:添加-前 ...
- 扩展 IHttpModule
上篇提到请求进入到System.Web后,创建完HttpApplication对象后会执行一堆的管道事件,然后可以通过HttpModule来对其进行扩展,那么这篇文章就来介绍下如何定义我们自己的mod ...
- mac vim设置python语法高亮
1 "显示行号 2 set nu 3 4 "设置缩进tabstop 5 set ts=4 6 set shiftwidth=4 7 set expandtab 8 ...
- No-1.第一个 Python 程序
1. 第一个 HelloWorld 程序 1.1 Python 源程序的基本概念 Python 源程序就是一个特殊格式的文本文件,可以使用任意文本编辑软件做 Python 的开发 Python 程序的 ...
- Poj 1106 Transmitters
Poj 1106 Transmitters 传送门 给出一个半圆,可以任意旋转,问这个半圆能够覆盖的最多点数. 我们枚举每一个点作为必然覆盖点,那么使用叉积看极角关系即可判断其余的点是否能够与其存在一 ...
- I - DFS(依然是漫水填充)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
- python virtualenv 管理工具 - virtualenvwrapper
我们使用python virtualenv构建不同的python环境,python3 也加入了virtualenv 模块. virtualenvwrapper 提供了更便捷的 virtualenv环境 ...
- React Native学习(五)—— 使用插件react-native-scrollable-tab-view
本文基于React Native 0.52 Demo上传到Git了,有需要可以看看,写了新内容会上传的.Git地址 https://github.com/gingerJY/React-Native-D ...