• Spring 使用指南 ~ 1、Spring 的 IOC 和 DI 简介


    一、IOC && DI

    1、概念介绍

    • IOC:控制反转,即把类的实例化交给 Spring 来管理
    • DI:依赖注入,即通过 Spring 为应用中被 Spring 管理的类实例注入其依赖的实例和其他信息

    2、使用 XML 形式

    1)、构造函数注入
    package com.luo.spring.guides.iocdi.xml.constructorconfusion;
    
    import org.springframework.context.support.GenericXmlApplicationContext;
    
    /**
    * @author : archer
    * @date : Created in 2022/11/30 14:46
    * @description :
    */
    public class ConstructorConfusion {
    
       private String someValue;
    
       public ConstructorConfusion(String someValue) {
           System.out.println("ConstructorConfusion(String) called");
           this.someValue = someValue;
       }
    
       public ConstructorConfusion(int someValue) {
           System.out.println("ConstructorConfusion(int) called");
           this.someValue = "Number: " + Integer.toString(someValue);
       }
    
       public String toString() {
           return someValue;
       }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    app-context-xml.xml

    
    
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:c="http://www.springframework.org/schema/c"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="constructorConfusion"
              class="com.luo.spring.guides.iocdi.xml.constructorconfusion.ConstructorConfusion">
            <constructor-arg type="int">
                <value>90value>
            constructor-arg>
        bean>
    beans>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    测试

    package com.luo.spring.guides.iocdi.xml.constructorconfusion;
    
    import org.springframework.context.support.GenericXmlApplicationContext;
    
    /**
     * @author : archer
     * @date : Created in 2022/11/30 14:51
     * @description :
     */
    public class Main {
        public static void main(String... args) {
            GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
            ctx.load("classpath:iocdi/app-context.xml");
            ctx.refresh();
            ConstructorConfusion cc = (ConstructorConfusion) ctx.getBean("constructorConfusion");
            System.out.println(cc);
            ctx.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    输出

    ConstructorConfusion(int) called
    Number: 90

    2)、setter 注入
    package com.luo.spring.guides.iocdi.xml.setter;
    
    public class Dependency {
    
        @Override
        public String toString() {
            return "Dependency{}";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    package com.luo.spring.guides.iocdi.xml.setter;
    
    public class SetterInjection {
    
    	private Dependency dependency;
    
    	public void setDependency(Dependency dependency) {
    		this.dependency = dependency;
    	}
    
    	@Override
    	public String toString() {
    		return dependency.toString();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    app-context-xml.xml

    
    
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:c="http://www.springframework.org/schema/c"
           xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    
        <bean id="dependency" class="com.luo.spring.guides.iocdi.xml.setter.Dependency"/>
    
        <bean id="setterInjection" class="com.luo.spring.guides.iocdi.xml.setter.SetterInjection"
              p:dependency-ref="dependency"/>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    测试

    package com.luo.spring.guides.iocdi.xml.setter;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * @author : archer
     * @date : Created in 2022/11/30 15:22
     * @description :
     */
    public class Main {
        public static void main(String[] args) {
            ApplicationContext ctx = new ClassPathXmlApplicationContext
                    ("iocdi/injection/setter/app-context-xml.xml");
    
            SetterInjection mr = ctx.getBean("setterInjection", SetterInjection.class);
            System.out.println(mr);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    输出

    Dependency{}

    3)、字段注入
    package com.luo.spring.guides.iocdi.xml.field;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    /**
     * Created by iuliana.cosmina on 2/15/17.
     */
    public class Inspiration {
    
    	private String lyric = "I can keep the door cracked open, to let light through";
    
    	public Inspiration(){
    
    	}
    
    	public Inspiration(@Value("For all my running, I can understand") String lyric) {
    		this.lyric = lyric;
    	}
    
    	public String getLyric() {
    		return lyric;
    	}
    
    	public void setLyric(String lyric) {
    		this.lyric = lyric;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    package com.luo.spring.guides.iocdi.xml.field;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    
    /**
     * Created by iuliana.cosmina on 2/15/17.
     */
    public class Singer {
    
    	@Autowired
    	private Inspiration inspirationBean;
    
    	public void sing() {
    		System.out.println("... " + inspirationBean.getLyric());
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    
    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                      http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:component-scan
                base-package="com.luo.spring.guides.iocdi.xml.field"/>
    
        <bean id="singer" class="com.luo.spring.guides.iocdi.xml.field.Singer"/>
    
        <bean id="inspiration" class="com.luo.spring.guides.iocdi.xml.field.Inspiration"/>
    
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    测试

    package com.luo.spring.guides.iocdi.xml.field;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * @author : archer
     * @date : Created in 2022/11/30 20:46
     * @description :
     */
    public class Main {
    
        public static void main(String... args) {
            ApplicationContext ctx = new ClassPathXmlApplicationContext("iocdi/injection/field/app-context-xml.xml");
            Singer singer = ctx.getBean("singer", Singer.class);
    
            Singer singerBean = ctx.getBean(Singer.class);
            System.out.println(singer == singerBean);
            singerBean.sing();
            singer.sing();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    输出

    true
    … I can keep the door cracked open, to let light through
    … I can keep the door cracked open, to let light through

    4)、查找方法注入
    • 这种方式性能较差,当有其他方式替代时,请使用其他方式
    package com.luo.spring.guides.iocdi.lookup.xml;
    
    /**
     * Created by iuliana.cosmina on 2/20/17.
     */
    public class Singer {
        private String lyric = "I played a quick game of chess with the salt and pepper shaker";
    
        public void sing() {
    //        System.out.println(lyric);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    package com.luo.spring.guides.iocdi.lookup.xml;
    
    public interface DemoBean {
        Singer getMySinger();
        void doSomething();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    package com.luo.spring.guides.iocdi.lookup.xml;
    
    public abstract class AbstractLookupDemoBean implements DemoBean {
        public abstract Singer getMySinger();
    
        @Override
        public void doSomething() {
            getMySinger().sing();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    package com.luo.spring.guides.iocdi.lookup.xml;
    
    public class StandardLookupDemoBean implements DemoBean {
    
    	private Singer mySinger;
    
    	public void setMySinger(Singer mySinger) {
    		this.mySinger = mySinger;
    	}
    
    	@Override
    	public Singer getMySinger() {
    		return this.mySinger;
    	}
    
    	@Override
    	public void doSomething() {
    		mySinger.sing();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    
    
    <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.xsd">
    
    <bean id="singer" class="com.luo.spring.guides.iocdi.lookup.xml.Singer"  scope="prototype"/>
    
    <bean id="abstractLookupBean" class="com.luo.spring.guides.iocdi.lookup.xml.AbstractLookupDemoBean">
        <lookup-method name="getMySinger" bean="singer"/>
    bean>
    
    <bean id="standardLookupBean" class="com.luo.spring.guides.iocdi.lookup.xml.StandardLookupDemoBean">
        <property name="mySinger" ref="singer"/>
    bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    测试

    package com.luo.spring.guides.iocdi.lookup.xml;
    
    import org.springframework.context.support.GenericXmlApplicationContext;
    import org.springframework.util.StopWatch;
    
    /**
     * @author : archer
     * @date : Created in 2022/12/1 11:23
     * @description :
     */
    public class Main {
    
        public static void main(String... args) {
            GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
            ctx.load("classpath:iocdi/injection/lookup/app-context-xml.xml");
            ctx.refresh();
    
            DemoBean abstractBean = ctx.getBean("abstractLookupBean", DemoBean.class);
            DemoBean standardBean = ctx.getBean("standardLookupBean", DemoBean.class);
    
            displayInfo("abstractLookupBean", abstractBean);
            displayInfo("standardLookupBean", standardBean);
    
            ctx.close();
        }
    
        public static void displayInfo(String beanName, DemoBean bean) {
            Singer singer1 = bean.getMySinger();
            Singer singer2 = bean.getMySinger();
    
            System.out.println("[" + beanName + "]: Singer Instances the Same?  "
                    + (singer1 == singer2));
    
            StopWatch stopWatch = new StopWatch();
            stopWatch.start("lookupDemo");
    
            for (int x = 0; x < 100000; x++) {
                Singer singer = bean.getMySinger();
                singer.sing();
            }
    
            stopWatch.stop();
            System.out.println("100000 gets took " + stopWatch.getTotalTimeMillis() + " ms");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    输出

    [abstractLookupBean]: Singer Instances the Same? false
    100000 gets took 561 ms
    [standardLookupBean]: Singer Instances the Same? true
    100000 gets took 1 ms

    3、使用注解形式

    1)、构造函数注入
    package com.luo.spring.guides.iocdi.annotation.constructorconfusion;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.support.GenericXmlApplicationContext;
    import org.springframework.stereotype.Service;
    
    @Service("constructorConfusion")
    public class ConstructorConfusion {
    
    	private String someValue;
    
    	public ConstructorConfusion(String someValue) {
    		System.out.println("ConstructorConfusion(String) called");
    		this.someValue = someValue;
    	}
    
    	@Autowired
    	public ConstructorConfusion(@Value("90") int someValue) {
    		System.out.println("ConstructorConfusion(int) called");
    		this.someValue = "Number: " + Integer.toString(someValue);
    	}
    
    	public String toString() {
    		return someValue;
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    app-context-annotation.xml

    
    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:c="http://www.springframework.org/schema/c"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/context 
              http://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:component-scan 
              base-package="com.luo.spring.guides.iocdi.annotation"/>
    
        <bean id="message" class="java.lang.String"
              c:_0="I hope that someone gets my message in a bottle"/>
    
        <bean id="message2" class="java.lang.String"
              c:_0="I know I won't be injected :("/>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    测试

    package com.luo.spring.guides.iocdi.annotation.constructorconfusion;
    
    import org.springframework.context.support.GenericXmlApplicationContext;
    
    /**
     * @author : archer
     * @date : Created in 2022/11/30 14:51
     * @description :
     */
    public class Main {
        public static void main(String... args) {
            GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
            ctx.load("classpath:iocdi/app-context-annotation.xml");
            ctx.refresh();
    
            ConstructorConfusion cc = (ConstructorConfusion) ctx.getBean("constructorConfusion");
            System.out.println(cc);
            ctx.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    输出

    ConstructorConfusion(int) called
    Number: 90

    2)、setter 注入
    package com.luo.spring.guides.iocdi.annotation.setter;
    
    import org.springframework.stereotype.Component;
    
    @Component("dependency")
    public class Dependency {
    
        @Override
        public String toString() {
            return "Dependency{}";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    package com.luo.spring.guides.iocdi.annotation.setter;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    @Component("setterInjection")
    public class SetterInjection {
    
    	private Dependency dependency;
    
    	@Autowired
    	public void setDependency(Dependency dependency) {
    		this.dependency = dependency;
    	}
    
    	@Override
    	public String toString() {
    		return dependency.toString();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    配置类

    package com.luo.spring.guides.iocdi.annotation.setter;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * @author : archer
     * @date : Created in 2022/11/30 15:32
     * @description :
     */
    @ComponentScan(basePackages = {"com.luo.spring.guides.iocdi.annotation"})
    @Configuration
    public class SetterInjectionConfiguration {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    测试

    package com.luo.spring.guides.iocdi.annotation.setter;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    /**
     * @author : archer
     * @date : Created in 2022/11/30 15:33
     * @description :
     */
    public class Main {
        public static void main(String... args) {
            ApplicationContext ctx = new AnnotationConfigApplicationContext
                    (SetterInjectionConfiguration.class);
            SetterInjection mr = ctx.getBean("setterInjection", SetterInjection.class);
            System.out.println(mr);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    输出

    Dependency{}

    3)、字段注入
    package com.luo.spring.guides.iocdi.annotation.field;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    /**
     * Created by iuliana.cosmina on 2/15/17.
     */
    @Service("singer")
    public class Singer {
    
    	@Autowired
    	private Inspiration inspirationBean;
    
    	public void sing() {
    		System.out.println("... " + inspirationBean.getLyric());
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    package com.luo.spring.guides.iocdi.annotation.field;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    /**
     * Created by iuliana.cosmina on 2/15/17.
     */
    @Component
    public class Inspiration {
    
    	private String lyric = "I can keep the door cracked open, to let light through";
    
    	public Inspiration(@Value("For all my running, I can understand") String lyric) {
    		this.lyric = lyric;
    	}
    
    	public String getLyric() {
    		return lyric;
    	}
    
    	public void setLyric(String lyric) {
    		this.lyric = lyric;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    测试

    package com.luo.spring.guides.iocdi.annotation.field;
    
    import com.luo.spring.guides.iocdi.annotation.InjectionConfiguration;
    import com.luo.spring.guides.iocdi.annotation.setter.SetterInjection;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.support.GenericXmlApplicationContext;
    
    /**
     * @author : archer
     * @date : Created in 2022/11/30 20:46
     * @description :
     */
    public class Main {
    
        public static void main(String... args) {
            ApplicationContext ctx = new AnnotationConfigApplicationContext
                    (InjectionConfiguration.class);
            Singer singer = ctx.getBean("singer", Singer.class);
    
            Singer singerBean = ctx.getBean(Singer.class);
            System.out.println(singer == singerBean);
            singerBean.sing();
            singer.sing();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    输出

    ConstructorConfusion(int) called
    true
    … For all my running, I can understand
    … For all my running, I can understand

    4)、查找方法注入
    • 这种方式性能较差,当有其他方式替代时,请使用其他方式
    package com.luo.spring.guides.iocdi.lookup.annotation;
    
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component;
    
    /**
     * Created by iuliana.cosmina on 2/20/17.
     */
    @Component("singer")
    @Scope("prototype")
    public class Singer {
        private String lyric = "I played a quick game of chess with the salt and pepper shaker";
    
        public void sing() {
            // commented to avoid console pollution
            //System.out.println(lyric);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    package com.luo.spring.guides.iocdi.lookup.annotation;
    
    public interface DemoBean {
        Singer getMySinger();
        void doSomething();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    package com.luo.spring.guides.iocdi.lookup.annotation;
    
    import org.springframework.beans.factory.annotation.Lookup;
    import org.springframework.stereotype.Component;
    
    @Component("abstractLookupBean")
    public class AbstractLookupDemoBean implements DemoBean {
        @Lookup("singer")
        public Singer getMySinger() {
            return null; // This implementation will be overridden by dynamically generated subclass
        }
    
        @Override
        public void doSomething() {
            getMySinger().sing();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    package com.luo.spring.guides.iocdi.lookup.annotation;
    
    import org.springframework.stereotype.Component;
    
    import javax.annotation.Resource;
    
    @Component("standardLookupBean")
    public class StandardLookupDemoBean implements DemoBean {
    
    	private Singer mySinger;
    
    //	@Autowired
    //	@Qualifier("singer")
    	@Resource(name = "singer")
    	public void setMySinger(Singer mySinger) {
    		this.mySinger = mySinger;
    	}
    
    	@Override
    	public Singer getMySinger() {
    		return this.mySinger;
    	}
    
    	@Override
    	public void doSomething() {
    		mySinger.sing();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    测试

    package com.luo.spring.guides.iocdi.lookup.annotation;
    
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.support.GenericApplicationContext;
    import org.springframework.context.support.GenericXmlApplicationContext;
    import org.springframework.util.StopWatch;
    
    /**
     * @author : archer
     * @date : Created in 2022/12/1 12:04
     * @description :
     */
    public class Main {
    
        @Configuration
        @ComponentScan(basePackages = {"com.luo.spring.guides.iocdi.lookup.annotation"})
        public static class LookupConfig {}
    
        public static void main(String... args) {
            GenericApplicationContext ctx = new AnnotationConfigApplicationContext(LookupConfig.class);
            //Arrays.stream(ctx.getBeanDefinitionNames()).forEach(s-> System.out.println(s));
    
            DemoBean abstractBean = ctx.getBean("abstractLookupBean", DemoBean.class);
            DemoBean standardBean = ctx.getBean("standardLookupBean", DemoBean.class);
    
    
            displayInfo("abstractLookupBean", abstractBean);
            displayInfo("standardLookupBean", standardBean);
    
            ctx.close();
        }
    
        public static void displayInfo(String beanName, DemoBean bean) {
            Singer singer1 = bean.getMySinger();
            Singer singer2 = bean.getMySinger();
    
            System.out.println("[" + beanName + "]: Singer Instances the Same?  "
                    + (singer1 == singer2));
    
            StopWatch stopWatch = new StopWatch();
            stopWatch.start("lookupDemo");
    
            for (int x = 0; x < 100000; x++) {
                Singer singer = bean.getMySinger();
                singer.sing();
            }
    
            stopWatch.stop();
            System.out.println("100000 gets took " + stopWatch.getTotalTimeMillis() + " ms");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    输出

    [abstractLookupBean]: Singer Instances the Same? false
    100000 gets took 575 ms
    [standardLookupBean]: Singer Instances the Same? true
    100000 gets took 1 ms

  • 相关阅读:
    浅谈Docker原理
    计算机视觉:池化层的作用是什么?
    vue3+ts部分场景示例
    Redisson分布锁原理分析及源码解读
    既然测试也要求写代码,那干脆让开发兼任测试不就好了吗?
    .Net 6.0全局异常捕获
    leetcode_力扣_84. 柱状图中最大的矩形
    TCP相关面试题
    vue+element模仿实现PC端网易云,对接第三方接口
    【深度学习基础课程】单一神经元
  • 原文地址:https://blog.csdn.net/lxz352907839/article/details/128118380