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





     

     


    
    
        
            
        

    

    
    
        
            
        
    




运行

/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

你可能感兴趣的:(JAVA 核心笔记 || [xxx] Spring 之 依赖注入 DL)