JAVA 核心笔记 || [xxx] Spring 之 JavaConfig 配置多个Bean

配置多 Bean

用法

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 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();

    }
}

BeanConfig.java

package com.mj.bean.annotation;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeanConfig {

    @Bean(name="beanPerson" )
    public IBean beanPerson(){
        return new BeanPerson();
    }

    @Bean(name = "beanStu")
    public IBean beanStu(){
        return new BeanStu();
    }

    @Bean(name = "beanTch")
    public IBean beanTch(){
        return new BeanTch();
    }
}

IBean.java

package com.mj.bean.annotation;

public interface IBean {

    public  void show();
}

BeanPerson.java

package com.mj.bean.annotation;

public class BeanPerson implements IBean {
    private String cName = "BeanPerson";
    @Override
    public void show(){
        System.out.println( cName );
    }

    public void setcName(String cName) {
        this.cName = cName;
    }

}

BeanStu.java

package com.mj.bean.annotation;

public class BeanStu implements IBean {
    private String cName = "BeanStu";
    @Override
    public  void show(){
        System.out.println( cName);
    }

    public void setcName(String cName) {
        this.cName = cName;
    }
}

BeanTch.java

package com.mj.bean.annotation;

public class BeanTch implements  IBean {
    private String cName = "BeanTch";

    @Override
    public  void show(){
        System.out.println(cName);
    }

    public void setcName(String cName) {
        this.cName = cName;
    }
}

运行

/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=51288:/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[828]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/java (0x10f6814c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10f70d4e0). One of the two will be used. Which one is undefined.
Dec 05, 2018 10:36:12 AM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5ce65a89: startup date [Wed Dec 05 10:36:12 CST 2018]; root of context hierarchy
BeanPerson
BeanStu
BeanTch

Process finished with exit code 0

你可能感兴趣的:(JAVA 核心笔记 || [xxx] Spring 之 JavaConfig 配置多个Bean)