1. 新建員工類,他有電話號碼和出生日期等信息
package com.chapter3.getterdi; import java.util.Date; public class Employee { int id; String name; PhoneNumber phoneNumber; Date birthDate; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public PhoneNumber getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(PhoneNumber phoneNumber) { this.phoneNumber = phoneNumber; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } }
2. 新建PhoneNumber類
package com.chapter3.getterdi; public class PhoneNumber { String areaNumber; String telNumber; @Override public String toString() { return areaNumber + "-" + telNumber; } public String getAreaNumber() { return areaNumber; } public void setAreaNumber(String areaNumber) { this.areaNumber = areaNumber; } public String getTelNumber() { return telNumber; } public void setTelNumber(String telNumber) { this.telNumber = telNumber; } }
3. 新建屬性編輯器
package com.chapter3.getterdi; import java.beans.PropertyEditorSupport; public class PhoneEditor extends PropertyEditorSupport { @Override public void setAsText(String text) throws IllegalArgumentException { PhoneNumber phoneNumber = new PhoneNumber(); String[] str = text.split("-"); phoneNumber.setAreaNumber(str[0]); phoneNumber.setTelNumber(str[1]); setValue(phoneNumber); } }
4.新建日期的屬性編輯器
package com.chapter3.getterdi; import java.beans.PropertyEditorSupport; import java.text.ParseException; import java.text.SimpleDateFormat; public class DateEditor extends PropertyEditorSupport { String dateFormat; public String getDateFormat() { return dateFormat; } public void setDateFormat(String dateFormat) { this.dateFormat = dateFormat; } @Override public void setAsText(String text) throws IllegalArgumentException { SimpleDateFormat format = new SimpleDateFormat(dateFormat); try { setValue(format.parse(text)); } catch (ParseException e) { e.printStackTrace(); } } }
5. Spring的配置文件
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 自定義屬性編輯器的註冊 --> <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="com.chapter3.getterdi.PhoneNumber"> <bean id="phoneEditor" class="com.chapter3.getterdi.PhoneEditor"/> </entry> <entry key="java.util.Date"> <bean id="dateEditor" class="com.chapter3.getterdi.DateEditor"> <property name="dateFormat" value="yyyy-MM-dd"/> </bean> </entry> </map> </property> </bean> <bean id="employee" class="com.chapter3.getterdi.Employee"> <property name="id" value="1"/> <property name="name" value="張三"/> <property name="phoneNumber" value="025-8630471"/> <property name="birthDate" value="1983-10-10"/> </bean> </beans> <?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 自定義屬性編輯器的註冊 --> <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="com.chapter3.getterdi.PhoneNumber"> <bean id="phoneEditor" class="com.chapter3.getterdi.PhoneEditor"/> </entry> <entry key="java.util.Date"> <bean id="dateEditor" class="com.chapter3.getterdi.DateEditor"> <!-- 對於 所有java.util.Date類型 的變量,都採用dateEditor編輯器來處理 --> <property name="dateFormat" value="yyyy-MM-dd"/> </bean> </entry> </map> </property> </bean> <bean id="employee" class="com.chapter3.getterdi.Employee"> <property name="id" value="1"/> <property name="name" value="張三"/> <property name="phoneNumber" value="025-8630471"/> <property name="birthDate" value="1983-10-10"/> </bean> </beans> <?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 自定義屬性編輯器的註冊 --> <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="com.chapter3.getterdi.PhoneNumber"> <bean id="phoneEditor" class="com.chapter3.getterdi.PhoneEditor"/> </entry> <entry key="java.util.Date"> <bean id="dateEditor" class="com.chapter3.getterdi.DateEditor"> <property name="dateFormat" value="yyyy-MM-dd"/> </bean> </entry> </map> </property> </bean> <bean id="employee" class="com.chapter3.getterdi.Employee"> <property name="id" value="1"/> <property name="name" value="張三"/> <property name="phoneNumber" value="025-8630471"/> <property name="birthDate" value="1983-10-10"/> </bean> </beans> <?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 自定義屬性編輯器的註冊 --> <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="com.chapter3.getterdi.PhoneNumber"> <bean id="phoneEditor" class="com.chapter3.getterdi.PhoneEditor"/> </entry> <entry key="java.util.Date"> <bean id="dateEditor" class="com.chapter3.getterdi.DateEditor"> <property name="dateFormat" value="yyyy-MM-dd"/> </bean> </entry> </map> </property> </bean> <bean id="employee" class="com.chapter3.getterdi.Employee"> <property name="id" value="1"/> <property name="name" value="張三"/> <property name="phoneNumber" value="025-8630471"/> <property name="birthDate" value="1983-10-10"/> </bean> </beans>
6. 測試類
package com.chapter3.getterdi; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import org.apache.commons.beanutils.BeanUtils; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { BeanFactory factory = new ClassPathXmlApplicationContext("springchapter3.xml"); Employee employee = (Employee) factory.getBean("employee"); Field[] fields = employee.getClass().getDeclaredFields(); for (int i = 0; i < fields.length; i++) { try { System.out.println(fields[i].getName() + " : " + BeanUtils.getProperty(employee, fields[i].getName())); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } } }
· 前言 · 在國內,公網服務器與本地服務器的通信一直是個難題,本地服務器因爲IP是動態變化的,公網服務器沒辦法將請求發送給本地服務器。爲了解決這個問題,所以採用WebSocket協議替換Http協議。爲了實現請求等待,使用Gu
本文分享自華爲雲社區《超詳細的Java後臺開發面試題之Spring IOC與AOP》,作者:GaussDB 數據庫。 一、前言 IOC和AOP是Spring中的兩個核心的概念,下面談談對這兩個概念的理解。 二、IOC(Inverse o
這個其實是一個特別高頻的面試題,松哥也一直很想和大家仔細來聊一聊這個話題,網上關於這塊的文章很多,但是我一直覺得要把這個問題講清楚還有點難度,今天我來試一試,看能不能和小夥伴們把這個問題梳理清楚,當然,如果小夥伴們覺得看文章不過癮,松哥也有
有小夥伴在學習 Spring Security 的遇到一個問題: 箭頭所指的位置報紅,也就是 Spring 容器中沒有找到一個類型爲 HttpSecurity 的 Bean。 小夥伴說如果他在配置類上加 @EnableWebSecurit
前兩天和小夥伴聊了 Spring Security+JWT 實現無狀態登錄,然後有小夥伴反饋了一個問題,感覺這是一個我們平時寫代碼容易忽略的問題,寫一篇文章和小夥伴們聊一聊。 一 問題復原 先來說問題吧,在 Spring Security+
如果你脫口而出說添加 @Order 註解或者是實現 Ordered 接口,那麼恭喜,你掉坑了。 一 @Order 註解和 Ordered 接口 在 Spring 框架中,@Order 是一個非常實用的元註解,它位於 spring-core
本文分享自華爲雲社區《10分鐘搞懂各種內存溢出案例!!(含完整源碼,建議收藏)》,作者:冰 河。 作爲程序員,多多少少都會遇到一些內存溢出的場景,如果你還沒遇到,說明你工作的年限可能比較短,或者你根本就是個假程序員!哈哈,開個玩笑。今天,我
一、配置入口 Logback支持XML、Groovy的配置方式,以XML來說,它會默認查找resources目錄下的logback-test.xml(用於測試)/logback.xml文件。 而如果你使用的Spring Boot,那麼你還可
本教程將引導您向現有的Web應用程序添加Spring安全策略,在本教程中您將學習如何: 搭建Spring安全策略 部署應用程序 MyEclipse v2023.1.2離線版下載 搭建Spring安全策略 1. 創建一個 scaf
文|苟振東(花名:盛知) Koupleless 項目 committer 螞蟻集團技術專家 本文 5789 字 閱讀 12 分鐘 本篇文章屬於 Koupleless 進階系列文章第二篇,默認讀者對 Koupleless 的基礎概念、能力都
1. 引言 在現代軟件開發過程中,性能優化和故障排查是保證應用穩定運行的關鍵任務之一。Java作爲一種廣泛使用的編程語言,其生態中湧現出了許多優秀的監控和診斷工具,諸如:SkyWalking、Zipkin等,它們幫助開發者和運維人員
有小夥伴們問到這個問題*簡單寫篇文章和大夥聊一下。 一 SecurityFilterChain 首先大夥都知道Spring Security 裏邊的一堆功能都是通過 Filter 來實現的無論是認證、RememberMe Login、會話管
在windows環境,springboot 處理提交的json數據報錯“com.fasterxml.jackson.databind.JsonMappingException: Invalid UTF-8 start byte 0xb1”。