首页 > 编程语言 >spring bean 的属性为 java.util.Properties 时如何初始化该属性

spring bean 的属性为 java.util.Properties 时如何初始化该属性

时间:2023-07-13 18:13:53浏览次数:36  
标签:4.1 spring jar springframework RELEASE java 属性

 

 

 

public class FooBean {

    private java.util.Properties attr;

    public java.util.Properties getAttr() {
        return attr;
    }

    public void setAttr(java.util.Properties attr) {
        this.attr = attr;
    }
    
}

 

    <bean id="fooBean" class="cn.zno.foo.FooBean">
        <property name="attr">
            <props>
                <prop key="a">some string</prop>
                <prop key="b">${user.home}\somedir</prop>
            </props>
        </property>
    </bean>

 

等价于:

        FooBean fooBean = new FooBean();
        fooBean.setAttr(new Properties());
        
        fooBean.getAttr().setProperty("a", "some string");
        fooBean.getAttr().setProperty("b", System.getProperty("user.home") + "\\somedir");
        
//      fooBean.getAttr().put(null,null);// 用put 添加对象

目前未发现有何种方法可以在 spring bean configuratoin 中实现 put 方法。java configuration 中可以

 

目前只发现a,b 两种用法。

Element : prop
The string value of the property. Note that whitespace is trimmed off to avoid unwanted
whitespace caused by typical XML formatting.

 

当${...} 指定的属性不存在时:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'user.home1' in string value "${user.home1}\somedir"
	at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) ~[spring-core-4.1.9.RELEASE.jar:4.1.9.RELEASE]
	at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-4.1.9.RELEASE.jar:4.1.9.RELEASE]
	at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver.resolveStringValue(PropertyPlaceholderConfigurer.java:258) ~[spring-beans-4.1.9.RELEASE.jar:4.1.9.RELEASE]
	at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:282) ~[spring-beans-4.1.9.RELEASE.jar:4.1.9.RELEASE]
	at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:204) ~[spring-beans-4.1.9.RELEASE.jar:4.1.9.RELEASE]
	at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitMap(BeanDefinitionVisitor.java:262) ~[spring-beans-4.1.9.RELEASE.jar:4.1.9.RELEASE]
	at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:198) ~[spring-beans-4.1.9.RELEASE.jar:4.1.9.RELEASE]
	at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitPropertyValues(BeanDefinitionVisitor.java:141) ~[spring-beans-4.1.9.RELEASE.jar:4.1.9.RELEASE]
	at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:82) ~[spring-beans-4.1.9.RELEASE.jar:4.1.9.RELEASE]
	at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:208) ~[spring-beans-4.1.9.RELEASE.jar:4.1.9.RELEASE]
	at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:222) ~[spring-beans-4.1.9.RELEASE.jar:4.1.9.RELEASE]
	at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:86) ~[spring-beans-4.1.9.RELEASE.jar:4.1.9.RELEASE]
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:265) ~[spring-context-4.1.9.RELEASE.jar:4.1.9.RELEASE]
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:162) ~[spring-context-4.1.9.RELEASE.jar:4.1.9.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:610) ~[spring-context-4.1.9.RELEASE.jar:4.1.9.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462) ~[spring-context-4.1.9.RELEASE.jar:4.1.9.RELEASE]
	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:125) ~[spring-test-4.1.9.RELEASE.jar:4.1.9.RELEASE]
	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60) ~[spring-test-4.1.9.RELEASE.jar:4.1.9.RELEASE]
	at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:109) ~[spring-test-4.1.9.RELEASE.jar:4.1.9.RELEASE]
	at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:261) ~[spring-test-4.1.9.RELEASE.jar:4.1.9.RELEASE]
	at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:68) ~[spring-test-4.1.9.RELEASE.jar:4.1.9.RELEASE]
	at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:86) ~[spring-test-4.1.9.RELEASE.jar:4.1.9.RELEASE]
	... 25 more

 

标签:4.1,spring,jar,springframework,RELEASE,java,属性
From: https://www.cnblogs.com/zno2/p/10333814.html

相关文章

  • SpEL (Spring Expression Language)
    https://docs.spring.io/spring-framework/docs/3.0.x/reference/expressions.html 6.1 IntroductionTheSpringExpressionLanguage(SpELforshort)isapowerfulexpressionlanguagethatsupportsqueryingandmanipulatinganobjectgraphatruntime.T......
  • @ConfigurationProperties 前缀注入属性
     importjava.util.LinkedHashMap;importjava.util.Map;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.context.annotation.Configuration;@ConfigurationProperties(prefix="zoo")@Configura......
  • spring 静态变量方式加载properties 文件(支持profile)
     foo-test.properties(测试环境)foo-pro.properties(生产环境)需要根据spring.profiles.active切换 importjava.io.IOException;importjava.util.Properties;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.facto......
  • Java如何将数组转换为集合?
    在Java中,可以使用`Arrays`类的`asList()`方法将数组转换为集合。该方法接受一个数组作为参数,并返回一个包含数组元素的固定大小的列表。以下是将数组转换为集合的示例:String[]array={"item1","item2","item3"};List<String>list=Arrays.asList(array);在上述示例中,......
  • Spring及IOC
    Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器(框架) IoC容器控制反转IoC(InversionofControl),是一种设计思想,DI(依赖注入)是实现IoC的一种方法没有IoC的程序中,我们使用面向对象编程,对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控......
  • JavaScript 中获取数组最后一个元素3种方法及性能
    当需要从JavaScript中的数组中获取最后一个元素时,有多种选择,本文将提供3种可用方法。1.数组length属性length属性返回数组中元素的数量。从数组的长度中减去1得到数组最后一个元素的索引,使用它可以访问最后一个元素。从长度中减去1的原因是,在JavaScript中,数组索引......
  • ble开发 蓝牙服务中对服务、特性、属性以及UUID的理解
    1.蓝牙服务包括多个服务(service),如下为蓝牙串口通信例程的三个服务 2.其中每个服务包括多个特性(Characteristic),如下为GenericAccess服务的四个特性 3.属性即为每个特性是否可读写 4.每个特性都有其对应的UUID简单理解UUID就是编号,服务和特性都有各自的UUID他们都是唯一......
  • SpringMVC入门案例
                ......
  • spring纯注解开发模式
    1、IOC的注解:1.1@Component【重点】:相当于<bean>标签:把对象交给spring管理,spring会帮助我们创建对象。@controller,@Service,@Repository与@Component的作用完全一致,但更加详细化。@Controller:用于web层的bean;@Service:用于service层的bean;@Repository:用于dao层的bean;1.2其他......
  • spring cloud Eureka 注册中心
    SpringCloud是一组框架和工具集,用于快速构建分布式系统,为微服务架构提供了全套技术支持。其中,注册中心是SpringCloud微服务架构中的一个重要组件,它提供了服务注册和发现的功能,是构建分布式系统的基础。本文将介绍SpringCloud中的Eureka注册中心,并给出相应的示例说明。Eureka注......