Spring/Maven/MyBatis配置文件结合properties文件使用


使用properties文件也叫注入,比如把一些常用的配置项写入到这个文件,然后在Spring的XML配置文件中使用EL表达式去获取。

这种方式不只Spring可以使用,同样MyBatis也可以使用,只不过加载的方式不一样,但是获取值同样是EL表达式。具体的参考官方文档。

properties语法参考:https://zh.wikipedia.org/wiki/.properties,注意转移字符。

 

Spring:

本次使用的例子来自这章http://www.cnblogs.com/EasonJim/p/7055499.html,通过改造实现将数据库连接信息全部使用properties去配置。

前提:

1、新建db.properties文件,加入如下配置:

jdbc.username=root
jdbc.password=root
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/test?useUnicode\=true&characterEncoding\=UTF-8&serverTimezone\=UTC

注意:在定义这些变量的时候,尽量避免一些系统变量,比如username,这个代表操作系统的用户名。

2、在加载properties文件路径上,使用的是classpath加通配符*去实现扫描项目的classes目录下的文件来加载,不再是绝对路径,具体的用法参考:http://www.cnblogs.com/EasonJim/p/6709314.html

下面将列举加载方式:

提示:在获取值时,通常可以为其设置默认值,比如${timeout:100}

一、<context:property-placeholder />

1、在配置Spring的Bean文件时,比如引入头声明,如下所示:

复制代码
...
<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">
...
复制代码

2、Bean配置文件加入:

<context:property-placeholder ignore-unresolvable="true" location="classpath:db.properties" />

3、获取变量:

复制代码
    <!--采用DBCP连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
复制代码

二、<util:properties />

1、在配置Spring的Bean文件时,比如引入头声明,如下所示:

复制代码
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/util
                        http://www.springframework.org/schema/util/spring-util.xsd">
复制代码

2、Bean配置文件加入:

<util:properties id="db" location="classpath:db.properties"/>

3、获取变量:

复制代码
    <!--采用DBCP连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="#{db['jdbc.driver']}" />
        <property name="url" value="#{db['jdbc.url']}" />
        <property name="username" value="#{db['jdbc.username']}" />
        <property name="password" value="#{db['jdbc.password']}" />
    </bean>
复制代码

三、通过代码注入实现代码上可以获取这些变量(@Value)

1、Bean配置文件加入:

复制代码
    <!-- 使用注解注入properties中的值 -->
    <bean id="config" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:db.properties</value>
            </list>
        </property>
        <!-- 设置编码格式 -->
        <property name="fileEncoding" value="UTF-8"></property>
    </bean>
复制代码

2、在需要测试的类中写入如下代码,但是这个类必须是通过Bean注入过的,做直接的使用就是MVC的Controller。

复制代码
package com.jsoft.testmybatis.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.jsoft.testmybatis.inter.IUserOperation;
import com.jsoft.testmybatis.models.Article;

@Controller
@RequestMapping("/article")
public class UserController {
    
    @Value("#{config['jdbc.username']}")
    private String userName;
    
    @Value("#{config['jdbc.password']}")
    private String password;
    
    @Value("#{config['jdbc.url']}")
    private String url;
    
    @Value("#{config['jdbc.driver']}")
    private String driver;
    
    @Autowired
    IUserOperation userMapper;

    @RequestMapping("/list")
    public ModelAndView listall(HttpServletRequest request,HttpServletResponse response){
        
        System.out.println("测试:"+this.userName+"-"+this.password+"-"+this.url+"-"+this.driver);
        
        List<Article> articles=userMapper.getUserArticles(1); 
        ModelAndView mav=new ModelAndView("/article/list");
        mav.addObject("articles",articles);
        return mav;
    }
}
复制代码

3、另一种Bean实现,定义

复制代码
复制代码
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <array>
            <value>classpath:db.properties</value>
        </array>
    </property>
</bean>
复制代码
复制代码

4、使用@value注解获取配置文件的值

@Value("${isenable}")
private Boolean isEnable;

 

Maven:

在Maven使用很有帮助,比如多模块项目时,在Parent模块上定一个总的属性,每个模块引入之后,到时需要修改也只是一处地方。

一、直接在POM中使用properties定义属性

1、POM

    <!-- 项目属性 -->
    <properties>
        <!-- main version setting -->
        <servlet.version>3.1.0</servlet.version>
    </properties>

2、使用:

复制代码
        <!-- Servlet Library -->
        <!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${servlet.version}</version>
            <scope>provided</scope>
        </dependency>
复制代码

二、如果对于引入文件基本是无解,只能在POM中定义。

 

MyBatis:

一、通过properties节点引入:

1、在MyBatis配置文件下引入:

...
<configuration>

    <!-- 引入properties配置文件 -->
    <properties resource="config.properties" />
...

2、使用:

复制代码
    <!-- 配置环境 -->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${driver}" />
                <property name="url" value="${url}" />
                <property name="username" value="${username}" />
                <property name="password" value="${password}" />
            </dataSource>
        </environment>
    </environments>
复制代码

猜你喜欢

转载自blog.csdn.net/qq_33390613/article/details/80143369
今日推荐