Spring的核心
1. 了解Spring的相关概念
1.1 Spring概述
Spring:春天。
Python和Java的竞争史。
Java:1995年 Python:1991年
Write once , run anywhere(跨平台) Life is short , use python!(龟叔) 2.x 语法像命令 3.x更加接近于纯粹面向对象
Java 5新特性 兼容性 你可以用Python代码去调用Java、C、C++的代码,而且非常容易 Python库一直在增加
200? - 2009年 Sun(Java)被收购了! Python以为上位的时机到了
Spring来了… 它改变了Java的开发方式(IoC和AOP) …
Apache所有的产物几乎都是适用于Java。
2017年 AI(人工智能)概念吵得火热 都是用C的内容开发的,但是本身C开发的就够复杂了,还要用C去编写整个系统。Python(胶水语言),科学家们比较喜欢了,Python计算库非常多…
微服务(SOA衍变) Dubbox + Zookeeper等 | Spring Cloud
[外链图片转存失败(img-qY7ieFgj-1564302006762)(assets/1564276165890.png)]
Spring框架是一个轻量级的,不侵入的开源框架!它的核心是IoC和AOP,整套生命体系中对于Java开发来讲都做了很好的支持,并且它在兼容性方面比较强,不独占,进行联合,对于第三方框架支持比较好。
核心Jar:Beans、Core、Context、SpringExpressionLanguage
Spring面向bean编程。
[外链图片转存失败(img-fTCIG83V-1564302006769)(assets/1564276456486.png)]
1.2 Spring基础环境使用
4.x系列的稳定版(RELEASE)
-
导包:核心beans 、 core 、context、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 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> </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
// 1.加载配置文件 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); // 2.从Spring容器中获取HelloSpring的对象 然后使用即可 HelloSpring helloSpring = ac.getBean(HelloSpring.class); helloSpring.helloSpring();
2. 理解并掌握Spring IoC的原理和配置
IoC: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 IoC中的DI来帮助你实现依赖的注入,按名称、按类型注入…。
极大的降低了我们应用的耦合性,同样增强了程序的扩展性和维护性,Spring侵入式极低。
3. 理解并掌握Spring AOP的原理和配置
AOP(Aspect Oriented Programming)面向切面编程
Spring侵入式比较低
的特点。
public class UserServiceImpl implements UserService{
private Logger logger = xxx;
public void addUser(User user){
logger.info("正在添加....添加信息为:"+xxxx);
Connection conn = xxx;
try{
// .....
conn.commit();
logger.info("添加成功!...");
}catch(Exception e){
conn.rollback();
logger.error("添加失败!异常信息为:"+xxx);
}
}
}
代理模式实现增强处理!
[外链图片转存失败(img-8CIukEyE-1564302006774)(assets/1564287212888.png)]
**AOP原理:**将复杂的需求分解出不同方面,将散布在系统中的公共功能集中解决
采用代理机制组装起来运行,在不改变原程序的基础上对代码段进行增强处理,增加新的功能。
所谓面向切面编程,是一种通过预编译和运行期动态代理的方式实现在不修改源代码的情况下给程序动态添加功能的技术。
需求:给应用程序添加日志记录!
实现增强处理!
[外链图片转存中…(img-8CIukEyE-1564302006774)]
**AOP原理:**将复杂的需求分解出不同方面,将散布在系统中的公共功能集中解决
采用代理机制组装起来运行,在不改变原程序的基础上对代码段进行增强处理,增加新的功能。
所谓面向切面编程,是一种通过预编译和运行期动态代理的方式实现在不修改源代码的情况下给程序动态添加功能的技术。
需求:给应用程序添加日志记录!
[外链图片转存失败(img-GDdGkSVr-1564302006776)(assets/1564288626764.png)]