JAVA 核心笔记 || [xxx] Spring 之 依赖注入 DL

Spring 依赖注入 DL

Spring 两种注入方式

  • Setter 方法注入
  • 构造器注入

使用
App.java

import com.mj.bean.*;
import com.mj.bean.annotation.BeanConfig;
import com.mj.bean.annotation.BeanPerson;
import com.mj.bean.annotation.IBean;
import com.mj.bean.dl.BeanHideHelper;
import com.mj.bean.dl.BeanShowHelper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import java.io.FileNotFoundException;

public class App {



    public static void main(String args[]) throws FileNotFoundException{
        /*
        //ClassPathXmlApplicationContext
        ApplicationContext context = new ClassPathXmlApplicationContext("com/mj/xml/Bean.xml");
        BeanSay sa = (BeanSay) context.getBean("BeanSay");
        sa.setMsg("__Spring");
        sa.talk();

        //FileSystemXmlApplicationContext
        ApplicationContext fileContext = new FileSystemXmlApplicationContext("/src/com/mj/xml/Bean.xml");
        BeanSay sa1 = (BeanSay) fileContext.getBean("BeanSay");
        sa1.setMsg("=Spring====");
        sa1.talk();

        ApplicationContext animalContext = new ClassPathXmlApplicationContext("com/mj/xml/Bean.xml");
        BeanAnimal ani =  (BeanAnimal) animalContext.getBean("Animal");
        ani.setAnimalName("dog");
        ani.showAnimal();

        BeanAnimal animal =  (BeanAnimal) animalContext.getBean("Animal");
        animal.setAnimalName("pig");
        animal.showAnimal();

        // init method   destroy method
        BeanLife life = (BeanLife) context.getBean("BeanLife");
        life.showName();

        //BeanDog 继承  BeanAnimal
        BeanDog dog = (BeanDog)context.getBean("BeanDog");
        dog.showAnimal();
        //加载多配置文件
        BeanUser user = (BeanUser)context.getBean("BeanUser");
        user.showUser();
        */


        /*
        ApplicationContext annotationCtx = new AnnotationConfigApplicationContext(BeanConfig.class);
        IBean beanPerson = (IBean) annotationCtx.getBean("beanPerson");
        beanPerson.show();

        IBean beanStu = (IBean)annotationCtx.getBean("beanStu");
        beanStu.show();

        IBean beanTch = (IBean)annotationCtx.getBean("beanTch");
        beanTch.show();
        */

        // Setter 注入
        ApplicationContext context = new ClassPathXmlApplicationContext("com/mj/xml/BeanDL.xml");
        BeanShowHelper hps = (BeanShowHelper) context.getBean("BeanShowHelper");
        hps.show();

        //构造函数注入
        BeanHideHelper bhh = (BeanHideHelper) context.getBean("BeanHideHelper");
        bhh.show();



    }
}


IHelper.java

package com.mj.bean.dl;

public interface IHelper {

    public void doc();

    public void lang();
}

BeanHelperJava.java

package com.mj.bean.dl;

public class BeanShowHelper {

    private IHelper helper;

    public void setHelper(IHelper helper) {
        this.helper = helper;
    }

    public void show(){
        helper.doc();
        helper.lang();
    }
}

BeanHelperPython.java

package com.mj.bean.dl;

public class BeanHelperPython implements IHelper {

    @Override
    public void doc() {
        System.out.println("doc=python");
    }

    @Override
    public void lang() {
        System.out.println("lang=python");
    }
}


BeanShowHelper.java

package com.mj.bean.dl;

public class BeanShowHelper {

    private IHelper helper;

    public void setHelper(IHelper helper) {
        this.helper = helper;
    }

    public void show(){
        helper.doc();
        helper.lang();
    }
}

BeanHideHelper.java

package com.mj.bean.dl;

public class BeanHideHelper {

    private  IHelper helper;

    public  BeanHideHelper(IHelper helper){
        this.helper = helper;
    }

    public void show(){
        helper.doc();
        helper.lang();
    }



}

BeanDL.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 = "BeanHelperJava" class = "com.mj.bean.dl.BeanHelperJava"> </bean>

    <bean id = "BeanHelperPython" class = "com.mj.bean.dl.BeanHelperPython"> </bean>


    <!-- Setter注入 -->
    <bean id="BeanShowHelper" class="com.mj.bean.dl.BeanShowHelper" >
        <property name="helper">
            <ref bean="BeanHelperJava" ></ref>
        </property>

    </bean>

    <!-- 构造函数注入 -->
    <bean id="BeanHideHelper" class="com.mj.bean.dl.BeanHideHelper" >
        <constructor-arg>
            <bean class="com.mj.bean.dl.BeanHelperPython" ></bean>
        </constructor-arg>
    </bean>



</beans>

运行

/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=51657:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/tools.jar:/Users/mac/d/project/java/mspr/out/production/mspr:/Users/mac/d/project/java/mspr/lib/spring-instrument-4.3.18.RELEASE.jar:/Users/mac/d/project/java/mspr/lib/spring-instrument-tomcat-4.3.18.RELEASE.jar:/Users/mac/d/project/java/mspr/lib/spring-aspects-4.3.18.RELEASE.jar:/Users/mac/d/project/java/mspr/lib/spring-context-support-4.3.18.RELEASE.jar:/Users/mac/d/project/java/mspr/lib/spring-aop-4.3.18.RELEASE.jar:/Users/mac/d/project/java/mspr/lib/spring-jms-4.3.18.RELEASE.jar:/Users/mac/d/project/java/mspr/lib/spring-expression-4.3.18.RELEASE.jar:/Users/mac/d/project/java/mspr/lib/spring-jdbc-4.3.18.RELEASE.jar:/Users/mac/d/project/java/mspr/lib/spring-oxm-4.3.18.RELEASE.jar:/Users/mac/d/project/java/mspr/lib/commons-logging-1.2.jar:/Users/mac/d/project/java/mspr/lib/spring-messaging-4.3.18.RELEASE.jar:/Users/mac/d/project/java/mspr/lib/aopalliance-1.0.jar:/Users/mac/d/project/java/mspr/lib/spring-tx-4.3.18.RELEASE.jar:/Users/mac/d/project/java/mspr/lib/spring-beans-4.3.18.RELEASE.jar:/Users/mac/d/project/java/mspr/lib/spring-orm-4.3.18.RELEASE.jar:/Users/mac/d/project/java/mspr/lib/spring-context-4.3.18.RELEASE.jar:/Users/mac/d/project/java/mspr/lib/spring-core-4.3.18.RELEASE.jar:/Users/mac/d/project/java/mspr/lib/spring-test-4.3.18.RELEASE.jar App
objc[905]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/java (0x1003b54c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x1023f54e0). One of the two will be used. Which one is undefined.
Dec 05, 2018 12:11:22 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@67424e82: startup date [Wed Dec 05 12:11:22 CST 2018]; root of context hierarchy
Dec 05, 2018 12:11:22 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/mj/xml/BeanDL.xml]
=help java=
lang=java
doc=python
lang=python

Process finished with exit code 0
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,222评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,455评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,720评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,568评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,696评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,879评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,028评论 3 409
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,773评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,220评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,550评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,697评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,360评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,002评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,782评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,010评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,433评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,587评论 2 350

推荐阅读更多精彩内容