国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

@Value初使用小記

2019-11-10 19:17:57
字體:
來源:轉載
供稿:網友
1、@Value是SPRing內部用來讀取properties配置文件內容的注解2、詳細使用過程1)創建.properties文件config.properties
test.name=${name}userPageSize=5test.abc=abc2)配置文件中將.properties文件引入i.引入context的Schema命名空間,配置文件中應包含<context:annocation-config />標簽用于使系統可以識別各種注解,但由于自動掃描compoent標簽中已經自動注入了以上功能,所以只要<context:component-scan/>標簽即可實現,無需再添加。命名空間引入:
<?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:p="http://www.springframework.org/schema/p"	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" 	xmlns:task="http://www.springframework.org/schema/task" 	xmlns:util="http://www.springframework.org/schema/util"	xsi:schemaLocation="		http://www.springframework.org/schema/beans		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd		http://www.springframework.org/schema/util   		http://www.springframework.org/schema/util/spring-util-3.2.xsd		http://www.springframework.org/schema/context		http://www.springframework.org/schema/context/spring-context-3.2.xsd		http://www.springframework.org/schema/mvc		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd		http://www.springframework.org/schema/tx        	http://www.springframework.org/schema/tx/spring-tx-3.2.xsd		http://www.springframework.org/schema/task  		http://www.springframework.org/schema/task/spring-task-3.2.xsd		http://www.springframework.org/schema/aop  		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">ii、配置文件的引入多個文件同時引入:
<bean id="propertyConfig"         	 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">         <property name="locations">                 <list>                    <value>classpath:jdbc.properties</value>                     <value>classpath:config.properties</value>                 </list>            </property>        </bean> 單個文件引入:
<bean id="propertyConfigurer"         	 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">          <property name="location" value="classpath:jdbc.properties" />      </bean>注意:在使用springmvc時,實際上是兩個Spring容器,dispatcher-servlet.xml是一個,我們的cotroller在這里;applicationContext.xml是另外一個,數據源以及數據庫配置在這里。在service中可以拿到@Value值是因為通常我們將獲取屬性文件引入在applicationContext.xml中,這樣在Controller中是取不到的,故必須在dispatcher-servlet.xml中把屬性文件再定義一下。3)使用@Value注解i: $用法
@Controller@RequestMapping("/user")public class UserController {	@Resource(name = "userServiceImpl")	private UserServiceImpl userService;		@Resource(name="userTest")	private UserTest userTest;		@Value("${test.abc}")	private String testabc;	@Value("${userPageSize}")	private String userPageSize;ii:#的用法
@Component	public class User {	private static final long serialVersionUID = 952204796252534860L;	private int id;	private String name;	private String passWord;	private String nickname = null;	private Date time = null;	private String action = null;	private String token;//生成的訪問憑證token	private int status;   //1 有效 0 測試用戶  -1刪除//	private List<Role> roles = new ArrayList<Role>();	@Value("${userPageSize}")	private String userPageSize;			public String getUserPageSize() {		return userPageSize;	}	public void setUserPageSize(String userPageSize) {		this.userPageSize = userPageSize;	}
@Controller	//@PropertySource("classpath:config.properties")	@RequestMapping("/user")	public class UserController {	@Resource(name = "userServiceImpl")	private UserServiceImpl userService;		@Resource(name="userTest")	private UserTest userTest;		@Value("${test.abc}")	private String testabc;	@Value("#{user.userPageSize}")	private String testabc2;		@Value("${userPageSize}")	private String userPageSize;注意:實體類上必須有注解標注@Component可以泛指各種組件,才可生效————————————————————————————————————————————————讀取pom文件配置參數:1、pom參數配置:
<profiles>  	<profile>  		<id>local</id>	  		<properties>	  			<name>xinrui</name>	  			<db_url>jdbc:MySQL://220.181.29.165:3306/video?useUnicode=true&characterEncoding=UTF8&autoReconnect=true</db_url>//注意xml中不識別&要用轉譯符號&	  			<db_username>123</db_username>	  			<db_password>123</db_password>				<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>	  		</properties>  	</profile>  </profiles><build>    <finalName>storm</finalName>
<!-- 指定的動態配置文件目錄 -->	 <resources>             <resource>                 <directory>src/main/resources</directory>                 <includes>                     <include>**/*.properties</include>                 </includes>                 <filtering>true</filtering>             </resource>     </resources>  </build>2、properties參數配置
driver=com.mysql.jdbc.Driverurl=${db_url}username=${db_username}password=${db_password}3、maven clean,編譯運行——————————————————————————————————————1、maven加載時候的文件可以查看properties文件是否已經加載參數,注意清clean緩存 這是文件路徑:2、在maven的pom.xml文件中,<properties>用于定義全局變量,在pom中通過${property_name}的形式引用變量的值。pom 的全局變量可以分為以下幾種:a、系統shell的環境變量env.property_name,如${env.PATH}表示當前引用該系統的PATH變量值,PATH必須大寫b、java System Properties即Java屬性文件,如${java.home}c、project.property_name,直接引用POM中的元素值,如${project.version}表示引用<propject><version>1.0</version></project>中的1.0d、settings.property_name,直接引用setting.xml中的元素值,如${settings.offline}表示引用<setting><offline>false</offline></setting>中的falsee、property_name,直接訪問<properties>中已經定義的變量值,如${myVar}表示引用<properties><myVar>myvalue</myVar></properties>中的myvalue3、使用Maven編譯項目遇到——“maven編碼gbk的不可映射字符”解決辦法:
<!-- 指明編譯源代碼時使用的字符編碼,maven編譯的時候默認使用的GBK編碼, 通過project.build.sourceEncoding屬性設置字符編碼,告訴maven這個項目使用UTF-8來編譯 -->      <properties>           <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>       </properties> 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 上虞市| 孟村| 柯坪县| 西青区| 凉山| 翁牛特旗| 永清县| 屯门区| 阜平县| 大埔县| 安龙县| 改则县| 永和县| 镇原县| 三门县| 海安县| 醴陵市| 安达市| 射阳县| 蓝山县| 辉南县| 曲松县| 罗山县| 富平县| 辽宁省| 绥芬河市| 雅安市| 罗城| 古浪县| 行唐县| 黄梅县| 商水县| 兴城市| 镇巴县| 澜沧| 瑞金市| 新宁县| 榕江县| 宁强县| 宁强县| 东源县|