zoukankan      html  css  js  c++  java
  • [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();
        }
    }
  • 相关阅读:
    2015 年最受 Linux 爱好者欢迎的软硬件大盘点
    Java 9终于要包含Jigsaw项目了
    Linux 容器技术史话:从 chroot 到未来
    开发者最常用的 8 款 Sublime Text 3 插件
    60,000毫秒内对Linux的性能诊断效的方法
    bzoj 2595 [Wc2008]游览计划(斯坦纳树)
    bzoj 3997 [TJOI2015]组合数学(DP)
    bzoj 1014 [JSOI2008]火星人prefix(splay+hash)
    bzoj 1090 [SCOI2003]字符串折叠(区间DP)
    bzoj 1537 [POI2005]Aut- The Bus(DP+BIT)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9573811.html
Copyright © 2011-2022 走看看