1.spring开发步骤(IOC(控制反转)+DI(依赖注入)+自动扫描机制)
高内聚就是说相关度比较高的部分尽可能的集中,不要分散
低耦合就是说两个相关的模块尽可以能把依赖的部分降低到最小,不要让两个系统产生强依赖
一、spring是什么
百度:Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。
spring是一个开源框架,为了解决企业应用开发的复杂性而创建的,但是现在已经不止应用于企业应用
是一个轻量级的控制控制反转(IOC)和面向切面(AOP)的容器框架
–从大小与开销两方面而言Spring都是轻量的
–通过控制反转(IOC)的技术达到松耦合的目的
–提供了面向切面编程的丰富支持,允许通过分离应用的业务逻辑与系统服务进行内聚性的开发
–包含并管理应用对象的配置和生命周期,这个意义上是一个容器
–将简单的组件配置、组合成为复杂的应用,这个意义上就是框架
适用范围
–构建企业应用(SpringMVC+Spring+Hibernate/ibatis)
–单独适用Bean容器(Bean管理)
–单独适用AOP进行切面处理
–其他的Spring功能,如对消息的支持等
–在互联网中的应用。。
二、开发步骤
1.导入Spring3.2.9所必须的jar包:
2.创建Spring配置文件,如spring.xml:
//版本问题-3.0
<?xml version="1.0" encoding="UTF-8"?>
<!--
id:唯一标识(自定义)。
class:需要实例化的类,完整限定名(包名.类名)。
此处调用类的默认构造函数进行实例化。
-->
<bean id="student" class="com.pojo.Student"/>
</beans>
3.创建ApplicationContext(上下文)对象,如:
ApplicationContext context = new ClassPathXmlApplicationContext("config/spring.xml");
4.通过ApplicationContext对象调用getBean方法获取对象实例,如:
Student student = context.getBean("student", Student.class); // 3.0之后加入的方式
Student student = (Student)context.getBean("student"); // 3.0之前方式
三、
https://blog.youkuaiyun.com/qq_22654611/article/details/52606960
IOC(Inversion of Control,控制反转):应用本身不负责依赖对象的创建及维护,而是由spring容器负责。
控制反转(实例化Bean)的3种方式:
1. 使用类的构造函数实例化(常用),如:
<bean id="student" class="com.pojo.Student"/>
2. 使用静态工厂方法实例化,如:
<bean id="singleton" class="com.pojo.Singleton" factory-method="createInstance"/>
3. 使用实例工厂方法实例化,如:
<bean id="factory" class="com.pojo.StudentFactory"/>
<bean id="student" factory-bean="factory" factory-method="createInstance"/>
DI(Dependency Injection依赖注入):在运行期由spring容器动态的将依赖对象注入到应用中。
依赖注入的3种方式:
1. 使用构造函数注入,如:
<bean id="student" class="com.pojo.Student">
<!-- 根据参数名注入
<constructor-arg name="name" value="zw"/>
<constructor-arg name="age" value="18"/>
-->
<!-- 根据参数索引注入
<constructor-arg index="0" value="zw"/>
<constructor-arg index="1" value="19"/>
-->
<!-- 根据参数类型注入
<constructor-arg type="String" value="zw"/>
<constructor-arg type="int" value="20"/>
-->
</bean>
2. 使用属性的setter方法注入(常用),如:
<bean id="student" class="com.pojo.Student">
<!-- name属性:实体中的属性名 -->
<property name="name" value="zw"/>
<property name="age" value="21"/>
</bean>
<bean id="studentDao" class="com.dao. StudentDao"/>
<bean id="studentService" class="com.service.StudentService">
<!-- ref属性:引用其他bean的id属性值 -->
<property name="dao" ref="studentDao"/>
</bean>
3. 使用注解方式注入,@Autowired或@Resource两种方式(常用)。
属性和方法
@Autowired:按类型注入依赖对象,可注解在属性或方法上。
@Resource:默认按名称注入,若找不到对应名称时会按类型注入,可注解在属性或方法上(常用)。
使用注解方式注入依赖对象需在Spring的配置文件中添加如下配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解功能-->
<context:annotation-config/>
</beans>
如:
<bean id="studentDao" class="com.dao.StudentDao"/>
<bean id="studentService" class="com.service.StudentService"/>
@Resource private StudentDao studentDao;
Bean的作用域(scope属性)有5种:
singleton:单例,默认值(常用)。
如:<bean id="student" class="com.pojo.Student" scope="singleton"/>
prototype:每次创建新实例(常用)。
如:<bean id="student" class="com.pojo.Student" scope="prototype"/>
request:创建的实例在request范围内有效(web)。
session:创建的实例在session范围内有效(web)。
globalSession:与Portlet容器配合使用。
自动扫描机制:在类路径下寻找标注了@Component、@Service、@Controller、@Repository注解的类,并把这些类纳入Spring容器中管理,它的作用与在xml文件中使用<bean/>标签配置是一样的,id属性值默认为类名的首字母小写。
要使用自动扫描机制,需在配置文件中添加如下配置:
<!--
component-scan:开启扫描指定包及子包下的注解标识。
base-package:指定包名,多个包之间用逗号分隔。
-->
<context:component-scan base-package="com.*"/>
常用在类上的5个注解:
@Component:泛指组件,当组件不好归类时可以使用该注解。
@Service:用于标注业务层组件。//service包
@Controller:用于标注控制层组件,如Action。//web
@Repository:用于标注数据访问组件,即DAO组件。//dao
@Scope:标注Bean的作用域。
2. 框架+ApplicationContext介绍
一、什么是框架
1、维基百科:软件框架,通常指的是为了实现某个业界标准或完成特定基本任务的软件组件规范,也指为了实现某个软件组件规范是,提供规范锁要求之基础功能的软件产品
框架就是制定一套规范或者规则(思想),大家(程序员)在该规范或者规则(思想)下工作。或者说就是使用别人搭好的舞台,你来做表演。
2、框架的特点
–半成品
–封装了特点的处理流程和控制逻辑
–成熟的、不断升级改进的软件
3、框架与类库的区别
–框架一般是封装了逻辑、高类聚的,类库则是松散的工具组合(可以组装成框架)
–框架专注于某一领域,类库则是更通用的
4、为什么使用框架
–软件系统日趋复杂
–重用度高,开发效率和质量提高
–软件设计人员要专注于对领域的了解,使需求分析更充分
–易于上手,快速解决问题
二、ApplicationContext介绍
ApplicationContext类体系结构
ApplicationContext的主要实现类是ClassPathXmlApplicationContext和FileSystemXmlApplicationContext,前者默认从类路径加载配置文件,后者默认从文件系统中装载配置文件
ApplicationContext context = new ClassPathXmlApplicationContext(“config/spring.xml”);
3.AOP(面向切面编程)
一、
AOP(面向切面编程):即在运行期动态的将功能切入到指定类的指定方法。常用于日志记录、安全控制、事务处理、异常处理等。
使用AOP需在配置文件中添加如下配置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
或
使用AOP注解方式需添加如下配置:
<!-- 启用aspectj注解支持 -->
<aop:aspectj-autoproxy/>
<!-- 包扫描 -->
<context:component-scan base-package="com.*"/>
AOP所需jar包:
相关包下载地址:
http://repo1.maven.org/maven2/org/aspectj/aspectjweaver/
http://repo1.maven.org/maven2/aopalliance/aopalliance/
AOP应用步骤:
1. 定义目标(Target),即具体的业务类。
2. 定义切面(Aspect),即包含切面方法的类。
3. 定义切入点(Pointcut),即执行切面方法的时机。
4. 定义通知(Advice), 即指定具体的切面方法。
常用通知(Advice)类型:
1. 前置通知(Before):在目标方法之前执行。before
2. 后置通知(AfterReturning):在目标方法之后执行。
3. 环绕通知(Around):在目标方法前后分别执行。arround
4. 异常通知(AfterThrowing):在目标方法异常后执行,类似于执行catch代码块。
5. 最终通知(After):类似于执行finally代码块。
切入点(pointcut)语法:execution([修饰符] 返回类型 [包名.类名.]方法名(参数)[异常])。
注:其中[]部分可省略,其余部分必须定义。
参数:可使用通配符*和..,其中*表示任意类型的单个参数,而..表示任意类型任意个数的参数。如参数为具体类型且属于java.lang包下则直接使用类名,否则需用类的完整限定名。
[包名.类名.]方法名:可使用通配符.*和..*,其中.*表示当前包下的所有类的所有方法,..*表示当前包及其子孙包下的所有类的所有方法。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--定义目标(Target) -->
<bean id="studentService" class="com.service.StudentService"></bean>
<!--定义切面(Aspect)-->
<bean id="sleepAscept" class="com.ascept.SleepAscept"></bean>
<bean id="eatAscept" class="com.ascept.EatAscept"></bean>
<!--切面 -->
<aop:config>
<!-- 切入点 -->
<aop:pointcut expression="execution(public void com.service.StudentService.sleep())" id="sleepPointcut"></aop:pointcut>
<aop:pointcut expression="execution(public void com.service.StudentService.eat())" id="eatPointcut"></aop:pointcut>
<!-- -->
<aop:aspect ref="sleepAscept">
<!--前置通知(sleepAspect中的方法在sleepPointcut之前执行-->
<aop:before method="sleepBefore" pointcut-ref="sleepPointcut"/>
<!-- 后置通知 -->
<aop:after-returning method="sleepAfter" pointcut-ref="sleepPointcut"/>
</aop:aspect>
<aop:aspect ref="eatAscept">
<!--环绕通知(eatAscept中的方法在sleepPointcut前后执行-->
<aop:around method="eatArround" pointcut-ref="eatPointcut"/>
<!--异常通知 throwing中的参数名要与eatAfterThrowing方法中抛出的异常保持一致-->
<aop:after-throwing method="eatAfterThrowing" pointcut-ref="eatPointcut" throwing="ex"/>
<!--最终通知 顺序不一定最后,根据配置顺序来-->
<aop:after method="eatAfter" pointcut-ref="eatPointcut"/>
</aop:aspect>
</aop:config>
</beans>
4.Spring+AOP+JDBC事务管理
Spring+AOP+JDBC事务管理:
事务:是应用程序中一系列严密的操作,在一个事务中的所有操作要么全部执行成功提交(commit),要么全部回滚(rollback),也就是事务具有原子性。原子性、一致性、隔离性和持久性为事务的四个特点,在JDBC中每个事务对应一个连接(Connection)对象。
开发步骤:
一.导入所需jar包:
二.在配置文件中添加如下配置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>
三.配置数据源:
<!-- 加载数据库配置文件 -->
<context:property-placeholder location="classpath:config/db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.pwd}"/>
</bean>
四.通过AOP配置事务,事务一般配置在业务层上
<!--
配置声明式事务步骤:
1.配置事务管理器
2.配置通知(Advice)
3.配置切面(AOP),事务一般配置在业务层
-->
<!-- 1.配置事务管理器 -->
<bean id="txManager(自定义)"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager(固定)">
<property name="dataSource(固定)" ref="dataSource(引用数据源配置的id属性值)"/>
</bean>
<!-- 2.配置通知(Advice) -->
<tx:advice id="txAdvice(自定义)" transaction-manager="txManager(引用事务管理器配置的id属性值)">
<tx:attributes>
<tx:method name="query*" read-only="true(设置为只读事务,提高性能)"/>
<tx:method name="delete*(设置给哪些方法名加上事务管理)"/>
</tx:attributes>
</tx:advice>
<!-- 3.配置切面(AOP),事务一般配置在业务层 -->
<aop:config>
<aop:advisor advice-ref="txAdvice(引用通知配置的id属性值)"
pointcut="execution(* com.service.ShopService.*(..))"/>
</aop:config>
使用事务注解方式需按如下步骤:
1.在配置文件中添加如下配置:
<!--
配置注解式事务步骤:
1.配置事务管理器
2.启用事务注解,事务一般配置在业务层
-->
<!-- 1.配置事务管理器 -->
<bean id="txManager(自定义)"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource(固定)" ref="dataSource(引用数据源配置的id属性值)"/>
</bean>
<!-- 2.启用事务注解,事务一般配置在业务层 -->
<tx:annotation-driven transaction-manager="txManager(引用事务管理器配置的id属性值)"/>
2.在类或方法上添加事务注解:@Transactional。
该注解添加在类上时,表示该类下的所有公共方法都执行事务操作;添加在方法上时表示该方法执行事务操作,如:
@Transactional
public class ShopService
@Transactional(readOnly=true)
public List<Map<String, Object>> queryAll()
5.Spring+Mybatis整合步骤
Spring+Mybatis整合步骤:
mybatis-spring-1.3.1.jar下载地址:
http://mvnrepository.com/artifact/org.mybatis/mybatis-spring
一.导入sm整合所需jar包:
二.工程结构如图:
ShopMapper.java:映射器(Mapper)接口。
ShopMapper.xml:SQL映射文件。
<?xml version="1.0" encoding="UTF-8"?>
<mapper namespace="com.dao.ShopMapper">
<select id="queryAll" resultType="shop">
select * from shop
</select>
<delete id="delete">
delete from shop where shopid=#{sid}
</delete>
</mapper>
Shop.java:数据库表映射的对应实体类。
ShopService.java:业务服务层。
db.properties:数据库配置文件。
jdbc.driver=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.user=meitao
jdbc.pwd=123
mybatis.xml:mybatis配置文件。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 控制台输出SQL -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
</configuration>
spring-bean.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="shopService" class="com.service.ShopService">
<property name="shopMapper" ref="shopMapper"/>
</bean>
</beans>
spring-dao.xml:配置mybatis。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 加载数据库配置文件 -->
<context:property-placeholder location="classpath:config/db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.pwd}"/>
</bean>
<!-- 配置SqlSessionFactory -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean(固定)">
<!-- 指定数据源 -->
<property name="dataSource(固定)" ref="dataSource(配置数据源的id属性值)"/>
<!-- 指定别名,多个包之间用逗号分隔 -->
<property name="typeAliasesPackage(固定)" value="com.pojo"/>
<!-- 指定mybatis的全局配置文件 -->
<property name="configLocation(固定)" value="classpath:config/mybatis.xml"/>
</bean>
<!-- 配置映射器(Mapper) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer(固定)">
<!-- 指定映射器接口,多个包之间用逗号分隔,
代理类的实例名默认为接口名的首字母小写 -->
<property name="basePackage(固定)" value="com.dao"/>
</bean>
</beans>
spring-tx.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--
配置声明式事务步骤:
1.配置事务管理器
2.配置通知(Advice)
3.配置切面(AOP),事务一般配置在业务层
-->
<!-- 1.配置事务管理器 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 2.配置通知(Advice) -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="query*" read-only="true"/>
<tx:method name="delete*"/>
</tx:attributes>
</tx:advice>
<!-- 3.配置切面(AOP),事务一般配置在业务层 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.service.ShopService.*(..))"/>
</aop:config>
</beans>
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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 引用其他配置文件 -->
<import resource="classpath:config/spring-*.xml"/>
</beans>
TestSM.java:junit测试类。