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的更多相关文章

  1. springboot项目war包部署及出现的问题Failed to bind properties under 'mybatis.configuration.mapped-statements[0].

    1.修改pom文件 修改打包方式 为war: 添加tomcat使用范围,provided的意思即在发布的时候有外部提供,内置的tomcat就不会打包进去 <groupId>com.scho ...

  2. Spring Boot @EnableAutoConfiguration和 @Configuration的区别

    Spring Boot @EnableAutoConfiguration和 @Configuration的区别 在Spring Boot中,我们会使用@SpringBootApplication来开启 ...

  3. 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 ...

  4. 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工程后,出现如下错误: ...

  5. maven错误:Project configuration is not up-to-date with pom.xml

    原因: 1.导入maven工程后,出现如下错误: Description    Resource    Path    Location    TypeProject configuration is ...

  6. 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 ...

  7. Maven for Myeclipse的一个常见错误 Project configuration is not up-to-date with pom.xml

    使用Myeclipse开发Maven项目时,经常会发现一个错误提示: Description Resource Path Location Type Project configuration is ...

  8. Project configuration is not up-to-date with pom.xml错误解决方法

    导入一个Maven项目之后发现有一个如下的错误: Project configuration is not up-to-date with pom.xml. Run project configura ...

  9. 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 ...

随机推荐

  1. 迅为iTOP-4418开发板串口虚拟控制台配置为普通串口

    如何将 iTOP-4418 的串口控制台配置为普通串口.在最终的产品中,如果需要额外的串口,可以将调试用的控制台串口作为普通串口使用,因为交付给最终用户手中的产品,一般不需要调试串口.屏蔽串口控制台需 ...

  2. (转)淘淘商城系列——使用maven tomcat插件启动聚合工程

    http://blog.csdn.net/yerenyuan_pku/article/details/72672389 上文我们一起学习了如何使用maven tomcat插件来启动web工程,本文我们 ...

  3. 谷歌全屏脚本 start chrome.exe --kiosk http://www.baidu.com

    start chrome.exe --kiosk http://www.baidu.com

  4. CAD参数绘制圆弧(com接口)

    在CAD设计时,需要绘制圆弧,用户可以在图面点圆弧起点,圆弧上的一点和圆弧的终点,这样就绘制出圆弧. 主要用到函数说明: _DMxDrawX::DrawArc2 由圆弧上的三点绘制一个圆弧.详细说明如 ...

  5. 使用JQuery MiniUI,json数据构建TreeGrid(树图)

    index.html直接上代码. 需要引用MiniUI的boot.js <!DOCTYPE html> <html> <head> <meta charset ...

  6. java线程池 多线程搜索文件包含关键字所在的文件路径

    文件读取和操作类 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; publi ...

  7. js模拟输入支付密码

    html <div class="content"> <!--<div class="title">支付宝支付密码:</di ...

  8. Jedis集成到项目中

    Jedis整合到项目中,就可以在项目中使用redis了,作为Java程序狗,这个可以会,贴代码了,不截图了,哈哈 一.maven中的pom.xml中添加依赖 <dependency> &l ...

  9. MFC定时器的使用

    巧妙地使用定时器能达到意想不到的效果,写界面的时候能实现渐变,也能帮助多线程控制等我们知道,在VC的MFC中,已经为我们封装好了很多全面和强大的函数集,所以在MFC编程时,巧妙地调用MFC函数库可以为 ...

  10. laravel学习笔记1--基础

    一.安装 1.安装 composer create-project laravel/laravel=v5.5.28 laravel 2.测试 配置nginx根目录为laravel/public,并且美 ...