java增強: spring框架

  1. 基本概念:起源,優點
  2. 基本使用
  3. 整合mybatis框架

Spring:  是一個開源的輕量級容器框架(包含並管理應用對象的配置和生命週期,javabean的創建方式可配置爲prototype 或singleton ) 提倡的編程思想是:控制反轉(IoC),   面向切面(AOP)

Spring 的起源:Rod Johnson 在2002年編著的《Expert one on one J2EE design and development》一書中,對Java EE 系統框架臃腫、低效、脫離現實的種種現狀提出了質疑,並因此開發出輕便小巧的interface21框架,也即spring的前身。

part1: 基本使用 (使用xml文件:管理對象創建)

(開發工具: idea + maven )

第一步: 創建java  module, 添加maven支持, 配置pom.xml添加jar包依賴

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>

第二步: 創建包 a, 創建 【接口 + 實現子類】, 在resources目錄下:配置xml 使得spring管理對象的創建

//接口
public interface HelloI {
    void say();
}

//接口--實現類
public class HelloImpl implements HelloI {
   private String name;
    //構造
    public HelloImpl( String b, int a){ System.out.println("String b ,int a"); }
    public HelloImpl(int a, String b){ System.out.println("int a, String b "); }
    public HelloImpl(Integer a, String b){ System.out.println("Integer a, String b ");}
    public HelloImpl(String name) { this.name = name; }
    public HelloImpl() { }

    //一般方法
    public void say() { System.out.println("hello  "+ name); }

    //get,set
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}

在resources目錄下:配置自定義的beans.xml  ( 使得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-4.3.xsd">

    <bean id="helloImpl" class="a.HelloImpl">
        <property name="name" value="tom"/>
    </bean>

    <bean id="helloImpl2" class="a.HelloImpl" scope="prototype">
        <constructor-arg type="int" value="1" ></constructor-arg>
        <constructor-arg type="java.lang.String" value="aa" ></constructor-arg>
        <property name="name" value="tom"/>
    </bean>
    <bean id="helloImpl3" class="a.HelloImpl" scope="prototype">
        <constructor-arg type="java.lang.String" value="aa" index="0"></constructor-arg>
        <constructor-arg type="int" value="1" index="1" ></constructor-arg>
        <property name="name" value="tom"/>
    </bean>
</beans>

第三步: 創建測試類, 驗證對象的創建

@org.junit.Test
    public void t(){

        //創建: 上下文
       ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
       HelloI h= (HelloI) context.getBean("helloImpl");
       h.say();
    }

part2:  以註解的方式創建對象

第一步: 修改接口實現類的定義---添加@Repository註解

@Repository
public class HelloImpl implements HelloI {...}

第二步: 修改自定義的beans.xml文件, 添加--掃描註解的支持

 <!--自動掃描-->
    <context:component-scan base-package="類所在的包名"></context:component-scan>

第三步: 創建測試類,驗證對象的創建

    @org.junit.Test
    public void t2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        
        //getBean: 對象名必須爲類名的小寫,如HelloImpl-->helloImpl
        HelloI h= (HelloI) context.getBean("helloImpl");
        h.say();
    }

part3: 面向切面編程( 三種配置方式: 加強版代理模式,使用<aop:config>標籤配置)

第一步:創建包: aop, 新建方法增強類

//自定義方法如何增強:代理模式的'handler'角色

    //後置增強
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;

public class AfterReturningTest implements AfterReturningAdvice {
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("after....");
    }
}

    //環繞增強
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class AroundAdviceTest implements MethodInterceptor {

    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        long start=System.nanoTime();
        System.out.println("--------方法  begin");

        Object res = methodInvocation.proceed();
        System.out.println("--------方法  end ---time="+ (System.nanoTime()-start));
        return res;
    }
}

    //前置增強
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;

public class BeforeMethodTest implements MethodBeforeAdvice {
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("before....");
    }
}

    //異常增強
import org.springframework.aop.ThrowsAdvice;
import java.lang.reflect.Method;

public class ThrowsAdviceTest implements ThrowsAdvice {
    public void afterThrowing(Method method, Object[] args, Object target, Exception ex){
        System.out.println("error....");
        ex.printStackTrace();
    }
}

 第二步: 創建xml文件, 使得spring管理對象的裝配---創建自定義的aop.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"
       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-4.3.xsd">

    <!--方法增強處理器: 前置通知-->
   <bean id="beforeAdvice" class="aop.BeforeMethodTest"></bean>
    <bean id="errorAdvice" class="aop.ThrowsAdviceTest"></bean>
    <bean id="aroundAdvice" class="aop.AroundAdviceTest"></bean>
    <bean id="afterAdvice" class="aop.AfterReturningTest"></bean>

    <!--接口實例: 目標對象-->
    <bean id="helloImpl" class="a.HelloImpl">
        <property name="name" value="tom"/>
    </bean>

    <!--代理: 創建proxy對象-->
    <bean id="proxyBean" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--接口[]-->
        <property name="proxyInterfaces">
            <list>
                <value>a.HelloI</value>
            </list>
        </property>

        <!--處理器: 方法增強者-->
        <property name="interceptorNames">
            <list>
                <value>beforeAdvice</value>
                <value>errorAdvice</value>
                <value>aroundAdvice</value>
                <value>afterAdvice</value>
            </list>
        </property>

        <!--接口實例對象-->
        <property name="target" ref="helloImpl"></property>
    </bean>
</beans>

第三步: 創建測試類, 驗證aop

@Test
    public void t(){
        //創建: 上下文
        ApplicationContext context = new ClassPathXmlApplicationContext("aop.xml");
        HelloI h= (HelloI) context.getBean("proxyBean");
        h.say();
    }

結果如下:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章