Spring自动装配
Posted lemon-le
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring自动装配相关的知识,希望对你有一定的参考价值。
今天的心情比较复杂,一言难尽!!!还是好好学习吧;学习地址:https://www.w3cschool.cn/wkspring/o1qy1h9q.html
自动装配之“byName”
还是前面的编辑器调用拼写检查的例子:
SpellChecker.java:
package com.lee.byName; public class SpellChecker { public SpellChecker() { System.out.println("Inside SpellChecker constructor"); } public void checkSpelling() { System.out.println("Inside checkSpelling"); } }
TextEditor.java:
package com.lee.byName; public class TextEditor { private SpellChecker spellChecker; private String name; public SpellChecker getSpellChecker() { return spellChecker; } public void setSpellChecker(SpellChecker spellChecker) { this.spellChecker = spellChecker; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void spellCkeck() { spellChecker.checkSpelling(); } }
MainApp.java:
package com.lee.byName; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); TextEditor td = (TextEditor) context.getBean("textEditor"); td.spellCkeck(); } }
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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="textEditor" class="com.lee.byName.TextEditor" autowire="byName"> <property name="name" value="Genric Text Editor"></property> </bean> <bean id="spellChecker" class="com.lee.byName.SpellChecker"> </bean> </beans>
这里XML配置文件中的beans的autowire属性设置为byName;然后将它的属性与配置文件中定义为相同名称的beans进行匹配和连接。我的理解就是:
<bean id="spellChecker" class="com.lee.byName.SpellChecker"> 这里的id名字spellChecker与前面TextEditor里定义的命名一样private SpellChecker spellChecker;
自动装配之“byType”
这里与上面唯一不同的就是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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="textEditor" class="com.lee.byType.TextEditor" autowire="byType"> <property name="name" value="Genric Text Editor"></property> </bean> <bean id="SpellChecker" class="com.lee.byType.SpellChecker"> </bean> </beans>
这里XML配置文件中的beans的autowire属性设置为byType;然后type与配置文件中beans名称中的相同的进行匹配和连接。我的理解就是:
<bean id="SpellChecker" class="com.lee.byName.SpellChecker"> 这里的id名字spellChecker与前面TextEditor里定义的命名一样private SpellChecker spellChecker;
byName:属性名称
byType:属性类型
自动装配之“构造函数自动装配”
Spring构造函数自动装配与byType非常相似;但它应用于构造器参数,在XML配置文件中beans的autowire设置为constructor。
SpellChecker.java:
package com.lee.constructor; public class SpellChecker { public SpellChecker() { System.out.println("Inside SpellChecker construtor"); } public void checkSpelling() { System.out.println("Inside checkSpelling"); } }
TextEdictor.java:
package com.lee.constructor; public class TextEdictor { private SpellChecker spellChecker; private String name; public TextEdictor(SpellChecker spellChecker,String name) { this.spellChecker=spellChecker; this.name = name; } public SpellChecker getSpellChecker() { return spellChecker; } public String getName() { return name; } public void spellCheck() { spellChecker.checkSpelling(); } }
MainApp.java:
package com.lee.constructor; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans3.xml"); TextEdictor td = (TextEdictor) context.getBean("textEditor"); td.spellCheck(); } }
Beans3.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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="textEditor" class="com.lee.constructor.TextEdictor" autowire="constructor"> <constructor-arg value="Genric Text Editor" /> </bean> <bean id="SpellChecker" class="com.lee.constructor.SpellChecker"> </bean> </beans>
码云:https://gitee.com/lemon_le/w3-Spring/tree/master/AutoBeans
以上是关于Spring自动装配的主要内容,如果未能解决你的问题,请参考以下文章