java spring框架配置_Java必备干货:Spring框架之IOC的基本配置

Spring

框架之

IOC

的基本配置

前言,上一章我们学习了

Spring

IOC

特性以及

IOC

的实现原理:注解和反射,本章我们将学习如何在

Spring

中使用

IOC

Spring

IOC

配置

Spring

最重要的特性是

IOC

控制反转,利于

IOC

我们能降低对象之间的耦合性。

IOC

需要通过一定的配置实现,配置方法分为:

1)

使用

xml

文件配置

2)使用注解配置

使用

Spring

的基本功能,必须先导入

Spring

的依赖:

org.springframework

spring-context

5.1.5.RELEASE

Spring Context

:向

Spring

框架提供上下文信息。

Spring

上下文包括企业服务,例如

JNDI

EJB

、电子邮件、国际化、校验和调度功能。它包含

Spring Core

组件,能实现

IOC

的核心功能。

使用

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

目录下,添加配置文件:

applicationContext.xml

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">

配置说明:

是根标签,代表

Spring

Java

对象容器

标签代表在容器中创建一个

Java

对象,属性

id

代表对象名,

class

是对象的类型。

在配置文件中首先创建了一个

cpu

对象和一个

memory

对象,然后创建了一个

computer

对象,

computer

中有

Cpu

类型的

cpu

属性和

Memory

类型

memory

属性以及

String

类型的

brand

属性,这里使用依赖注入的方式给属性赋值。

property

指的是对象的属性,

name

是属性名,

ref

是对象引用,这里引用了前面的

cpu

对象。

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

}

}

d5feccc77771719cecd6efc3a6faa5ca.png

使用注解配置

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

}

}

ef5a63403199d7ef76e5cab610f93b68.png

总结

本章我们学习了两种

Spring

的配置方法,

XML

配置的好处是:和代码耦合性低,容易维护,注解配置的好处是:代码简洁。两种配置方法的优势互补,在实际开发过程中一般会使用

XML

和注解混合进行配置。

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值