Spring 注解方式引入配置文件

配置文件,我以两种为例,一种是引入Spring的XML文件,另外一种是.properties的键值对文件;

一。引入Spring XML的注解是@ImportResource

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface ImportResource

@ImportResource有三个属性,value、locations、reader,准确来说是两个,locations和value属性是一样的,都是接受字符串数组,代表导入文件的位置;reader是代表Spring解析文件的类,Spring支持XML或者groovy方式的文件解析,默认的XML解析的reader对象为XmlBeanDefinitionReader的doLoadBeanDefinitions方法;

用法就像下面这样:当然如果不是Spring注解的容器AnnotationConfigApplicationContext,同样可以使用@ImportResource注解,只要开启了注解扫描,并且标注的类已经被Spring管理到了(比如标注了注解@Component等  或者 XML中定义过该bean)

@ImportResource(locations= {"com/lvbinbin/pack1/spring.xml"})
public class AppConfig {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
        String[] names = ac.getBeanDefinitionNames();
        for (String string : names) {
            System.out.println(string+" , "+ac.getBean(string));
        }
    }
}

简而言之,使用@ImportResource生效两个条件,一是开启包扫描 ,当然别的配置也有这种功能,只要满足注册了ConfigurationClassPostProcessor 这个BPP对象,如果不知道有没有这个bean,用上面的方式查看容器中所有的bean有没有这个bean就能知道;

    二是@ImportResource标注的类已经是个bean,被Spring管理到了,上面输出所有bean的方式就可以查看;

二。引入properties文件的注解有一种是 @PropertySource

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(PropertySources.class)
public @interface PropertySource

先来介绍@PropertySource的用法

jdbc.properties

jdbcDriver=com.mysql.jdbc.Driver
userName=root
userName_zh=吕彬彬
userName_en=lvbinbin

配置类  当然这个注解是标注在类上的,但是不一定要只能标注在@Configuration注解的类上,只要被Spring管理到的Bean都是可以标注的,然后使用@Value("${}")的方式注入的; 只要PropertySource的文件引入一次,该Spring容器中bean都可以使用@Value来设置属性;

@Configuration
@PropertySource(value= {"pack5/jdbc.properties"})
public class ConfigA {

        @Value("${jdbcDriver}")
        private String jdbcName;
        
        @Value("${userName_zh}")
        private String userName;
        
        public static void main(String[] args) {
            AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ConfigA.class);
            System.out.println(ac.getBean(ConfigA.class).jdbcName);
            System.out.println(ac.getBean(ConfigA.class).userName);
            MutablePropertySources mps = ac.getEnvironment().getPropertySources();
            mps.forEach(ps->{System.out.println(ps.getName());});   
        }
}

查看结果:   也验证了这种方式可以注入属性;  下面三行输出就是environment对象中的propertySources集合里已经有的propertySource; 其中最后一行就是这个jdbc.propertySource的name,这是Spring默认生成规则生成的;如果不满意,可以按照下面的方式来生成自定义;

 修改注解@PropertySource里的name属性:

@PropertySource(value= {"pack5/jdbc.properties"},name="mysqlJdbcProperties")

 测试结果就显示成为这样:

 这样@PropertySource的属性说明: 1. name就是该配置文件对应的properySource的name值,你要说用处嘛? 可以通过如下方式指定propertySource的name值来获取配置文件中的键值对;

String res = (String) ac.getEnvironment().getPropertySources().get("mysqlJdbcProperties").getProperty("userName_zh");

                2. value就是配置文件的位置,字符串数组,支持Spring的多种资源写法,classpath、file等;

                3. ignoreResourceNotFound 属性  默认是false,当配置文件不存在的时候,就会抛出异常;true代表没有该文件也没有问题;建议默认即可,这样缺少配置文件时候能够发现问题所在;因为${}这种解析方式 比如键 值不存在,会原样的字符串返回;

                4. encoding 属性就是编码,默认是UTF-8;具体使用效果我也看不出来,如果引入properties文件乱码了,不妨试试修改该属性;

此外,SpringEl表达式也是可以注入属性的,简单介绍下使用方式:其中 environment 是Spring容器中的环境对象的名字,后面直接跟需要的属性,就会遍历PropertySources对象去寻找;

@Value("#{environment['userName_en']}")
private String userNameEn;

 查看输出

lvbinbin

 查看Spring日志,证明了这一点;不过我觉得还是 ${}更容易理解一点

原文地址:https://www.cnblogs.com/lvbinbin2yujie/p/10291480.html