package com.study.spring;

import com.study.spring.beans.Blue;
import com.study.spring.beans.Book;
import com.study.spring.beans.Dog;
import com.study.spring.beans.Hourse;
import com.study.spring.beans.MyEnv;
import com.study.spring.beans.PAD;
import com.study.spring.beans.Pean;
import com.study.spring.config.MyConfig;
import com.study.spring.etc.MyFactoryBean;
import com.study.spring.service.HelloService;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created on 2018-06-19
 *
 * @author liuzhaoyuan
 */
public class Test1 {

    @Test
    public void te() throws Exception {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class);

        HelloService helloService = (HelloService) applicationContext.getBean("helloService");

        Hourse hourse = (Hourse) applicationContext.getBean("hourse");
        hourse.say();

        helloService.sayHello();
        helloService.div(10, 5);

        MyFactoryBean myFactoryBean = (MyFactoryBean) applicationContext.getBean("&myFactoryBean");

        myFactoryBean.getObject().say();

        Blue blue = (Blue) applicationContext.getBean("myFactoryBean");
        blue.say();

        applicationContext.close();
    }

    @Test
    public void tr2() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class);

        Hourse hourse = (Hourse) applicationContext.getBean(Hourse.class);
        hourse.say();

//        hourse = (Hourse) applicationContext.getBean("hourse02");
//        hourse.say();

    }

    @Test
    public void testProperties() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class);
        printBean(applicationContext);
        Dog bean = (Dog) applicationContext.getBean("dog01");
        System.out.println(bean.getName());

    }

    @Test
    public void testPean() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class);
        printBean(applicationContext);
        Pean bean1 = (Pean) applicationContext.getBean("pean");
        Pean bean2 = (Pean) applicationContext.getBean("pean");
        System.out.println(bean1);
        System.out.println(bean2);

        PAD hourse1 = (PAD) applicationContext.getBean("pad");
        PAD hourse2 = (PAD) applicationContext.getBean("pad");
        System.out.println(hourse1);
        System.out.println(hourse2);

    }

    /**
     * 1. 命令行参数激活
     */
    @Test
    public void testProfile() {
        // 1. 创建 applicationContext
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        // 2. 设置激活的环境
        applicationContext.getEnvironment().setActiveProfiles("test");

        // 3. 注册配置类
        applicationContext.register(MyConfig.class);

        // 4. 刷新容器
        applicationContext.refresh();

        printBean(applicationContext);
        MyEnv bean = applicationContext.getBean(MyEnv.class);

        System.out.println("----------------------------------------");
        System.out.println(bean.getEnv());

    }

    private void printBean(AnnotationConfigApplicationContext applicationContext) {
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for (String definitionName : beanDefinitionNames) {
            System.out.println("-->  " + definitionName);
        }
    }
}

@@ -1548,16 +1548,675 @@
 e.say();
+%0A        %0A        ConfigurableListableBeanFactory factory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory();%0A%0A        BeanDefinitionRegistry beanDefinitionRegistry = (DefaultListableBeanFactory) factory;%0A%0A        // %E5%90%8E%E7%AB%AF%E7%9A%84%E5%8D%95%E4%BE%8B%E9%83%BD%E7%94%B1%E5%AE%83%E7%AE%A1%E7%90%86%EF%BC%8C%E8%BF%99%E9%87%8C%E6%98%AF%E8%8E%B7%E5%8F%96%E4%B8%80%E4%B8%AA%E5%8D%95%E4%BE%8B bean%0A        Hourse aa = (Hourse) applicationContext.getBean(%22hourse%22);%0A%0A        // %E4%BD%BF%E7%94%A8%0A        hourse.say();%0A%0A%0A        // %E6%89%8B%E5%8A%A8%E9%94%80%E6%AF%81%0A        beanDefinitionRegistry.removeBeanDefinition(%22hourse%22);%0A%0A%0A        Hourse aa2 = applicationContext.getBean(%22hourse%22, Hourse.class);%0A%0A        if (aa2 == null) %7B%0A            System.out.println(%22%E5%B7%B2%E7%BB%8F%E9%94%80%E6%AF%81%22);%0A        %7D else %7B%0A            System.out.println(%22%E6%B2%A1%E6%9C%89%E9%94%80%E6%AF%81%22);%0A        %7D
 %0A%0A//    

Update Test1.java