国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解SpringMVC加載配置Properties文件的幾種方式

詳解SpringMVC加載配置Properties文件的幾種方式

2020-08-19 11:37webin Java教程

這篇文章主要介紹了詳解SpringMVC加載配置Properties文件的幾種方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下。

最近開發的項目使用了SpringMVC的框架,用下來感覺SpringMVC的代碼實現的非常優雅,功能也非常強大,

網上介紹Controller參數綁定、URL映射的文章都很多了,寫這篇博客主要總結一下SpringMVC加載配置Properties文件的幾種方式

1.通過context:property-placeholde實現配置文件加載

1.1、在spring.xml中加入context相關引用

?
1
2
3
4
5
6
7
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd">

 1.2、引入jdbc配置文件        

?
1
<context:property-placeholder location="classpath:jdbc.properties"/>

1.3、jdbc.properties的配置如下

?
1
2
3
4
jdbc_driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8
jdbc_username=root
jdbc_password=123456

1.4、在spring-mybatis.xml中引用jdbc中的配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
  destroy-method="close" >
  <property name="driverClassName">
   <value>${jdbc_driverClassName}</value>
  </property>
  <property name="url">
   <value>${jdbc_url}</value>
  </property>
  <property name="username">
   <value>${jdbc_username}</value>
  </property>
  <property name="password">
   <value>${jdbc_password}</value>
  </property>
  <!-- 連接池最大使用連接數 -->
  <property name="maxActive">
   <value>20</value>
  </property>
  <!-- 初始化連接大小 -->
  <property name="initialSize">
   <value>1</value>
  </property>
  <!-- 獲取連接最大等待時間 -->
  <property name="maxWait">
   <value>60000</value>
  </property>
  <!-- 連接池最大空閑 -->
  <property name="maxIdle">
   <value>20</value>
  </property>
  <!-- 連接池最小空閑 -->
  <property name="minIdle">
   <value>3</value>
  </property>
  <!-- 自動清除無用連接 -->
  <property name="removeAbandoned">
   <value>true</value>
  </property>
  <!-- 清除無用連接的等待時間 -->
  <property name="removeAbandonedTimeout">
   <value>180</value>
  </property>
  <!-- 連接屬性 -->
  <property name="connectionProperties">
   <value>clientEncoding=UTF-8</value>
  </property>
 </bean>

1.5、在Java類中引用jdbc.properties中的配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JdbcConfig{  
   
  @Value("${jdbc_url}")
  public String jdbcUrl; //這里變量不能定義成static
   
  @Value("${jdbc_username}")
  public String username; 
   
  @Value("${jdbc_password}")
  public String password; 
    
}

1.6、在controller中調用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@RequestMapping("/service/**")
@Controller
public class JdbcController{
  
     @Autowired
   private JdbcConfig Config; //引用統一的參數配置類
 
     @Value("${jdbc_url}")
     private String jdbcUrl; //直接在Controller引用
     @RequestMapping(value={"/test"}) 
    public ModelMap test(ModelMap modelMap) { 
        modelMap.put("jdbcUrl", Config.jdbcUrl);
        return modelMap; 
     }
     @RequestMapping(value={"/test2"})
   public ModelMap test2(ModelMap modelMap) { 
      modelMap.put("jdbcUrl", this.jdbcUrl);
      return modelMap;
   
}

1.7、測試

在ie中輸入http://localhost:8080/testWeb/service/test 或http://localhost:8080/testWeb/service/test2 

返回如下結果:

?
1
2
3
  jdbcUrl:"jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8"
}

注:通過context:property-placeholde加載多個配置文件

 只需在第1.2步中將多個配置文件以逗號分隔即可

 

復制代碼 代碼如下:

<context:property-placeholder location="classpath:jdbc.properties,classpath:XXX.properties"/> 

 

2、通過util:properties實現配置文件加載

2.1、在spring.xml中加入util相關引用

