一.Spring框架简介及官方压缩包目录介绍
主要发明人,Rod Johnson。轮子理论推崇者,轮子理论是不用重复发明轮子,IT行业可以直接用写好的代码。Spring框架宗旨是不重复发明技术,让原有技术使用起来更加方便。
Spring几大核心功能:IoC/DI,控制反转/依赖注入。AOP,面向切面编程。声明式事务。
Spring框架runtime:①test,spring提供的测试功能。②Core Container,核心容器,Spring启动最基本的条件。Beans,负责创建类对象并管理对象。Core,核心类。Context,上下文参数,获取外部资源或管理注解等。SpEI,expression.jar。③AOP,实现aop功能需要依赖。④Aspects,切面AOP依赖的包。⑤Data Access/Integration,spring封装数据访问层相关内容。JDBC,Spring对JDBC封装后的代码。ORM,封装了持久层框架的代码,例如Hibernate。transactions,对应spring-tx.jar,声明式事务使用。⑥WEB,需要spring完成web相关功能时需要。如tomcat加载spring配置文件时需要有spring-web包。
Spring框架中最重要的概念:容器(Container),把Spring当做一个大容器。BeanFactory接口(老版本),新版本中ApplicationContext接口,是BeanFactory子接口,BeanFactory的功能在ApplicationContext中都有。
从Spring3开始把Spring框架的功能拆分成多个jar,Spring2及之前就一个jar。
二.Spring环境搭建
①导入jar包,四个核心包和一个日志包(commons-logging)
②在src下新建applicationContext.xml,文件名称和路径自定义,记住Spring容器ApplicationContext,applicationContext.xml配置的信息最终存储到了ApplicationContext容器中,spring配置文件时基于schema,schema文件扩展名.xsd,把schema理解成DTD的升级版,比DTD具备更好的扩展性,每次引入一个xsd文件是一个namespace(xmlns),配置文件中只需要引入基本schema,通过<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">
<!-- id表示获取到对象标识
class 创建哪个类的对象
-->
<bean id="peo" class="com.bjsxt.pojo.People"/>
</beans>
③编写测试方法 ,getBean("<bean>标签id值",返回值类型);如果没有第二个参数,默认是Object,getBeanDefinitionNames(),Spring容器中目前所有管理的所有的对象。
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
People people = ac.getBean("peo",People.class);
三.Spring IoC
中文名称,控制反转。英文名称,Inversion of Control。
IoC是什么?IoC完成的事情原先由程序员主动通过new实例化对象事情,转交给Spring负责。控制反转中控制指的是控制类的对象。控制反转中反转指的是转交给Spring负责。IoC最大的作用是解耦,程序员不需要管理对象,解除了对象管理和程序员之间的耦合。
Spring创建对象的三种方式
通过构造方法创建:默认无参构造创建,有参构造创建需要明确配置,需要在类中提供有参-构造方法,在applicationContext.xml中设置调用哪个构造方法创建对象,如果设定的条件匹配多个构造方法执行最后的构造方法,index参数的索引从0开始,name参数名,type类型(区分开关键字和封装类int和Integer)。
<!-- id表示获取到对象标识
class 创建哪个类的对象
-->
<bean id="peo" class="com.bjsxt.pojo.People">
<!-- ref引用另一个bean value 基本数据类型或String等 -->
<constructor-arg index="0" name="id" type="int" value="123"></constructor-arg>
<constructor-arg index="1" name="name" type="java.lang.String" value="张三"></constructor-arg>
</bean>
实例工厂:工厂设计模式,帮助创建类对象,一个工厂可以生产多个对象。实例工厂,必须先创建工厂才能生产对象。实现步骤,①必须有一个实例工厂。②在applicationContext.xml中配置工厂对象和需要创建的对象。
public class PeopleFactory {
public People newInstance(){
return new People(1,"测试");
}
}
<bean id="factory" class="com.bjsxt.pojo.PeopleFactory"></bean>
<bean id="peo" factory-bean="factory" factory-method="newInstance"></bean>
静态工厂:不需要创建工厂,快速创建对象。实现步骤,①编写一个静态工厂(在方法上添加static)。
public class PeopleFactory {
public static People newInstance(){
return new People(1,"测试");
}
}
<bean id="peo" class="com.bjsxt.pojo.PeopleFactory" factory-method="newInstance"></bean>
如何给Bean的属性赋值(注入):①通过构造方法设置值。②设置注入(通过set方法)。
<?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 id="peo" class="com.bjsxt.pojo.People">
<!-- 如果属性是基本数据类型或String等简单类型 -->
<property name="id" value="123"></property>
<!-- 和上面等效 -->
<property name="name">
<value>zhangsan</value>
</property>
<!-- 如果属性是Set<?> -->
<property name="sets">
<set>
<value>1</value>
<value>2</value>
<value>3</value>
<value>4</value>
</set>
</property>
<!-- 如果list中只有一个值 -->
<property name="list" value="1">
</property>
<!-- 如果数组中只有一个值,可以直接通过value属性赋值 -->
<property name="strs" >
<array>
<value>1</value>
<value>2</value>
<value>3</value>
</array>
</property>
<!-- 如果属性是map -->
<property name="map">
<map>
<entry key="a" value="b" >
</entry>
<entry key="c" value="d" >
</entry>
</map>
</property>
<!-- 如果属性是Properties类型 -->
<property name="demo">
<props>
<prop key="key">value</prop>
<prop key="key1">value1</prop>
</props>
</property>
</bean>
</beans>
DI:中文名称,依赖注入;英文名称,Dependency Injection。DI是和IoC是一样的,当一个类(A)中需要依赖另一个类()对象时,把B赋值给A的过程就叫做依赖注入。
<?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 id="peo" class="com.bjsxt.pojo.People">
<property name="desk" ref="desk"></property>
</bean>
<bean id="desk" class="com.bjsxt.pojo.Desk">
<property name="id" value="1"></property>
<property name="price" value="12"></property>
</bean>
</beans>
四.使用Spring简化Mybatis
①导入mybatis所有jar和spring基本包,spring-jdbc,spring-tx,spring-aop,spring整合mybatis的包等。②编写spring配置文件applicationContext.xml。③正常编写pojo,编写mapper包下时必须使用接口绑定方案或注解方案(必须有接口),正常编写Service接口和Service实现类,需要在Service实现类中声明Mapper接口对象,并生成get/set方法,spring无法管理Servlet
<?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">
<!-- 数据源封装类 .数据源:获取数据库连接,spring-jdbc.jar中-->
<bean id="dataSouce" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/ssm"></property>
<property name="username" value="root"></property>
<property name="password" value="smallming"></property>
</bean>
<!-- 创建SqlSessionFactory对象 -->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接信息来源于dataSource -->
<property name="dataSource" ref="dataSouce"></property>
</bean>
<!-- 扫描器相当于mybatis.xml中 mappers下package标签,扫描com.bjsxt.mapper包后会给对应接口创建对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 要扫描哪个包 -->
<property name="basePackage" value="com.bjsxt.mapper"></property>
<!-- 和factory产生关系 -->
<property name="sqlSessionFactory" ref="factory"></property>
</bean>
<!-- 由spring管理service实现类 -->
<bean id="airportService" class="com.bjsxt.service.impl.AirportServiceImpl">
<property name="airportMapper" ref="airportMapper"></property>
</bean>
</beans>
public class AirportServiceImpl implements AirportService {
private AirportMapper airportMapper;
public AirportMapper getAirportMapper() {
return airportMapper;
}
public void setAirportMapper(AirportMapper airportMapper) {
this.airportMapper = airportMapper;
}
@Override
public List<Airport> show() {
return airportMapper.selAll();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- 上下文参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- spring配置文件 -->
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 封装了一个监听器,帮助加载Spring的配置文件爱 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
@WebServlet("/airport")
public class AirportServlet extends HttpServlet{
private AirportService airportService;
@Override
public void init() throws ServletException {
//对service实例化
//ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//spring和web整合后所有信息都存放在webApplicationContext
ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
airportService=ac.getBean("airportService",AirportServiceImpl.class);
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("list", airportService.show());
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
}