@TOCSpring总结(1)
前言
原来的网站开发采用的是Tomcat+Mysql+JSP技术选型,用来开发小型网站尚且够用,但面对大型网站就有些繁杂。
后来,对重复部分进行工具抽取,渐渐发展起各种框架,用来减少开发者的重复操作。现在为止,java网站开发中最为流行的框架是SSM框架,即Spring+SpringMVC+Mybatis技术选型型。其中,Mybatis优化了mysql操作;Spring优化了逻辑控制层操作。
一、Spring是如何产生的?
Spring就是对原来繁杂的代码进行重复代码抽取,同时对一些操作优化,逐渐成型的。Spring最为核心的功能是IOC和AOP,即控制反转和面向切面编程。其中,控制反转的核心是依赖注入(DI)。
我们进行网站开发的过程中,写一个类或方法的时候,不可避免地会引用其他的类、方法或数据,也就是说,我们的现在正在编写的类或方法,必须依赖在我们引用的类、方法或数据的基础之上。这就是程序之间的依赖关系。
IOC控制反转,即通过xml配置或者注解的方式降低类、方法等之间的相互依赖关系。依赖注入,通俗地讲,就是当我们编写一个类,但此时需要引用另一个类的时候,我们不是直接引用,而是通过读取xml配置文件或者注解的方式向Spring发起请求,由Spring为我们提供那个需要引用的类。
二、相比原来,框架优化了什么?
1.原来的程序写法
我们来看一个最简单的例子,代码如下:
package com.lhp;
import java.sql.*;
public class SinpleExample {
public static void main(String[] args) {
//加载mysql驱动
/**
* 加载mysql驱动时可以有两种方式,一种是new一个驱动对象;一种是利用反射加载驱动。
* 若对应的jdbc驱动不存在,则第一种方式会出现编译期错误;第二种方式会出现运行期错误。
* 一般情况下,不使用new方式创建对象
* 第二种方式中,反射需要的是一个代表驱动路径的字符串。若我们不在程序里写死这个字符串,而是把字符串写进xml配置文件中,
* 需要的时候再读取出来,则是初级版本的spring。
*/
try {
//new一个驱动对象
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
//利用反射加载驱动类
//Class.forName("com.mysql.cj.jdbc.Driver");
} catch (SQLException e) {
e.printStackTrace();
} //catch (ClassNotFoundException e) {
//e.printStackTrace();
//}
//连接数据库
String URL = "jdbc:mysql://localhost:3306/sinpleexamplesql?useSSL=false&PublicallowkeyRetrieval=true";
String user = "root";
String password = "root";
Connection conn = null;
try {
conn = DriverManager.getConnection(URL,user,password);
} catch (SQLException e) {
e.printStackTrace();
}
//执行查询
Statement st = null;
ResultSet rs = null;
try {
st = conn.createStatement();
String sql = "select * from simpleexamplesql";
rs = st.executeQuery(sql);
//遍历结果集
while (rs.next()) {
int id = rs.getInt("id");
String country = rs.getString("country");
System.out.println("id: " + id);
System.out.println("country: " + country);
}
} catch (SQLException e) {
e.printStackTrace();
}
//关闭连接
try {
rs.close();
st.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
输出结果:
这个程序的运行依赖于mysql-jdbc驱动的存在,这就是类之间的依赖关系。一个程序里面的依赖关系越复杂,则后期的修改和维护就越难。降低程序的依赖关系,这就是IOC的作用。
程序中,将原来的new一个驱动改为反射,将有可能的编译期错误推迟到运行期,就是一种降低依赖关系。spring中,通过配置的方式,将反射中的代表驱动的字符串不再写死,而是需要的时候通过配置读取,则是控制反转。
什么意思呢?
我们需要引用一个类时,直接引用,那么这个引用的控制权在编写者手中;当我们采用spring时,我们向spring发起请求,读取xml配置文件,spring再将类提供给我们,这个引用的控制权就在spring手中。这就是控制反转。
那么,怎么通过读取配置文件来降低依赖关系呢?
2.xml配置文件
新建spring项目,spring中最简单的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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="jdbcDriver" class="com.mysql.cj.jdbc.Driver"></bean>
</beans>
pom.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
</dependencies>
</project>
查询程序如下:
package com.lhp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.sql.*;
public class springDemo {
public static void main(String[] args) {
//获取核心容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//根据id获取mysql驱动对象
ac.getBean("jdbcDriver");
//连接数据库
String URL = "jdbc:mysql://localhost:3306/sinpleexamplesql?useSSL=false&PublicallowkeyRetrieval=true";
String user = "root";
String password = "root";
Connection conn = null;
try {
conn = DriverManager.getConnection(URL,user,password);
} catch (SQLException e) {
e.printStackTrace();
}
//执行查询
Statement st = null;
ResultSet rs = null;
try {
st = conn.createStatement();
String sql = "select * from simpleexamplesql";
rs = st.executeQuery(sql);
//遍历结果集
while (rs.next()) {
int id = rs.getInt("id");
String country = rs.getString("country");
System.out.println("id: " + id);
System.out.println("country: " + country);
}
} catch (SQLException e) {
e.printStackTrace();
}
//关闭连接
try {
rs.close();
st.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
总结
这篇文章主要介绍了什么叫IOC,即通过xml配置或者注解来降低程序间的依赖关系。