日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring 基于设值函数的依赖注入

發布時間:2023/12/19 javascript 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring 基于设值函数的依赖注入 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

看個例子:

public class TextEditor {private SpellChecker spellChecker;// a setter method to inject the dependency.public void setSpellChecker(SpellChecker spellChecker) {System.out.println("Inside setSpellChecker." );this.spellChecker = spellChecker;}// a getter method to return spellCheckerpublic SpellChecker getSpellChecker() {return spellChecker;}public void spellCheck() {spellChecker.checkSpelling();} }

另一個依賴類文件 SpellChecker.java 的內容:

public class SpellChecker {public SpellChecker(){System.out.println("Inside SpellChecker constructor." );}public void checkSpelling() {System.out.println("Inside checkSpelling." );} }

Beans.xml的內容:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!-- Definition for textEditor bean --><bean id="textEditor" class="com.sap.TextEditor"><property name="spellChecker" ref="spellChecker"/></bean><!-- Definition for spellChecker bean --><bean id="spellChecker" class="com.sap.SpellChecker"></bean></beans>

執行結果:

為什么Spring IOC容器,如此智能地知道調用TextEditor的setSpellChecker方法呢?

利用Java反射機制調用set方法:

SpellChecker的writeMethod是如何解析出來的呢?

此處可以看到,GenericTypeAwarePropertyDescriptor試圖解析Bean類的set方法作為writeMethod:

將setSpellChecker改成SetSpellChecker,就會遇到異常:org.springframework.beans.NotWritablePropertyException: Invalid property ‘spellChecker’ of bean class [com.sap.TextEditor]: Bean property ‘spellChecker’ is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

完整的調用棧:

Jul 24, 2020 4:13:04 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4ee285c6: startup date [Fri Jul 24 16:13:04 CST 2020]; root of context hierarchy Jul 24, 2020 4:13:05 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [Beans.xml] Inside SpellChecker constructor. Jul 24, 2020 4:13:05 PM org.springframework.context.support.ClassPathXmlApplicationContext refresh WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'textEditor' defined in class path resource [Beans.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'spellChecker' of bean class [com.sap.TextEditor]: Bean property 'spellChecker' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'textEditor' defined in class path resource [Beans.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'spellChecker' of bean class [com.sap.TextEditor]: Bean property 'spellChecker' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1650)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1357)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:758)at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)at com.sap.MainApp.main(MainApp.java:11) Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'spellChecker' of bean class [com.sap.TextEditor]: Bean property 'spellChecker' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?at org.springframework.beans.BeanWrapperImpl.createNotWritablePropertyException(BeanWrapperImpl.java:247)at org.springframework.beans.AbstractNestablePropertyAccessor.processLocalProperty(AbstractNestablePropertyAccessor.java:426)at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:278)at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:266)at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:97)at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:77)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1646)... 13 more

要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":

總結

以上是生活随笔為你收集整理的Spring 基于设值函数的依赖注入的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。