Spring框架學習筆記02

1.使用註解配置spring

(1)步驟

1.導包(4+2+spring-aop)

這裏寫圖片描述

2.爲主配置文件引入新的命名空間(約束)
3.開啓使用註解代理配置文件

這裏寫圖片描述

4.在類中使用註解完成配置

(2)將對象註冊到容器

@Component("user")
//@Service("user")//service層
//@Controller("user")//web層
//@Repository("user")//dao層
//相當於<bean name="user" class="cn.itheima.bean.User"></bean>
public class User {
}

(3)修改對象的作用範圍

這裏寫圖片描述

(4)值類型注入

1.通過反射的Field賦值,破壞了封裝性

這裏寫圖片描述

2.通過set方法賦值,推薦使用.
@Value("lijisheng")
public void setName(String name) {
    this.name = name;
}

(5)引用類型注入

這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述

(6)初始化|銷燬方法

這裏寫圖片描述

2.STS插件

Eclipse用戶可以安裝STS插件(Spring Tool Suite)可以方便spring配置文件的編輯,MyEclipse自帶

3.spring餘junit整合測試

(1)導包4+2+aop+test

這裏寫圖片描述

(2)配置註解

package cn.itheima.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.itheima.bean.User;

//幫我們創建容器
@RunWith(SpringJUnit4ClassRunner.class)
//指定創建容器時使用哪個配置文件
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
    @Resource(name="user")
    private User u;

    //將名爲user的對象注入到u變量中
}

(3)測試

@Test
public void fun1(){

    System.out.println(u);
} 

4.spring中的aop

(1)aop思想介紹

這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述

(2)spring中的aop概念

這裏寫圖片描述

(3)spring實現aop的原理

1.動態代理(優先)
被代理對象必須要實現接口,才能產生代理對象.如果沒有接口將不能使用動態代理技術
2.cglib代理(沒有接口)
第三方代理技術,cglib代理.可以對任何類生成代理.代理的原理是對目標對象進行繼承代理. 如果目標對象被final修飾.那麼該類無法被cglib代理.

(4)aop名詞學習

這裏寫圖片描述

5.spring中的aop演示

(1)步驟(xml配置)

1.導包4+2

這裏寫圖片描述
2.準備目標對象

package cn.itheima.service.impl;

import cn.itheima.service.UserService;

public class UserServiceImpl implements UserService{

    public void save() { 
        System.out.println("新增用戶");
    }

    public void delete() {
        System.out.println("刪除用戶");
    }

    public void update() {
        System.out.println("修改用戶");
    }

    public void select() {
        System.out.println("查詢用戶");
    }

}
3.準備通知
package cn.itheima.spring_aop;

import org.aspectj.lang.ProceedingJoinPoint;


//通知類
public class MyAdvice {
    //前置通知(目標方法運行之前調用)
    //後置通知(如果出現異常不會調用,目標方法運行之後調用)
    //環繞通知(目標方法之前和之後都會調用)
    //異常攔截通知(如果出現異常就會出現)
    //後置通知(無論是否出現異常都會調用)

    //前置通知
    public void before(){
        System.out.println("這是一個前置通知!");
    }

    //後置通知
    public void afterReturning(){
        System.out.println("這是一個後置通知,出現異常不會調用");
    }

    //環繞通知
    public Object around(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("這是環繞通知之前的部分");
        Object obj = pjp.proceed();//調用目標方法
        System.out.println("這是環繞通知之後的部分");
        return obj;

    }

    //異常通知
    public void afterExecption(){
        System.out.println("出事了,出現異常了");
    }

    //後置通知
    public void after(){
        System.out.println("這是後置通知,出現異常也會調用");
    }

}
4.配置進行織入,將通知織入目標對象中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">

<!-- 準備工作:準備導入aop約束 -->
    <!--  1.配置目標對象 -->
    <bean name="userService" class="cn.itheima.service.impl.UserServiceImpl"></bean>
    <!--  2.配置通知對象 -->
    <bean name="myAdvice" class="cn.itheima.spring_aop.MyAdvice"></bean>
    <!--  3.將通知織入目標對象 -->
    <aop:config>
        <!-- 配置切入點
             public void cn.itheima.service.impl.UserServiceImpl.save()
             void cn.itheima.service.impl.UserServiceImpl.save()
             * cn.itheima.service.impl.UserServiceImpl.*(..) 
             * cn.itheima.service.impl.*ServiceImpl.*(..)/* cn.itheima.service.impl..*ServiceImpl.*(..) -->
        <aop:pointcut expression="execution( * cn.itheima.service.impl.*ServiceImpl.*(..))" id="pc"/>
        <aop:aspect ref="myAdvice">
            <!-- 指定名爲before的方法爲前置方法 ,切入到pc中-->
            <aop:before method="before" pointcut-ref="pc"/>
            <!-- 後置 -->
            <aop:after-returning method="afterReturning" pointcut-ref="pc"/>
            <!-- 環繞 -->
            <aop:around method="around" pointcut-ref="pc"/>
            <!-- 異常攔截 -->
            <aop:after-throwing method="afterExecption" pointcut-ref="pc"/>
            <!-- 後置 -->
            <aop:after method="after" pointcut-ref="pc"/>
        </aop:aspect>
    </aop:config>

</beans>

(2)步驟(註解配置)

1.導包4+2
2.準備目標對象
3.準備通知
4.配置進行織入,將通知織入目標對象中
<!-- 準備工作:準備導入aop約束 -->
<!--  1.配置目標對象 -->
<bean name="userService" class="cn.itheima.service.impl.UserServiceImpl"></bean>
<!--  2.配置通知對象 -->
<bean name="myAdvice" class="cn.itheima.spring_aop.MyAdvice"></bean>
package cn.itheima.e_annotation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;


//通知類
@Aspect
//表示該類是一個通知類
public class MyAdvice {
    //前置通知(目標方法運行之前調用)
    //後置通知(如果出現異常不會調用,目標方法運行之後調用)
    //環繞通知(目標方法之前和之後都會調用)
    //異常攔截通知(如果出現異常就會出現)
    //後置通知(無論是否出現異常都會調用)

    @Pointcut(value = "execution( * cn.itheima.service.impl.*ServiceImpl.*(..))")
    public void pc(){}

    @Before("MyAdvice.pc()")
    //指定該方法是前置通知,並指定切入點
    public void before(){
        System.out.println("這是一個前置通知!");
    }

    //後置通知
    @AfterReturning("MyAdvice.pc()")
    public void afterReturning(){
        System.out.println("這是一個後置通知,出現異常不會調用");
    }

    //環繞通知
    @Around("MyAdvice.pc()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("這是環繞通知之前的部分");
        Object obj = pjp.proceed();//調用目標方法
        System.out.println("這是環繞通知之後的部分");
        return obj;

    }

    //異常通知
    @AfterThrowing("MyAdvice.pc()")
    public void afterExecption(){
        System.out.println("出事了,出現異常了");
    }

    //後置通知
    @After("MyAdvice.pc()")
    public void after(){
        System.out.println("這是後置通知,出現異常也會調用");
    }

}
發佈了63 篇原創文章 · 獲贊 2 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章