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. 数组(Arry)几个常用方法的详解

    join() 方法用于把数组中的所有元素放入一个字符串.元素是通过指定的分隔符进行分隔的. arrayObject.join(separator)separator 可选.指定要使用的分隔符.如果省略 ...

  2. jQuery的属性与样式之样式操作.css()

    .css() 方法:获取元素样式属性的计算值或者设置元素的CSS属性 获取: .css( propertyName ) :获取匹配元素集合中的第一个元素的样式属性的计算值 .css( property ...

  3. Android(java)学习笔记202:JNI之hello.c(c代码功能实现)指针语法解析

    1. 接下来我们细讲分析一下前面一讲中,c功能实现的代码: (1)hello.c : #include <jni.h> char* getHello() { //////// return ...

  4. vue之package.json文件解析

    1.package.json是什么? 什么是Node.js的模块(Module)?在Node.js中,模块是一个库或框架,也是一个Node.js项目.Node.js项目遵循模块化的架构,当我们创建了一 ...

  5. g20学习笔记

    BALProblem.h---------定义BALProblem类. BALProblem类保存我们的BA所需要的所有数据,包括相机与路标之间的联系,相机变量+路标变量的初始值.这些数据的原始信息都 ...

  6. python 2 encode and decode

    https://docs.python.org/2/howto/unicode.html a Unicode string is a sequence of code points, which ar ...

  7. OpenMP用法大全

    OpenMP基本概念OpenMP是一种用于共享内存并行系统的多线程程序设计方案,支持的编程语言包括C.C++和Fortran.OpenMP提供了对并行算法的高层抽象描述,特别适合在多核CPU机器上的并 ...

  8. Educational Codeforces Round 58 (Rated for Div. 2) (前两题题解)

    感慨 这次比较昏迷最近算法有点飘,都在玩pygame...做出第一题让人hack了,第二题还昏迷想错了 A Minimum Integer(数学) 水题,上来就能做出来但是让人hack成了tle,所以 ...

  9. 代理模式精讲(手写JDK动态代理)

    代理模式是一种架构型模式,表现出来就是一个类代表另一个类的功能,一般用在想对访问一个类的时候做一些控制,同时又不想影响正常的业务,这种代理模式在现实的生活中应用的也非常的广泛,我用穷举法给举几个好理解 ...

  10. add list of symbols in latex

    * add list of symbols -- latex 1. createa new tex file named "toc.tex' with the following conte ...