67 Spring的核心

67 Spring的核心

1.了解Spring的相关概念

1.1 Spring概述

Spring:春天。

Python和Java的竞争史

Java:1995年

Write once,run anywhere(跨平台)

Python:1991年

Life is short,use python!(龟叔)

2.x 语法像命令

3 x 更加接近于纯粹面向对象

Java 5新特性

兼容性 你可以用Python代码去调用Java,C,C++的代码,而且非常容易 Python库一直在增加。

200?-2009年 Sun(Java)被收购了!

Python以为上位的时机到了。

Spring来了…它改变了Java的开发方式( loC和AOP)…

Apache所有的产物几乎都是适用于Java。

2017年 AI(人工智能)概念吵的火热 都是用C的内容开发的,但是本身C开发的就够复杂了,还要用C去编写整个系统。

Python(胶水语言),科学家们比较喜欢了,Python计算库比较多…

微服务(SOA衍变)Dubbo + Zookeeper等 | Spring Cloud

在这里插入图片描述

Spring框架是一个轻量级的,不侵入的开源框架!它的核心是loC和AOP,整套生命体系中对于Java开发来讲都做了很好的支持,并且它在兼容性方面比较强,不独占,进行联合,对于第三方框架支持比较好.

核心Jar:

Beans,Core,Context,

SpringExpressionLanguage.

Spring面向bean编程。
在这里插入图片描述

1.2 Spring基础环境使用

4.x系列的稳定版(RELEASE)

导包:核心beans,core,content,spring表达式和日志(日志框架和commons日志

创建核心配置文件 applicationContext.xml

编写业务代码

public class HelloSpring{
		
	private String msg;
	private User user;
	
	public void helloSpring() {
		System.out.println(user.getUserName()+":hello!"+msg);
	}
	
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	
}

在核心配置文件中配置Bean

<?xml versions="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>
	
	</bean> -->
	<!-- 面向bean编程:bean可以理解为一个对象的描述 -->
	<!-- 配置User对象的描述  UserBean -->
	<bean id="user" class="cn.kgc.demo1.User">
		<property name="userName" value="小旭"></property>
	</bean>
	
	<!-- HelloSpring Bean -->
	<!-- id:唯一标识(Spring系统中唯一)  class:全限定名  -->
	<bean id="helloSpring" class="cn.kgc.demo1.HelloSpring">
		<!-- value:普通属性值 -->
		<property name="msg" value="World!"/>
		<!-- name:属性名   ref:另一个bean的id 用于注入对象属性 -->
		<property name="user" ref="user"/>
	</bean>

</beans>

使用API操作Spring

//加载配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

// 2.从Spring容器中获取HelloSpring的对象 然后使用即可
HelloSpring helloSpring = ac.getBean(HelloSpring.class);
helloSpring.helloSpring();

2.理解并掌握Spring loC的原理和配置

loC:Inversion of Control控制反转。

​ DI:Dependency Injection依赖注入。

传统的对象使用,必须先去创建对象Student stu = new Student();然后再使用stu调用方法和属性。

传统的对象属性赋值时,必须通过对象名.setxxx()方法赋值,而且如果是有对象属性的话,还要考虑另一个对象创建

public class User{
	private String userCode;
	private Long id;
	private String userName;  // setUserName("xxx")
	private Role role  //setRole(new Role(xxx))
	//省略getter/setter
}
main(){
	//创建User对象
	User user = new User();
	user.setUserCode("xxx");
	user.setId(xxx);
	....
	Role role = new Role();
	role.setXXX()
	....
	user.setRole(role);
分层开发:表现层,业务逻辑层,数据访问层
Controller ->Service ->DAO
public class UserServlet{
    private UserService userService = new UserServiceImpl();
    
}

public interface UserService{
    
}

public class UserServiceImpl implements UserService{
    // private UserDao userDao = new UserOracleDaoImpl(); // 耦合性
	
	private UserDao userDao; // xxx
	
	userDao.getList();
}

// 隔离数据库
public interface UserDao{
    List<User> getList()throxxxx;
}

// MySQL实现类
public class UserMySQLDaoImpl implements UserDao{
    // ...
}
// Oracle实现类
public class UserOracleDaoImpl implements UserDao{
    // ...
}

传统的对象的生命周期管理全都是由我们自己来维护的,而现在有了Spring的IoC之后,我们可以将对象的维护工作都交给它,未来你需要的时候,Spring就会提供给你。

传统的对象中的依赖(属性),以前也是由我们维护的,现在可以通过Spring loC中的DI来帮助你实现依赖的注入,按名称,按类型注入…。

极大的降低了我们应用的耦合性,同样增强了程序的扩展性和维护性,Spring 侵入式极低。

3. 理解并掌握Spring AOP的原理和配置

AOP(Ascept Oriented Programming)面向切面编程

Spring 侵入式比较低的特点

public class UserServiceImpl implements UserService{
	private Logger logger = xxx;
	
	public void addUser(User user){
		Connection conn = xxx;
		try{
		//....
		conn,commit();
		logger.info("添加成功!...")
		
		
		}catch(Exception e){
			conn.rollback();
			logger.error("添加失败!异常信息是:"+xxx);
		}
	}
}

代理模式实现增强处理!
在这里插入图片描述

AOP原理:将复杂的需求分解出不同方面,将散步在系统中的公共功能集中解决

采用代理机制组装起来运行,在不改变原程序的基础上对代码段进行增强处理,增加新的功能。

所谓面向切面编程,是一种通过预编译和运行期动态代理的方式实现在不修改源代码的情况下给程序动态添加功能的技术

需求:给应用程序添加日志记录!

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值