[Sprint] 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();
    }
}

原文地址:https://www.cnblogs.com/Answer1215/p/9573811.html

时间: 2024-10-25 18:10:52

[Sprint] Properties for project configuration的相关文章

【转载】Project configuration is not up-to-date with pom.xml错误解决方法

导入一个Maven项目之后发现有一个如下的错误: Project configuration is not up-to-date with pom.xml. Run project configuration update 其实这个问题解决非常简单: 在项目上右键——[Maven]——[Update Project Configuration……] 这时会打开一个(Update Maven Dependencies)的对话框,然后勾选住出错的项目,点击Ok 这样就搞定了. 出处:http://b

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

导入一个Maven项目之后发现有一个例如以下的错误: Project configuration is not up-to-date with pom.xml. Run project configuration update 事实上这个问题解决很easy: 在项目上右键--[Maven]--[Update Project Configuration--] 这时会打开一个(Update Maven Dependencies)的对话框,然后勾选住出错的项目,点击Ok 这样就搞定了.

Project configuration is not up-to-date with pom.xml. Run Maven-&gt;Update Project or use Quick Fix

完整错误信息: Description Resource Path Location TypeProject configuration is not up-to-date with pom.xml. Run Maven->Update Project or use Quick Fix. xunge-web line 1 Maven Configuration Problem 解决办法: Project configuration is not up-to-date with pom.xml.

Project configuration is not up-to-date with pom.xml. Run Maven-&gt;Update Project

在导入一个Maven项目之后发现有一个如下的错误: Project configuration is not up-to-date with pom.xml. Run project configuration update     解决办法1: 较老的Eclipse版本: 在项目上右键——[Maven]——[Update Project Configuration……]这时会打开一个(Update Maven Dependencies)的对话框,然后勾选住出错的项目,点击Ok这样就搞定了. 比

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

导入一个Maven项目之后发现有一个如下的错误: Project configuration is not up-to-date with pom.xml. Run project configuration update 其实这个问题解决非常简单: 在项目上右键——[Maven]——[Update Project Configuration……] 这时会打开一个(Update Maven Dependencies)的对话框,然后勾选住出错的项目,点击Ok 这样就搞定了.

Maven Project configuration is not up-to-date with pom.xml异常

一.异常信息: 导入或者新建maven工程后,出现如下错误: Project configuration is not up-to-date with pom.xml. Run project configuration update   rdc     line 1    Maven Configuration Problem 二.解决方案: 右键项目,[Maven]-->[Update Project Configuration...]即可...

异常:Project configuration is not up-to-date with pom.xml 解决方案

错误描述,在导入Maven项目后出现下面错误: Description Resource Path Location Type Project configuration is not up-to-date with pom.xml. Run project configuration update DIIT-Common line 1 Maven Configuration Problem 解决方法: 右键项目选择[Maven]—>[Update Project Configuration..

Maven:Resource Path Location Type Project configuration is not up-to-date with pom.xml. Run project configuration update

Maven构建项目的时候提示: Description Resource Path Location Type Project configuration is not up-to-date with pom.xml. Run project configuration update GamesMarket line 1 Maven Configuration Problem 如下图操作即可:

Maven异常Type Project configuration is not up-to-date with pom.xml. Run Maven-&gt;Update Project or use Quick Fix

eclipse maven错误“Project configuration is not up-to-date with pom.xml. Run proje” 导入maven工程后,出现如下错误: Description Resource Path Location Type Project configuration is not up-to-date with pom.xml. Run project configuration update rdc line 1 Maven Config