itcast-spring

时间:2022-02-09 16:35:04

  

黑马2014  spring后期 ssh整合后期  讲解不清楚 源码讲解太多   spring重新开始  itcast2016版本

介绍

itcast-spring

itcast-spring

itcast-spring

Spring搭建

itcast-spring

itcast-spring

itcast-spring

约束引入注意事项

导入至eclipse:windows-preference-cata-导入  spring-framework-4.2.4.RELEASE-scheme-beans-最新的版本

itcast-spring

新建xml文件,  <beans>--edit namespace- 添加xsi

itcast-spring

添加cata刚才的添加的scheme

itcast-spring

itcast-spring

Spring 概念

Ioc  di

itcast-spring

itcast-spring

applicationContext&BeanFactory

itcast-spring

itcast-spring

spring配置详解

bean元素

itcast-spring

itcast-spring

spring与strut2结合  scope选择多例

bean init-method属性    方法与对应class中的方法对应

    itcast-spring

itcast-spring factory-method

itcast-spring需要配置2个bean     factory-bean属性  factory-method

spring属性注入

itcast-spring

itcast-spring

itcast-spring

index   type  value 定义参数

itcast-spring

itcast-spring

基于其他bean的注入

itcast-spring

itcast-spring

itcast-spring

itcast-spring

itcast-spring

itcast-spring

itcast-spring导包 4核心+2日志+1web整合

itcast-spring

监听器(设置监听器 指定路径)  让applicationContext 随着servletContext启动而启动,  在servletContext(application)启动时候,创建applicationContext存储在servletContext(application)域中, 因此获得servletContext(application)就能获得applicationContext ,然后getBean

使用注解配置spring

itcast-spring

itcast-spring命名空间  前缀需要

itcast-spring

itcast-spring

itcast-spring

itcast-spring

itcast-spring

itcast-spring

itcast-spring

itcast-spring

spring中的aop

itcast-spring

itcast-spring

itcast-spring

结论概念:动态代理代理对象和目标对象实现了同个接口   cglib代理对象继承了目标对象

手动使用jdk代理  观光代码

package cn.itcast.service;

public interface UserService {
void save();
void delete();
void update();
void find();
}

UserServiceInterface

package cn.itcast.service;

public class UserServiceImpl implements UserService{

    public void save() {
System.out.println("save user");
}
public void delete() {
System.out.println("delete user");
}
public void update() {
System.out.println("update user");
}
public void find() {
System.out.println("find user");
}
}

UserServiceImpl

package cn.itcast.c_proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import cn.itcast.service.UserService;
import cn.itcast.service.UserServiceImpl;
public class UserServiceProxyFactory implements InvocationHandler{
private UserService us;
public UserServiceProxyFactory(UserService us) {
super();
this.us = us;
}
public UserService getUserServiceProxy(){
UserService usProxy = (UserService) Proxy.newProxyInstance(UserServiceProxyFactory.class.getClassLoader(), UserServiceImpl.class.getInterfaces(), this);
return usProxy;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("打开事务");
Object invoke = method.invoke(us, args);
System.out.println("关闭事务");
return invoke;
}
}

UseServiceProxyFactory

package cn.itcast.c_proxy;

import org.junit.Test;

import cn.itcast.service.UserService;
import cn.itcast.service.UserServiceImpl; public class Demo {
@Test
public void fun1(){
UserService us =new UserServiceImpl();
UserServiceProxyFactory userServiceProxyFactory = new UserServiceProxyFactory(us);
UserService userServiceProxy = userServiceProxyFactory.getUserServiceProxy();
userServiceProxy.save();
}
}

Demo

手动使用cglib代理(了解)

itcast-spring

itcast-spring

itcast-spring

itcast-spring

环绕通知唯一特殊 控制目标方法的调用  需要参数

itcast-spring

              itcast-spring把myadvice通知的save方法织入到切点pc中

itcast-spring

itcast-spring

itcast-spring

itcast-springpointcut注解  可以解决重复execution

spring整合jdbc

itcast-spring

itcast-spring

itcast-spring

                itcast-spring

                  itcast-spring

                      itcast-spring

                      itcast-spring

                    itcast-spring

省略掉注入jdbcTemplate ,  jdbcDaoSuport 有set DataSource方法

                              itcast-spring

                                              注意加前缀  可能有关键字冲突

spring 中aop事务

itcast-spring

      itcast-spring

itcast-spring

  itcast-spring

        itcast-spring

    itcast-spring

业务方法之间 平行调用   决定业务方法之间调用,事务应该如何处理

itcast-spring

1   haha如有有事务,hiahia就直接使用haha的事务 ,同在一个事务中     haha如有没有事务,hiahia就开一个事务     99%的情况(默认)

2. 如有有事务就使用   没有就算了

3.如有有事务就使用   没有就抛异常

itcast-spring

编码式了解

itcast-spring

itcast-spring

itcast-spring

相当于spring aop中把通知部分  由原来的 手写通知类  在applicationContext.xml中通过tx:advice配置完成     spring事务效果相当于 spring-aop中的环绕通知

itcast-spring

itcast-spring

少了个步骤需要导入transactionManager   spring中操作事务的核心  依赖DataSource

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"></bean>

itcast-spring

事务通知配置事务管理类和事务属性

transaction-manager="transactionManager"  引入Spring事务管理类

name=save  目标方法    isolation="DEFAULT" propagation="REQUIRED" read-only="false"  事务属性

itcast-spring

itcast-spring

itcast-spring

相关文章