package com.zznode.bean; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.beanutils.ConvertUtils; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.XPath; import org.dom4j.io.SAXReader; public class ClassPathXMlApplicationContext { private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>(); private Map<String, Object> sigletons = new HashMap<String, Object>(); public ClassPathXMlApplicationContext(String filename) { this.readXMl(filename); this.instanceBeans(); this.injectObject(); } /** * 爲Bean對象的屬性注入值 */ private void injectObject() { for (BeanDefinition beanDefinition : beanDefines) { Object bean = sigletons.get(beanDefinition.getId()); if (bean != null) { try { PropertyDescriptor[] ps = Introspector.getBeanInfo( bean.getClass()).getPropertyDescriptors(); for (PropertyDefinition propertyDefinition : beanDefinition .getPropertys()) { for (PropertyDescriptor propertyDescriptor : ps) { if (propertyDefinition.getName().equals( propertyDescriptor.getName())) { Method setter = propertyDescriptor .getWriteMethod();// 獲取屬性的setter方法 if (setter != null) { Object value = null; if (propertyDefinition.getRef() != null && !"".equals(propertyDefinition .getRef().trim())) { value = sigletons .get(propertyDefinition .getRef()); } else { value = ConvertUtils.convert( propertyDefinition.getValue(), propertyDescriptor .getPropertyType()); } setter.setAccessible(true); setter.invoke(bean, value);// 把引用對象注入到屬性 } break; } } } } catch (Exception e) { e.printStackTrace(); } } } } /** * 實例化Bean */ private void instanceBeans() { try { for (BeanDefinition beanDefinition : beanDefines) { if (beanDefinition.getClassName() != null && !"".equals(beanDefinition.getClassName().trim())) { sigletons.put(beanDefinition.getId(), Class.forName( beanDefinition.getClassName()).newInstance()); } } } catch (Exception e) { e.printStackTrace(); } } /** * 讀取XML配置文件 */ private void readXMl(String filename) { SAXReader reader = new SAXReader(); Document document = null; try { URL xmlpath = this.getClass().getClassLoader() .getResource(filename); document = reader.read(xmlpath); Map<String, String> nsMap = new HashMap<String, String>(); nsMap.put("ns", "http://www.springframework.org/schema/beans");// 加入命名空間 XPath xsub = document.createXPath("//ns:beans/ns:bean");// 創建beans/bean查詢路徑 xsub.setNamespaceURIs(nsMap);// 設置命名空間 List<Element> beans = xsub.selectNodes(document); for (Element element : beans) { String id = element.attributeValue("id"); String clazz = element.attributeValue("class"); BeanDefinition beanDefine = new BeanDefinition(id, clazz); XPath propertysub = document.createXPath("ns:property"); propertysub.setNamespaceURIs(nsMap); List<Element> properties = propertysub.selectNodes(element); for (Element property : properties) { String propertyName = property.attributeValue("name"); String propertyref = property.attributeValue("ref"); String propertyValue = property.attributeValue("value"); PropertyDefinition propertyDefinition = new PropertyDefinition( propertyName, propertyref, propertyValue); beanDefine.getPropertys().add(propertyDefinition); } beanDefines.add(beanDefine); } } catch (Exception e) { e.printStackTrace(); } } /** * 獲得Bean對象 * @param beanName * @return */ public Object getBean(String beanName) { return sigletons.get(beanName); } }
<?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" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy /> <bean id="siteDao" class="com.zznode.services.impl.SiteDao"/> <bean id="siteService" class="com.zznode.services.impl.SiteService" > <property name="siteDao" ref="siteDao"></property> <property name="name" value="arron"></property> <property name="id" value="88"></property> </bean> </beans>
package com.zznode.proxy; import com.zznode.bean.ClassPathXMlApplicationContext; import com.zznode.services.ISiteService; public class Util { public static void main(String[] args) { ClassPathXMlApplicationContext cxt = new ClassPathXMlApplicationContext( "beans.xml"); ISiteService service = (ISiteService) cxt.getBean("siteService"); service.handler("Alex"); } }
public class SiteDao implements ISiteDao{ public void handler(String name) { System.out.println(name+" is doing some things!"); } }
public interface ISiteDao { public void handler(String name); }
public class SiteService implements ISiteService { private ISiteDao siteDao; private Integer id; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public SiteService() { } public ISiteDao getSiteDao() { return siteDao; } public void setSiteDao(ISiteDao siteDao) { this.siteDao = siteDao; } public void handler(String name) { System.out.println("id="+id+",name="+this.getName()); siteDao.handler(name); } }
public interface ISiteService { public void handler(String name); }
本文分享自華爲雲社區《【Spring Boot 源碼學習】RedisAutoConfiguration 詳解》,作者: Huazie。 引言 帶大家分析 Spring Boot 內置的有關 Redis 的自動配置類【RedisAutoCo
在 Spring 框架中,@Transactional 註解作爲一種聲明式事務管理的關鍵機制,其背後的工作原理遠比簡單的 AOP(面向切面編程)和 ThreadLocal 存儲更爲細膩。該註解的實現核心在於 Spring 的 Transac
本文分享自華爲雲社區《超詳細的Java後臺開發面試題之Spring IOC與AOP》,作者:GaussDB 數據庫。 一、前言 IOC和AOP是Spring中的兩個核心的概念,下面談談對這兩個概念的理解。 二、IOC(Inverse o
這個其實是一個特別高頻的面試題,松哥也一直很想和大家仔細來聊一聊這個話題,網上關於這塊的文章很多,但是我一直覺得要把這個問題講清楚還有點難度,今天我來試一試,看能不能和小夥伴們把這個問題梳理清楚,當然,如果小夥伴們覺得看文章不過癮,松哥也有
有小夥伴在學習 Spring Security 的遇到一個問題: 箭頭所指的位置報紅,也就是 Spring 容器中沒有找到一個類型爲 HttpSecurity 的 Bean。 小夥伴說如果他在配置類上加 @EnableWebSecurit
前兩天和小夥伴聊了 Spring Security+JWT 實現無狀態登錄,然後有小夥伴反饋了一個問題,感覺這是一個我們平時寫代碼容易忽略的問題,寫一篇文章和小夥伴們聊一聊。 一 問題復原 先來說問題吧,在 Spring Security+
本教程將引導您使用Spring Annotator實現Spring控制器,標準Java類被添加到搭建項目中,Spring Annotator Spring啓用Java類。 雖然本教程的重點是Spring控制器,但是Spring Annota
本文分享自華爲雲社區《10分鐘搞懂各種內存溢出案例!!(含完整源碼,建議收藏)》,作者:冰 河。 作爲程序員,多多少少都會遇到一些內存溢出的場景,如果你還沒遇到,說明你工作的年限可能比較短,或者你根本就是個假程序員!哈哈,開個玩笑。今天,我
作者:天空小小 爲啥要寫? 從我接觸spring事務開始,就覺得這個東西很神奇,感覺實現很混亂,總是各種新的用法層出不窮。雖然懵懵懂懂的覺
一、配置入口 Logback支持XML、Groovy的配置方式,以XML來說,它會默認查找resources目錄下的logback-test.xml(用於測試)/logback.xml文件。 而如果你使用的Spring Boot,那麼你還可
背景 今天在使用redis做分佈式緩存提供自增id的時候,發現了�� t Goods_key_37的亂碼問題;因爲存的是Long,然後我就自定義了一個RedisTemplate<string,long>的實例來使用 解決過程 1.剛開始說
文|苟振東(花名:盛知) Koupleless 項目 committer 螞蟻集團技術專家 本文 5789 字 閱讀 12 分鐘 本篇文章屬於 Koupleless 進階系列文章第二篇,默認讀者對 Koupleless 的基礎概念、能力都
1. 引言 在現代軟件開發過程中,性能優化和故障排查是保證應用穩定運行的關鍵任務之一。Java作爲一種廣泛使用的編程語言,其生態中湧現出了許多優秀的監控和診斷工具,諸如:SkyWalking、Zipkin等,它們幫助開發者和運維人員