Spring 使用指南 ~ 1Spring 的 IOC 和 DI 简介
Posted 戴泽supp
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring 使用指南 ~ 1Spring 的 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;
app-context-xml.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"
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>90</value>
</constructor-arg>
</bean>
</beans>
测试
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();
输出
ConstructorConfusion(int) called
Number: 90
2)、setter 注入
package com.luo.spring.guides.iocdi.xml.setter;
public class Dependency
@Override
public String toString()
return "Dependency";
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();
app-context-xml.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"
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>
测试
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);
输出
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;
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());
<?xml version="1.0" encoding="UTF-8"?>
<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>
测试
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();
输出
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);
package com.luo.spring.guides.iocdi.lookup.xml;
public interface DemoBean
Singer getMySinger();
void doSomething();
package com.luo.spring.guides.iocdi.lookup.xml;
public abstract class AbstractLookupDemoBean implements DemoBean
public abstract Singer getMySinger();
@Override
public void doSomething()
getMySinger().sing();
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();
<?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.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>
测试
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");
输出
[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;
app-context-annotation.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:c=1Spring源码学习~Spring 介绍