Spring框架之IOC的基本配置,前言,上一章咱们学习了Spring的IOC特性以及IOC的实现原理:注解和反射,本章咱们将学习如何在Spring中使用IOC。spring
Spring的IOC配置
Spring最重要的特性是IOC控制反转,利于IOC咱们能下降对象之间的耦合性。app
IOC须要经过必定的配置实现,配置方法分为:框架
1)使用xml文件配置maven
2)使用注解配置学习
使用Spring的基本功能,必须先导入Spring的依赖:this
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>5.1.5.RELEASE</version>
- </dependency>
Spring Context:向 Spring框架提供上下文信息。Spring 上下文包括企业服务,例如JNDI、EJB、电子邮件、国际化、校验和调度功能。它包含Spring Core组件,能实现IOC的核心功能。spa
使用xml文件配置
- /**
- * CPU接口
- */
- public interface Cpu {
- void run();
- }
- /**
- * AMD的CPU
- */
- public class AMDCpu implements Cpu {
- public void run() {
- System.out.println("AMD的CPU正在运行....");
- }
- }
- /**
- * 内存接口
- */
- public interface Memory {
- void read();
- void write();
- }
- /**
- * DDR8G的内存
- */
- public class DDR8GMemory implements Memory {
- public void read() {
- System.out.println("使用DDR8G的内存读取数据....");
- }
- public void write() {
- System.out.println("使用DDR8G的内存写入数据....");
- }
- }
- 相似的IntelCpu和DDR16Memory类省略了代码
- /**
- * 电脑类
- */
- public class Computer {
-
- private Cpu cpu;
- private Memory memory;
- private String brand;
- ...省略get\set
- public Computer() {
}
public Computer(String brand, Cpu cpu, Memory memory) {
this.brand = brand;
this.cpu = cpu;
this.memory = memory;
}
public void start(){
System.out.println(brand+"电脑启动了");
cpu.run();
memory.read();
memory.write();
}
- }
在maven项目的resources目录下,添加配置文件:.net
applicationContext.xmlxml
- <?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">
- <!-- CPU对象-->
- <bean id="cpu" class="com.qianfeng.springioc.demo3.IntelCpu"/>
- <!--Memory对象-->
- <bean id="memory" class="com.qianfeng.springioc.demo3.DDR16GMemory"/>
- <!--电脑对象-->
- <bean id="computer" class="com.qianfeng.springioc.demo3.Computer">
- <!--属性的注入-->
- <property name="cpu" ref="cpu"></property>
- <property name="memory" ref="memory"></property>
- <property name="brand" value="小米电脑"></property>
- </bean>
- </beans>
配置说明:对象
<beans>是根标签,表明Spring的Java对象容器
<bean>标签表明在容器中建立一个Java对象,属性id表明对象名,class是对象的类型。
在配置文件中首先建立了一个cpu对象和一个memory对象,而后建立了一个computer对象,computer中有Cpu类型的cpu属性和Memory类型memory属性以及String类型的brand属性,这里使用依赖注入的方式给属性赋值。
<property name="cpu" ref="cpu"></property>
property 指的是对象的属性,name是属性名,ref是对象引用,这里引用了前面的cpu对象。
<property name="brand" value="华硕电脑"></property>
brand属性注入的是数值而不是对象引用,这里使用value注入值。
Spring上下文对象
Spring容器能够看作是一个JavaBean的工厂BeanFactory,BeanFactory负责建立并保存各个JavaBean,BeanFactory的子类有:
1)ClassPathXMLApplicationContext
基于XML配置文件上下文
2)AnnotationConfigApplicationContext
基于注解配置的上下文
3)FileSystemApplicationContext
基于文件系统的上下文
使用ClassPathXMLApplicationContext的方法:
- public class TestComputer {
-
- @Test
- public void testComputer(){
- //建立XML文件的应用程序上下文对象
- ClassPathXmlApplicationContext cxt =
- new ClassPathXmlApplicationContext("applicationContext.xml");
- //经过类型从容器得到Java对象
- Computer computer = cxt.getBean(Computer.class);
- //还能够经过对象名得到对象
- // Computer computer = (Computer) cxt.getBean("computer");
- computer.start();
- }
- }

使用注解配置
Spring的IOC也能够不使用配置文件,彻底经过Java代码和注解实现配置,这种配置方法代码更加简洁。
经常使用注解:
@Component
配置到类上面,Spring容器会自动扫描并添加有该注解类的对象
@Autowired
配置到属性或set方法上,容器会将容器中同类型的对象自动注入到属性中
@Qualifier
用于给不一样的组件设置标识,用于区分多个相同类型的对象
@Value
注入通常类型的值,如:@Value(20) 、 @Value("张三")
@Configuration
加在配置类上,该类做为Spring启动的入口
@ComponentScan
和@Configuration配合使用,加在配置类上,用于扫描包中全部@Component注解的类
- 在DDR8Memory类和IntelCpu类上添加@Component注解
- 修改Computer类:
- @Component
- public class Computer {
-
- @Value("苹果电脑")
- private String brand;
-
- @Autowired
- private Cpu cpu;
-
- @Autowired
- private Memory memory;
- ....
- }
-
- @Configuration
- @ComponentScan("com.qianfeng.springioc.demo4")
- public class MyConfig {
-
- public static void main(String[] args) {
- //建立基于注解的上下文对象
- AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext(MyConfig.class);
- //得到Computer对象
- Computer computer = cxt.getBean(Computer.class);
- computer.start();
- }
- }

总结
本章咱们学习了两种Spring的配置方法,XML配置的好处是:和代码耦合性低,容易维护,注解配置的好处是:代码简洁。两种配置方法的优点互补,在实际开发过程当中通常会使用XML和注解混合进行配置。