码迷,mamicode.com
首页 > 编程语言 > 详细

Spring自动装配

时间:2018-07-16 00:28:42      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:ati   import   oid   gen   htm   w3c   get   理解   r.java   

今天的心情比较复杂,一言难尽!!!还是好好学习吧;学习地址: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自动装配

标签:ati   import   oid   gen   htm   w3c   get   理解   r.java   

原文地址:https://www.cnblogs.com/lemon-le/p/9315620.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!