Spring入门(基于Java的容器注解之@ImportResource和@Value)

如何使用@ImportResource和@Value进行资源文件读取。

首先看个例子,使用beans来定义一个配置

<beans>
    <context:annotation-config/>
    <context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>

    <bean class="com.acme.AppConfig"/>

    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
</beans>

这个使用property-placeholder,在使用它的时候,它会对应一个资源文件,对应一个location,location对应一个资源文件的存放位置。<context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>这个这句话的作用是加载properties资源文件。properties文件是一种key、value的形式的文件。
加载了这个文件之后 ,在当前的文件中,可以通过 ${} 的方式,{}里边是properties文件中的key,通过这种方式来引用properties文件中的内容。比如说常用的数据库的配置方式。
我们的配置信息通常会写在资源文件中,然后通过property-placeholder这种方式去把它加载进来。然后在当前的配置文件中去引用它。比如说这里指定了一种数据源DriverManagerDataSource,然后可以指定这种数据源的url、username和password,它们的来源就是这个资源文件。

以上是使用xml文件配置的方式,那么如果使用注解要怎么做?

@Configuration
@ImportResource("classpath:/com/acme/properties-config.xml")
public class AppConfig{

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.username}")
    private String username;

    @Value("${jdbc.password}")
    private String password;

    @Bean
    public DataSource dataSource() {
        return new DriverManagerDataSource(url,username,password);
    }
}

仍然使用@Configuration,把这个类作为一个配置来使用。@ImportResource就是引入一个这种资源,然后资源对应一个xml文件,和前个例子差不多,xml文件也会对应一个property-placeholder。
用@Value这个注解从资源文件中取出它的key赋值给成员变量,包括username、password等。然后再使用@Bean这个注解去创建DriverManagerDataSource的一个对象,也就是和第一种的方式一样,去创建这个Bean的对象,同时把url、username、password传入DriverManagerDataSource的构造器。
这样就达到了从资源文件中去加载资源文件的配置,并应用到bean的创建中。

加载资源文件的例子
创建一个类MyDriverManager,其构造器有三个参数:

public class MyDriverManager {

    public MyDriverManager(String url, String userName, String password) {
        System.out.println("url : " + url);
        System.out.println("userName: " + userName);
        System.out.println("password: " + password);
    }
}

在前一个例子中的StoreConfig类中加上@ImportResource(“classpath:config.xml”)。

@Configuration
@ImportResource("classpath:config.xml")
public class StoreConfig {
    //...
}

然后看一下这个config.xml文件。

<?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: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.xsd" >

    <context:property-placeholder location="classpath:/config.properties"/>

</beans>

里边使用property-placeholder去指定一个资源文件的location,然后看一下这个资源文件的内容。
这里写图片描述

之后再看看在StoreConfig类中要如何配置它

@Value("${url}")
private String url;

@Value("${jdbc.username}")
private String username;

@Value("${password}")
private String password;

@Bean
public MyDriverManager myDriverManager() {
    return new MyDriverManager(url, username, password);
}

要注意username那里不可以直接写username,这样会取到当前用户的,通常会写为jdbc.username。这个类中首先声明成员变量,然后用@Value注解来得到。之后创建一个Bean:MyDriverManager,然后返回一个MyDriverManager的对象,给构造器传入参数。

单元测试类的写法如下:

@Test
public void testMyDriverManager() {
    MyDriverManager manager = super.getBean("myDriverManager");
    System.out.println(manager.getClass().getName());
}

大体的写法就是这样。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值