做者:小傅哥
博客:https://bugstack.cnhtml
沉澱、分享、成長,讓本身和他人都能有所收穫!😄
超賣、掉單、冪等,你的程序老是不抗揍!
前端
想一想,運營已經對外宣傳了七八天的活動,滿心歡喜的等着最後一天頁面上線對外了,忽然出現了一堆異常、資損、閃退,而用戶流量稍縱即逝,最後想死的心都有!java
就編程開發來說,丟三落4、亂碼七糟,可能這就是大部分初級程序員平常開發的真實寫照,在即便有測試人員驗證的狀況下,也會出現帶Bug上線的現象,只不過是當時沒有發現而已!由於是人寫代碼,就必定會有錯誤,即便是老碼農程序員
就程序Bug來說,會包括產品PRD流程上的Bug、運營配置活動時候的Bug、研發開發時功能實現的Bug、測試驗證時漏掉流程的Bug、上線過程當中運維服務相關配置的Bug,而這些其實均可以經過制定的流程規範和必定的研發經驗積累,慢慢儘量減小。spring
而另一類是溝通留下的Bug,一般狀況下業務提需求、產品定方案、研發作實現,最終還要有UI、測試、運營、架構等等各個環節的人員參與到一個項目的承接、開發到上線運行,而在這一羣人須要保持一個統一的信息傳播實際上是很難的。好比在項目開發中期,運營給產品說了一個新增的需求,產品以爲功能也不大,隨即找到對應的前端研發加個邏輯,但沒想到可能也影響到了後端的開發和測試的用例。最後功能雖然是上線了,可並不在整個產研測的需求覆蓋度範圍裏,也就隱形的埋下了一個坑。編程
因此,若是你想讓你的程序很抗揍,接的住農夫三拳,那麼你要作的就不僅是一個單純的搬磚碼農!後端
首先咱們回顧下這幾章節都完成了什麼,包括:實現一個容器、定義和註冊Bean、實例化Bean,按照是否包含構造函數實現不一樣的實例化策略,那麼在建立對象實例化這咱們還缺乏什麼?其實還缺乏一個關於類中是否有屬性的問題
,若是有類中包含屬性那麼在實例化的時候就須要把屬性信息填充上,這樣纔是一個完整的對象建立。設計模式
對於屬性的填充不僅是 int、Long、String,還包括尚未實例化的對象屬性,都須要在 Bean 建立時進行填充操做。不過這裏咱們暫時不會考慮 Bean 的循環依賴,不然會把整個功能實現撐大,這樣新人學習時就把握不住了,待後續陸續先把核心功能實現後,再逐步完善架構
鑑於屬性填充是在 Bean 使用 newInstance
或者 Cglib
建立後,開始補全屬性信息,那麼就能夠在類 AbstractAutowireCapableBeanFactory
的 createBean 方法中添加補全屬性方法。這部分你們在實習的過程當中也能夠對照Spring源碼學習,這裏的實現也是Spring的簡化版,後續對照學習會更加易於理解app
AbstractAutowireCapableBeanFactory
的 createBean 方法中添加 applyPropertyValues
操做。small-spring-step-04 └── src ├── main │ └── java │ └── cn.bugstack.springframework.beans │ ├── factory │ │ ├── factory │ │ │ ├── BeanDefinition.java │ │ │ ├── BeanReference.java │ │ │ └── SingletonBeanRegistry.java │ │ ├── support │ │ │ ├── AbstractAutowireCapableBeanFactory.java │ │ │ ├── AbstractBeanFactory.java │ │ │ ├── BeanDefinitionRegistry.java │ │ │ ├── CglibSubclassingInstantiationStrategy.java │ │ │ ├── DefaultListableBeanFactory.java │ │ │ ├── DefaultSingletonBeanRegistry.java │ │ │ ├── InstantiationStrategy.java │ │ │ └── SimpleInstantiationStrategy.java │ │ └── BeanFactory.java │ ├── BeansException.java │ ├── PropertyValue.java │ └── PropertyValues.java └── test └── java └── cn.bugstack.springframework.test ├── bean │ ├── UserDao.java │ └── UserService.java └── ApiTest.java
工程源碼:公衆號「bugstack蟲洞棧」,回覆:Spring 專欄,獲取完整源碼
Spring Bean 容器類關係,如圖 5-2
BeanReference
(類引用)、PropertyValue
(屬性值)、PropertyValues
(屬性集合),分別用於類和其餘類型屬性填充操做。AbstractAutowireCapableBeanFactory
,在 createBean 中補全屬性填充部分。cn.bugstack.springframework.beans.PropertyValue
public class PropertyValue { private final String name; private final Object value; public PropertyValue(String name, Object value) { this.name = name; this.value = value; } // ...get/set }
cn.bugstack.springframework.beans.PropertyValues
public class PropertyValues { private final List<PropertyValue> propertyValueList = new ArrayList<>(); public void addPropertyValue(PropertyValue pv) { this.propertyValueList.add(pv); } public PropertyValue[] getPropertyValues() { return this.propertyValueList.toArray(new PropertyValue[0]); } public PropertyValue getPropertyValue(String propertyName) { for (PropertyValue pv : this.propertyValueList) { if (pv.getName().equals(propertyName)) { return pv; } } return null; } }
cn.bugstack.springframework.beans.factory.config.BeanDefinition
public class BeanDefinition { private Class beanClass; private PropertyValues propertyValues; public BeanDefinition(Class beanClass) { this.beanClass = beanClass; this.propertyValues = new PropertyValues(); } public BeanDefinition(Class beanClass, PropertyValues propertyValues) { this.beanClass = beanClass; this.propertyValues = propertyValues != null ? propertyValues : new PropertyValues(); } // ...get/set }
new BeanDefinition(UserService.class, propertyValues);
cn.bugstack.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory { private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy(); @Override protected Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException { Object bean = null; try { bean = createBeanInstance(beanDefinition, beanName, args); // 給 Bean 填充屬性 applyPropertyValues(beanName, bean, beanDefinition); } catch (Exception e) { throw new BeansException("Instantiation of bean failed", e); } addSingleton(beanName, bean); return bean; } protected Object createBeanInstance(BeanDefinition beanDefinition, String beanName, Object[] args) { Constructor constructorToUse = null; Class<?> beanClass = beanDefinition.getBeanClass(); Constructor<?>[] declaredConstructors = beanClass.getDeclaredConstructors(); for (Constructor ctor : declaredConstructors) { if (null != args && ctor.getParameterTypes().length == args.length) { constructorToUse = ctor; break; } } return getInstantiationStrategy().instantiate(beanDefinition, beanName, constructorToUse, args); } /** * Bean 屬性填充 */ protected void applyPropertyValues(String beanName, Object bean, BeanDefinition beanDefinition) { try { PropertyValues propertyValues = beanDefinition.getPropertyValues(); for (PropertyValue propertyValue : propertyValues.getPropertyValues()) { String name = propertyValue.getName(); Object value = propertyValue.getValue(); if (value instanceof BeanReference) { // A 依賴 B,獲取 B 的實例化 BeanReference beanReference = (BeanReference) value; value = getBean(beanReference.getBeanName()); } // 屬性填充 BeanUtil.setFieldValue(bean, name, value); } } catch (Exception e) { throw new BeansException("Error setting property values:" + beanName); } } public InstantiationStrategy getInstantiationStrategy() { return instantiationStrategy; } public void setInstantiationStrategy(InstantiationStrategy instantiationStrategy) { this.instantiationStrategy = instantiationStrategy; } }
beanDefinition.getPropertyValues()
循環進行屬性填充操做,若是遇到的是 BeanReference,那麼就須要遞歸獲取 Bean 實例,調用 getBean 方法。cn.bugstack.springframework.test.bean.UserDao
public class UserDao { private static Map<String, String> hashMap = new HashMap<>(); static { hashMap.put("10001", "小傅哥"); hashMap.put("10002", "八杯水"); hashMap.put("10003", "阿毛"); } public String queryUserName(String uId) { return hashMap.get(uId); } }
cn.bugstack.springframework.test.bean.UserService
public class UserService { private String uId; private UserDao userDao; public void queryUserInfo() { System.out.println("查詢用戶信息:" + userDao.queryUserName(uId)); } // ...get/set }
@Test public void test_BeanFactory() { // 1.初始化 BeanFactory DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); // 2. UserDao 註冊 beanFactory.registerBeanDefinition("userDao", new BeanDefinition(UserDao.class)); // 3. UserService 設置屬性[uId、userDao] PropertyValues propertyValues = new PropertyValues(); propertyValues.addPropertyValue(new PropertyValue("uId", "10001")); propertyValues.addPropertyValue(new PropertyValue("userDao",new BeanReference("userDao"))); // 4. UserService 注入bean BeanDefinition beanDefinition = new BeanDefinition(UserService.class, propertyValues); beanFactory.registerBeanDefinition("userService", beanDefinition); // 5. UserService 獲取bean UserService userService = (UserService) beanFactory.getBean("userService"); userService.queryUserInfo(); }
beanFactory.registerBeanDefinition("userDao", new BeanDefinition(UserDao.class));
new PropertyValue("uId", "10001")
,另一種是對象屬性 new PropertyValue("userDao",new BeanReference("userDao"))
查詢用戶信息:小傅哥 Process finished with exit code 0
userDao.queryUserName(uId)
那麼咱們在看看Debug調試的狀況下,有沒有進入到實現的 Bean 屬性填充中,以下: