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>
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注