zoukankan      html  css  js  c++  java
  • spring读取prperties配置文件(2)

    接上篇,spring读取prperties配置文件(1),这一篇主要讲述spring如何用annotation的方式去读取自定义的配置文件。

    这里我先定义好属性文件"user.properties"。

    user.name=bingyulei
    user.description=没有什么好描述的

    然后再spring的配置文件中applicationContext.xml加入如下配置,这里我配置文件就放到classpath下,可以运行junit测试

    <?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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        <!-- 开启注释配置 -->
        <context:annotation-config />
        <!-- 自动扫描 包 -->
        <context:component-scan base-package="com.bing">
            <context:exclude-filter type="annotation"
                expression="org.springframework.stereotype.Controller" />
            <!-- 不用扫描的目录 -->
            <context:exclude-filter type="regex" expression="com.bing.vo.*" />
            <context:exclude-filter type="regex" expression="com.bing.util.*" />
        </context:component-scan>
        <!-- 自动读取属性文件的配置 -->
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:/user.properties</value>
                </list>
            </property>
            <property name="fileEncoding" value="utf-8" />
        </bean>
    </beans>

    创建要注入读取配置文件的类:这里我用”@Component“注释,和“@Controller”、"@Service"、"@Repository"意思差不多一样。

      @Component;@Controller;@Service;@Repository
          在annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个spring容器管理的类。即就是该类已经拉入到 spring的管理中了。

          而@Controller, @Service, @Repository是@Component的细化,这三个注解比@Component带有更多的语义,它们分别对应了控制层、服务层、持久层的类。

     1 package com.bing.test;
     2 
     3 import org.springframework.beans.factory.annotation.Value;
     4 import org.springframework.stereotype.Component;
     5 
     6 @Component("manager")
     7 public class Manager {
     8     @Value("${user.name}")
     9     private String myName;
    10     @Value("${user.description}")
    11     private String description;
    12 
    13     public void sayHello() {
    14         System.out.println("Hello " + myName);
    15     }
    16 
    17     public void getDes() {
    18         System.out.println(description);
    19 
    20     }
    21 }

    然后用junit测试一下:

     1 package com.bing.jtest;
     2 
     3 import javax.annotation.Resource;
     4 
     5 import org.junit.Test;
     6 import org.junit.runner.RunWith;
     7 import org.springframework.test.context.ContextConfiguration;
     8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     9 
    10 import com.bing.test.Manager;
    11 
    12 @ContextConfiguration(locations = { "classpath:applicationContext.xml" })
    13 @RunWith(SpringJUnit4ClassRunner.class)
    14 public class Testr {
    15     
    16     @Resource(name="manager")
    17     private Manager manager;
    18    
    19     @Test
    20     public void test() {
    21         manager.sayHello();
    22         manager.getDes();
    23     }
    24 }

    运行结果:

    Hello bingyulei
    没有什么好描述的

    另外:在jsp中如果想得到对应的config值,可以直接用下面标签,只需在界面中加入这个tab声明就可以了<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>

    <spring:message code="url.portal.help"/>

    欢迎交流学习:http://www.cnblogs.com/shizhongtao/p/3469323.html

    转载请注明出处

  • 相关阅读:
    开始熟悉一下数据结构了
    首个概率dp
    十六进制转化为八进制
    蓝桥之 剪格子
    蓝桥之 颠倒的价格
    还是闲的
    也是闲的
    闲的无聊,,,
    函数进阶
    函数基础
  • 原文地址:https://www.cnblogs.com/shizhongtao/p/3469323.html
Copyright © 2011-2022 走看看