@Value初使用小记

1、@Value是Spring内部用来读取properties配置文件内容的注解
2、详细使用过程
1)创建.properties文件config.properties

test.name=${name}
userPageSize=5
test.abc=abc

2)配置文件中将.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.Driver
url=${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.0
d、settings.property_name,直接引用setting.xml中的元素值,如${settings.offline}表示引用<setting><offline>false</offline></setting>中的false
e、property_name,直接访问<properties>中已经定义的变量值,如${myVar}表示引用<properties><myVar>myvalue</myVar></properties>中的myvalue

<!-- 指明编译源代码时使用的字符编码,maven编译的时候默认使用的GBK编码, 通过project.build.sourceEncoding属性设置字符编码,告诉maven这个项目使用UTF-8来编译 -->  
    <properties>  
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
     </properties> 






  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值