itcast-spring

   

 

 

 

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

 

介绍

 

Spring搭建

 

 

 约束引入注意事项

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

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

添加cata刚才的添加的scheme

 

Spring 概念

Ioc  di

 

 applicationContext&BeanFactory

 

 

spring配置详解

bean元素

spring与strut2结合  scope选择多例

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

    

 

 

  factory-method

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

 

 spring属性注入

 

index   type  value 定义参数

基于其他bean的注入

 

 

 

 

 

 

 

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

 

 

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

 

 

使用注解配置spring

 

 

 

 命名空间  前缀需要

 

 

 

 

 

 

spring中的aop

 

结论概念:动态代理代理对象和目标对象实现了同个接口   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代理(了解)

 

 

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

 

 

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

 

 

 

pointcut注解  可以解决重复execution

 

 

 

 spring整合jdbc

                

                  

                      

                      

                    

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

                              

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

 

spring 中aop事务

      

 

   

        

    

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

 

 

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

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

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

 

 

 编码式了解

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

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

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

 

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

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

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

 

posted on 2018-12-06 17:33  打酱油的地方  阅读(405)  评论(0编辑  收藏  举报

导航