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

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

@Value初使用小記

2019-11-10 19:37:48
字體:
供稿:網(wǎng)友
1、@Value是SPRing內(nèi)部用來讀取properties配置文件內(nèi)容的注解2、詳細(xì)使用過程1)創(chuàng)建.properties文件config.properties
test.name=${name}userPageSize=5test.abc=abc2)配置文件中將.properties文件引入i.引入context的Schema命名空間,配置文件中應(yīng)包含<context:annocation-config />標(biāo)簽用于使系統(tǒng)可以識別各種注解,但由于自動掃描compoent標(biāo)簽中已經(jīng)自動注入了以上功能,所以只要<context:component-scan/>標(biāo)簽即可實(shí)現(xiàn),無需再添加。命名空間引入:
<?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、配置文件的引入多個(gè)文件同時(shí)引入:
<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> 單個(gè)文件引入:
<bean id="propertyConfigurer"         	 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">          <property name="location" value="classpath:jdbc.properties" />      </bean>注意:在使用springmvc時(shí),實(shí)際上是兩個(gè)Spring容器,dispatcher-servlet.xml是一個(gè),我們的cotroller在這里;applicationContext.xml是另外一個(gè),數(shù)據(jù)源以及數(shù)據(jù)庫配置在這里。在service中可以拿到@Value值是因?yàn)橥ǔN覀儗@取屬性文件引入在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;注意:實(shí)體類上必須有注解標(biāo)注@Component可以泛指各種組件,才可生效————————————————————————————————————————————————讀取pom文件配置參數(shù):1、pom參數(shù)配置:
<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中不識別&要用轉(zhuǎn)譯符號&	  			<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>
<!-- 指定的動態(tài)配置文件目錄 -->	 <resources>             <resource>                 <directory>src/main/resources</directory>                 <includes>                     <include>**/*.properties</include>                 </includes>                 <filtering>true</filtering>             </resource>     </resources>  </build>2、properties參數(shù)配置
driver=com.mysql.jdbc.Driverurl=${db_url}username=${db_username}password=${db_password}3、maven clean,編譯運(yùn)行——————————————————————————————————————1、maven加載時(shí)候的文件可以查看properties文件是否已經(jīng)加載參數(shù),注意清clean緩存 這是文件路徑:2、在maven的pom.xml文件中,<properties>用于定義全局變量,在pom中通過${property_name}的形式引用變量的值。pom 的全局變量可以分為以下幾種:a、系統(tǒng)shell的環(huán)境變量env.property_name,如${env.PATH}表示當(dāng)前引用該系統(tǒng)的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>中已經(jīng)定義的變量值,如${myVar}表示引用<properties><myVar>myvalue</myVar></properties>中的myvalue3、使用Maven編譯項(xiàng)目遇到——“maven編碼gbk的不可映射字符”解決辦法:
<!-- 指明編譯源代碼時(shí)使用的字符編碼,maven編譯的時(shí)候默認(rèn)使用的GBK編碼, 通過project.build.sourceEncoding屬性設(shè)置字符編碼,告訴maven這個(gè)項(xiàng)目使用UTF-8來編譯 -->      <properties>           <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>       </properties> 
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 报价| 沛县| 赣榆县| 广河县| 英超| 仙居县| 绥芬河市| 孝感市| 上杭县| 池州市| 伊宁市| 定远县| 若尔盖县| 巫溪县| 靖边县| 寿宁县| 兴城市| 利辛县| 长阳| 仙居县| 綦江县| 新乡县| 兴和县| 尖扎县| 柳江县| 靖州| 山西省| 瑞安市| 名山县| 湟源县| 涞水县| 姜堰市| 武宣县| 大邑县| 五华县| 东港市| 来凤县| 乐东| 浦城县| 靖西县| 泸水县|