[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 ...
随机推荐
- js基础盲点
var myarray= new Array(8); //创建数组,存储8个数据. 注意:1.创建的新数组是空数组,没有值,如输出,则显示undefined.2.虽然创建数组时,指定了长度,但实际上数 ...
- 1、QQ装机部落---腾讯软件
亲,赚钱快人一步,我给你推荐QQ装机部落!大品牌,口碑好,知名软件,绿色无毒,每天结算,回报高!到这里注册:http://zjbl.qq.com/#/?invid=476058088 返利网邀请码立即 ...
- 利用ObjectMapper readValue()和泛型解决复杂json结构
import com.dj.fss.vo.MessageListVO; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; im ...
- PHP面向对象考察点
面向对象三大特性 封装 封装性就是把对象的属性和服务结合成一个独立的相同单位,并尽可能隐蔽对象的内部细节,包含两个含义: 把对象的全部属性和全部服务结合在一起,形成一个不可分割的独立单位(即对象). ...
- centos7.2密码在单用户下面的修改
centos7.2在但用户模式下面的修改 1.开机启动 2.grub模式按E健 3.Linux16行的"ro"修改为 "rw init=/sysroot/bin/sh&q ...
- Android(java)学习笔记192:ContentProvider使用之虚拟短信
1.虚拟短信应用场景: 急着脱身?应付老婆(老公.男女朋友查岗)? 使用虚拟通话短信吧.您只需通过简单设置,软件就会在指定时间会模拟一个“真实”来电或短信来迷惑对方,通过“真实”的证据让对方相 ...
- 04Struts2的配置文件
Struts2的配置文件 1.1 struts.xml中的标签详解 1.1.1 constant标签 作用: 用于修改struts2中的常量 属性: name:指定常量的key value:指 ...
- Js获取操作系统版本 && 获得浏览器版本
//利用原生Js获取操作系统版本function getOS() { var sUserAgent = navigator.userAgent; var isWin = (navigator.plat ...
- Getting start with dbus in systemd (02) - How to create a private dbus-daemon
Getting start with dbus in systemd (02) 创建一个私有的dbus-daemon (session) 环境 这里我们会有两个app: app1(client),ap ...
- 【MSSQL】MDF、NDF、LDF文件的含义
[MSSQL]MDF.NDF.LDF文件的含义 2012-09-03 15:32:56| 分类: SQL数据库|举报|字号 订阅 MDF是 primary data file 的缩写:NDF ...