Spring整理3 -- 自定义属性编辑器

在我们注入属性时,遇到是日期类型,如果按普通属性去注入,则会报错,那我们该怎么解决?解决办法:自定义属性编辑器。

什么是属性编辑器,作用?

自定义属性编辑器,spring配置文件中的字符串转换成相应的对象进行注入spring已经有内置的属性编辑器,我们可以根据需求自己定义属性编辑器。

步骤:

1、  定义一个属性编辑器必须继承java.beans.PropertyEditorSupport

2、  在配置文件配置上我们定义的属性编辑器

下面我们来做一个java.util.Date属性编辑器,代码如下:

定义一个属性编辑器UtilDatePropertyEditor:

/**

 * java.util.Date属性编辑器 

 */

public class UtilDatePropertyEditor extends PropertyEditorSupport {

    private String format="yyyy-MM-dd";    

    @Override

    public void setAsText(String text) 

throws IllegalArgumentException {

       SimpleDateFormat sdf = new SimpleDateFormat(format);

       try {

           Date d = sdf.parse(text);

           this.setValue(d);

       } catch (ParseException e) {

           e.printStackTrace();

       }

    }

 

    public void setFormat(String format) {

       this.format = format;

    }

}

 配置文件applicationContext.xml

   

  <!-- 定义属性编辑器 -->      

    <bean id="customEditorConfigurer" 

class="org.springframework.beans.factory.config.CustomEditorConfigurer">

       <property name="customEditors">

           <map>

              <entry key="java.util.Date">

                  <bean class="spring.UtilDatePropertyEditor">

                     <property name="format" value="yyyy-MM-dd"/>

                  </bean>

              </entry>

           </map>

       </property>

    </bean>

  

以后我们就可以为java.util.Date进行注入,和普通属性一样使用,测试代码(略)

要获取spring-boot-admin-starter-client自定义属性,可以按照以下步骤进行操作: 1. 首先,确保你的项目中已经添加了spring-boot-admin-starter-client包,并成功进行了打包。 2. 在项目的配置文件(比如application.properties或application.yml)中,可以使用以下格式设置spring-boot-admin-starter-client的自定义属性: ``` spring.boot.admin.client.custom-property=value ``` 其中,custom-property是你自定义属性名,value是对应的属性值。 3. 可以通过在spring-boot-admin-starter-server项目的代码中使用@ConfigurationProperties注解来获取spring-boot-admin-starter-client的自定义属性。例如: ```java @ConfigurationProperties(prefix = "spring.boot.admin.client") public class MyConfiguration { private String customProperty; // getter and setter // other configurations } ``` 在这个配置类中,使用prefix属性指定了属性的前缀为"spring.boot.admin.client",这样就能获取到spring-boot-admin-starter-client的自定义属性。 4. 运行spring-boot-admin-starter-server项目时,就可以通过MyConfiguration类获取spring-boot-admin-starter-client的自定义属性了。 综上所述,你可以通过在项目的配置文件中设置属性,并使用@ConfigurationProperties注解获取这些自定义属性来获取spring-boot-admin-starter-client的自定义属性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [spring-boot-admin-client-2.6.2-API文档-中文版.zip](https://download.csdn.net/download/qq_36462452/85294581)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [spring-boot-admin-starter-client与spring-boot版本不匹配的坑](https://blog.csdn.net/mahui_1980/article/details/117528352)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值