• [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();
        }
    }
  • 相关阅读:
    es之零停机重新索引数据
    es之索引的别名操作
    es索引基本操作(2)之 索引映射(mappings)管理和索引库配置管理(settings)
    进程管理(八)-进程控制
    进程管理(七)-进程状态与转换
    进程管理(六)-进程的描述
    numpy数组转置与轴变换
    numpy数组的索引和切片
    numpy数组的运算
    numpy库中数组的数据类型
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9573811.html
Copyright © 2020-2023  润新知