麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

@Value初使用小記

2019-11-10 19:51:07
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
1、@Value是SPRing內(nèi)部用來(lái)讀取properties配置文件內(nèi)容的注解2、詳細(xì)使用過(guò)程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)可以識(shí)別各種注解,但由于自動(dòng)掃描compoent標(biāo)簽中已經(jīng)自動(dòng)注入了以上功能,所以只要<context:component-scan/>標(biāo)簽即可實(shí)現(xiàn),無(wú)需再添加。命名空間引入:
<?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ù)庫(kù)配置在這里。在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;//生成的訪問(wèn)憑證token	private int status;   //1 有效 0 測(cè)試用戶  -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中不識(shí)別&要用轉(zhuǎn)譯符號(hào)&	  			<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>
<!-- 指定的動(dòng)態(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中通過(guò)${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,直接訪問(wèn)<properties>中已經(jīng)定義的變量值,如${myVar}表示引用<properties><myVar>myvalue</myVar></properties>中的myvalue3、使用Maven編譯項(xiàng)目遇到——“maven編碼gbk的不可映射字符”解決辦法:
<!-- 指明編譯源代碼時(shí)使用的字符編碼,maven編譯的時(shí)候默認(rèn)使用的GBK編碼, 通過(guò)project.build.sourceEncoding屬性設(shè)置字符編碼,告訴maven這個(gè)項(xiàng)目使用UTF-8來(lái)編譯 -->      <properties>           <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>       </properties> 
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 亚洲国产精品久久久久久久久 | 国产69久久精品成人看 | 日本欧美一区二区三区在线观看 | 日日草天天干 | 成人在线观看污 | 中文字幕在线视频网站 | 国产精品久久久久久久久久大牛 | 奶子吧naiziba.cc免费午夜片在线观看 | 国产午夜精品一区二区三区不卡 | 久久久久在线观看 | 国产69精品久久久久久久久久 | 精品一区二区三区毛片 | 成人免费乱码大片a毛片视频网站 | 好吊色欧美一区二区三区四区 | 亚洲第一综合 | 九九热精品在线 | 萌白酱福利视频在线网站 | 久草在线精品观看 | 视频一区二区久久 | 羞羞视频免费网站含羞草 | 久久草在线视频免费 | 国产亚洲欧美一区久久久在 | 蜜桃视频在线观看视频 | 性欧美大战久久久久久久免费观看 | 久久99在线 | chinese军人gay呻吟 | 欧美性生活久久久 | 久久久久久久免费看 | 热re91久久精品国产99热 | 欧美日韩在线视频观看 | 精品一区二区久久久久久按摩 | 男人久久天堂 | 九草在线视频 | 精品国产91久久久久久浪潮蜜月 | 国产一区毛片 | 成人店女老板视频在线看 | 国产一区免费在线 | 羞羞视频一区 | 色啪综合| 久久久久久久国产a∨ | 一级在线观看 |