?
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 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-4.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/util
   http://www.springframework.org/schema/util/spring-util-4.0.xsd">

2.2、 引入config配置文件 

?
1
<util:properties id="settings" location="classpath:config.properties"/>

2.3、config.properties的配置如下

?
1
gnss.server.url=http://127.0.0.1:8080/gnss/services/data-world/rest

2.4、在java類中引用config中的配置  

?
1
2
3
4
5
6
7
8
9
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component
public class Config { 
   @Value("#{settings['gnss.server.url']}"
   public String GNSS_SERVER_URL; 
  
}

2.5、在controller中調用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@RequestMapping("/service2/**")
@Controller
public class ConfigController{
  
     @Autowired
   private Config Config; //引用統一的參數配置類
 
     @RequestMapping(value={"/test"})
   public ModelMap test(ModelMap modelMap) { 
    modelMap.put("gnss.service.url",Config.GNSS_SERVER_URL);
      return modelMap;
   
}

2.6、測試

在ie中輸入http://localhost:8080/testWeb/service2/test

返回如下結果:

?
1
2
3
{
  "gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest"
}

3.直接在Java類中通過注解實現配置文件加載

3.1、在java類中引入配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
 
@Configuration
@PropertySource(value="classpath:config.properties"
public class Config { 
  
@Value("${gnss.server.url}"
public String GNSS_SERVER_URL;
 
@Value("${gnss.server.url}"
public String jdbcUrl; 
 
}
3.2、在controller中調用
@RequestMapping("/service2/**")
@Controller
public class ConfigController{
  
     @Autowired
   private Config Config; //引用統一的參數配置類
 
     @RequestMapping(value={"/test"})
   public ModelMap test(ModelMap modelMap) { 
    modelMap.put("gnss.service.url", Config.GNSS_SERVER_URL);
      return modelMap;
   
}

3.3、測試

在ie中輸入http://localhost:8080/testWeb/service2/test

返回如下結果:

?
1
2
3
{
  "gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest"
 }

最后附上spring.xml的完整配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 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-4.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/util
   http://www.springframework.org/schema/util/spring-util-4.0.xsd">
 
  <!-- 引入jdbc配置文件 -->
  <context:property-placeholder location="classpath:jdbc.properties"/>
 
   <!-- 引入多配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties,classpath:XXX.properties"/>
   <!-- 通過util引入config配置文件 -->
   <!-- <util:properties id="settings" location="classpath:config.properties" /> -->
   <!-- 掃描文件(自動將servicec層注入) -->
   <context:component-scan base-package="修改成你的Config類所在的package"/></beans>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/swebin/article/details/57074660

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美激情网 | 亚洲成人久久久 | 日本网站在线免费观看 | 中文字幕日韩视频 | 国产精品久久久久久亚洲调教 | 欧美黑人性暴力猛交喷水黑人巨大 | 亚洲精品永久免费 | 亚洲精品乱码久久久久膏 | 国产精品99 | 午夜av影院 | 亚洲精品一区二区三区四区高清 | 依人在线 | 永久91嫩草亚洲精品人人 | 久久69精品久久久久久久电影好 | 精品一二三区 | 玖玖色资源 | 夜夜艹| 亚洲+变态+欧美+另类+精品 | 伊人中文| 国产精品99久久久久久www | 99热a| 91精品国产综合久久久久久丝袜 | 一级一毛片| 久久这里只有精品免费 | 国产欧美视频一区二区三区 | 亚洲一区中文字幕 | 天天插天天干 | 国产香蕉视频在线播放 | 久久精品国产一区 | 久久嗨| 国产精品久久久久久久午夜片 | 成人精品影院 | 国产中文字幕一区 | 成人精品视频免费在线观看 | 欧美精品久久久 | 日韩av在线中文字幕 | 国产精品久久久久无码av | 精品久久久久久久久久 | 四季久久免费一区二区三区四区 | 日韩av一区二区在线观看 | 亚洲一区中文字幕 |