spring框架:
官网: https://spring.io/
下载jar包: https://repo.spring.io/webapp/#/home
artifacts---libs_releases_local---org--springframework---spring--要下载的版本
spring系列包含很多框架,这里的spring 值得是 spring framework 是spring基础框架,核心组件
spring:
是个轻量级框架
基本概念:
1.控制反转(IoC = Inversion of Control)
IoC,用白话来讲,就是由容器控制程序之间的关系,而非传统实现中,由程序代码直接操控。
这也就是所谓“控制反转”的概念所在:控制权由应用代码中转到了外部容器,控制权的转移,是 所谓反转
2.依赖注入(DI = Dependency Injection)
注入方式2种:
1. 设置注入 通过gettersetter方法来注入 必须有gettersetter方法
<bean id="car" class="com.turing.beans.Car">
<property name="name">
<value>宝马730Li 典雅版</value>
</property>
<property name="factory">
<value>德国柏林</value>
</property>
<property name="price">
<value>89.80万</value>
</property>
<property name="type">
<value>豪华车</value>
</property>
</bean>
bean表示实体类,property表示给实体类里面的属性 name 属性名 value 属性值
2. 构造注入 通过构造方法 必须有构造方法
<bean id="engine" class="com.turing.beans.Engine">
<constructor-arg index="0">
<value>EA113</value>
</constructor-arg>
<constructor-arg index="1">
<value>一汽-大众汽车有限公司</value>
</constructor-arg>
<constructor-arg index="2">
<value>125KW</value>
</constructor-arg>
<constructor-arg index="3">
<value>2.0升</value>
</constructor-arg>
</bean>
constructor-arg 表示构造方法中的传入的参数 index 参数位置 从0开始 value 参数值
自动装配: 无需手动配置注入,只需配上自动装配的xml配置或注解配置,AutoWire,属性值会自动注入
配置方式:
1. xml配置
<bean id="car" class="com.turing.beans.Car" autowire="byName">
<property name="name">
<value>宝马730Li 典雅版</value>
</property>
<property name="factory">
<value>德国柏林</value>
</property>
<property name="price">
<value>89.80万</value>
</property>
<property name="type">
<value>豪华车</value>
</property>
</bean>
<bean id="engine" class="com.turing.beans.Engine">
<constructor-arg index="0">
<value>EA113</value>
</constructor-arg>
<constructor-arg index="1">
<value>一汽-大众汽车有限公司</value>
</constructor-arg>
<constructor-arg index="2">
<value>125KW</value>
</constructor-arg>
<constructor-arg index="3">
<value>2.0升</value>
</constructor-arg>
</bean>
属性engine会自动按名称注入
2. 注解方式
public class Car {
private String name; //车名称
private String factory; //生产厂家:奔驰,宝马
private String price; //销售价格
private String type; //车级别:小型车,紧凑型,中型车,SUV,MPV
@Autowired
private Engine engine;
private Bodywork bodywork;
private Underpan underpan;
}
在需要自动装配的属性上面配置注解
默认按类型自动装配
注入方式:
1. byType 按类型 默认按类型自动装配
2. byName 按名称
注入类型:
1. 基本数据类型
2. Bean
3. 集合类型:
<bean id="order" class="com.turing.service.OrderServiceBean">
<!-- 注入List集合-->
<property name="lists">
<list>
<value>lihuoming</value>
</list>
</property>
<!-- 注入Set集合-->
<property name="sets">
<set>
<value>set</value>
</set>
</property>
<!-- 注入Map集合-->
<property name="maps">
<map>
<entry key="lihuoming" value="28"/>
</map>
</property>
<!-- 注入Proeprties集合-->
<property name="properties">
<props>
<prop key="12">sss</prop>
</props>
</property>
</bean>
4. 空字符串"" 和 null 值得是
<bean class="ExampleBean">
<property name="email" value=""/>
</bean>
String value ("")
<bean class="ExampleBean">
<property name="email"><null/></property>
</bean>
exampleBean.setEmail(null).
3. AOP 面向切面编程
基本概念:
aspect(切面):横切性关注点的抽象即为切面,它与类相似,只是二者的关注点不一样,类是对物体特征的抽象,而切面是横切性关注点的抽象
joinpoint(连接点):那些被拦截到的点,在spring中这些点指的是方法,因为spring只支持方法类型的连接点,实际上,joinpoint还可以是field和类构造器
Pointcut(切入点):对那些joinpoint进行拦截的定义
Advice(通知):拦截到joinpoint之后所要做的事情就是通知,通知分为前置通知,后置通知,异常通知,最终通知,环绕通知。
@Aspect
public class Logprint{
@Pointcut("execution(* com.jary.service..*.*(..))");//对com.jary.service包下面的子包以及子包下面的所以方法进行拦截
private void anyMethod(){}//声明一个切入点
@Before("anyMethod")//定义前置通知,在拦截方法执行前执行
public void doAccessCheck(){ System.out.println("前置通知")}
@AfterReturning("anyMethod")//通知都是基于一个切入点定义的方法(anyMethod)展开的
public void doAfterReturning(){ System.out.println("后置通知")}//在拦截方法执行后执行
@After("anyMethod")
public void doAfter(){System.out.println("最终通知")} //在后置通知之后执行
@AfterThrowing("anyMethod")//异常通知,发生异常的时候才会执行
public void doAfterThrowing(){System.out.println("异常通知")}//上图可以看出当异常通知执行时,后置通知将不会被执行,前置通知,最终通知还是会执行
@Around("anyMethod")//环绕通知可以实现上面的所有通知
public Object doBasicProfiling( ProceedingJoinPoint pjp) throw Throwable{
//环绕通知对于权限的设定首选
//if(){//判定用户是否有权限
System.out.println("进入方法");
Object result=pjp.proceed();//固定格式,必须执行此方法
System.out.println("退出方法");
return result;
}
}
Target(目标对象):代理的目标对象
Weave(织入):指将aspect应用到target对象并导致proxy对象创建的过程即是织入。
Introduction(引入):在不修改类代码的前提下,Introduction可以在运行期为类动态的添加一些方法和field。
例子:
<bean id="transactionDemo" class=""/>
<aop:config>
<aop:pointcut expression="execution(* com.yangxin.core.service.*.*.*(..))" id="p1" />
<aop:aspect ref = "transactionDemo">
<aop:before method="startTransaction" pointcut-ref="p1" />
<aop:after-returning method="commitTransaction" pointcut-ref="p1"/>
</aop:aspect>
</aop:config>
spring在web project里面的使用:
在web。xml里面配置:ContextLoaderListener
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
spring与其他框架的整合:
与struts2整合:
思路: 吧struts2框架中的属性service对象 和service层里面dao对象注入进去就行了
与hibernate整合:
思路: hibernate配置文件里面的所有的内容不再有hibernate自己配置,而是全部由spring注入,在spring配置文件里面
包括 数据源---》 SessionFactory-----> 事务-----》 service 层 ----》 业务逻辑层
与mybatis整合:
思路: mybatis配置文件里面的所有的内容不再有mybatis自己配置,而是全部由spring注入,在spring配置文件里面
包括 数据源---》 SqlSessionFactory-----> 事务-----》 service 层 ----》 业务逻辑层