Spring简介
目前遇到的问题:
A、我们现在使用的mvc模式,发现层和层之间的耦合性太高了
B、目前书写代码的后期的扩展性不高
解决方案
Spring 技术 Spring 是解决业务层和其他各层之间的耦合问题
Spring 学习的核心技能点
A、IOC:控制反转---帮助我们创建对象的
B、AOP:面向切面---提升代码的扩展性
C、TX:声明式事务---事务管理
SpringIOC的使用
责任链
我们使用mvc进行开发的时候,数据在各层之间进行传递,数据在业务上构成一个链条,这个链条称之为责任链。
基于责任链开发的缺点
基于责任链开发模式,我们发现代码层和层之间相互调用,造成了层和层的耦合性太高了 我们写代码的时候讲究的原则--低耦合 高内聚
解决的方案
Spring IOC : 控制反转
导包:

书写applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--Student stu =new Student()-->
<bean id="stu" class="com.bjsxt.spring1.Student"></bean>
</beans>
解析xml调用对象:
public class Test {
public static void main(String[] args) {
// Student stu =new Student();
//[1]解析xml文件
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
//[2]获得对象
//Student stu = (Student) app.getBean("stu");
Student stu1 = app.getBean("stu", Student.class);
stu1.eat();
}
}
Spring IOC的好处:
实现了各层之间的解耦


浙公网安备 33010602011771号