Spring框架的练习

注解

1.导入jar包

Spring框架的练习_第1张图片

 2.创建advice,service,servlet包

Spring框架的练习_第2张图片

 3.创建并书写BookService接口及实现类

//接口

package com.gao.service;

public interface BookService {

    void read();

    void listen();

    void write();

    void remember();
}



//实现类


package com.gao.service.impl;

import com.gao.service.BookService;
import org.springframework.stereotype.Service;

@Service
public class BookServiceImpl implements BookService {
    @Override
    public void read() {
        System.out.println("好好学习天天向上");
    }

    @Override
    public void listen() {
        System.out.println("天天向上好好学习");
    }

    @Override
    public void write() {
        System.out.println("天天学习好好向上");
    }

    @Override
    public void remember() {
        System.out.println("好好向上天天学习");
    }
}


4.创建并书写增强类

package com.gao.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class Student {
    @Before("execution(* *..BookService.*(..))")
    public void before(){
        System.out.println("前置通知。。。。。before");
    }
    @AfterReturning("execution(* *..BookService.*(..))")
    public void afterReturn(){
        System.out.println("后置通知。。。。。afterReturn");
    }
    @AfterThrowing("execution(* *..BookService.*(..))")
    public void afterThrow(){
        System.out.println("异常通知。。。。。afterThrow");
    }
    @After("execution(* *..BookService.*(..))")
    public void after(){
        System.out.println("最终通知。。。。。after");
    }
}

5.配置xml文件



    
    

    
    
    

6.测试类

package com.gao.servlet;

import com.gao.service.BookService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestServlet {
    @Test
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
        BookService bookService = context.getBean(BookService.class);

        bookService.read();
        System.out.println("我是分割线~~~~~~~~~");
        bookService.listen();
        System.out.println("我是分割线~~~~~~~~~");
        bookService.remember();
        System.out.println("我是分割线~~~~~~~~~");
        bookService.write();
    }
}

8.运行结果

Spring框架的练习_第3张图片

环绕通知 

1.导入jar包

Spring框架的练习_第4张图片

2.创建advice,service,servlet包

Spring框架的练习_第5张图片 

3.创建并书写BookService接口及实现类

package com.gao.service;

public interface BookService {

    void read();

    void lision();

    void write();

    void renumber();
}




package com.gao.service.impl;

import com.gao.service.BookService;

public class BookServiceImpl implements BookService {
    @Override
    public void read() {
        System.out.println("好好学习天天向上");
    }

    @Override
    public void lision() {
        System.out.println("天天向上好好学习");
    }

    @Override
    public void write() {
        System.out.println("天天学习好好向上");
    }

    @Override
    public void renumber() {
        System.out.println("好好向上天天学习");
    }
}

4.创建并书写增强类

package com.gao.advice;

import org.aspectj.lang.ProceedingJoinPoint;

public class Student {

    public void before(){
        System.out.println("前置通知。。。。。before");
    }
    public void afterReturn(){
        System.out.println("后置通知。。。。。afterReturn");
    }
    public void afterThrow(){
        System.out.println("异常通知。。。。。afterThrow");
    }
    public void after(){
        System.out.println("最终通知。。。。。after");
    }

    public Object around(ProceedingJoinPoint pjp){
        try {
            //前置增强
            System.out.println("环绕通知-----前置增强");

            //通过ProceedingJoinPoint完成代理对象的方法调用
            Object result=null;
            Object[] args = pjp.getArgs();
            result=pjp.proceed(args);

            //后置增强
            System.out.println("环绕通知-----后置增强");

            return result;
        } catch (Throwable e) {
            System.out.println("环绕通知-----异常增强");
            throw new RuntimeException(e);
        } finally {
            System.out.println("环绕通知-----最终增强");
        }
    }
}

5.配置xml文件




        
        
            
                
                    
                
            

    

6.测试类

package com.gao.servlet;

import com.gao.service.BookService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestServlet {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
        BookService bookService = context.getBean(BookService.class);

        bookService.read();
        System.out.println("我是分割线~~~~~~~~~");
        bookService.lision();
        System.out.println("我是分割线~~~~~~~~~");
        bookService.renumber();
        System.out.println("我是分割线~~~~~~~~~");
        bookService.write();
    }
}

7.运行结果

Spring框架的练习_第6张图片

 纯注解

1.导入jar包

Spring框架的练习_第7张图片

2.创建config,service,servlet和advice包

Spring框架的练习_第8张图片 

 3.创建并书写BookService接口及实现类

//接口
package com.gao.service;

public interface BookService {

    void listen();

    void read();

    void speak();

    void write();
}



//实现类
package com.gao.service.impl;

import com.gao.service.BookService;
import org.springframework.stereotype.Service;


@Service
public class BookServiceImpl implements BookService {
    @Override
    public void listen() {
        System.out.println("听");
    }

    @Override
    public void read() {
        System.out.println("读");
    }

    @Override
    public void speak() {
        System.out.println("说");
    }

    @Override
    public void write() {
        System.out.println("写");
    }
}

4.创建并书写增强类

package com.gao.advice;


import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class Student {
    @Before("execution(* *..BookService.*(..))")
    public void before(){
        System.out.println("前置通知。。。。。before");
    }
    @AfterReturning("execution(* *..BookService.*(..))")
    public void afterReturn(){
        System.out.println("后置通知。。。。。afterReturn");
    }
    @AfterThrowing("execution(* *..BookService.*(..))")
    public void afterThrow(){
        System.out.println("异常通知。。。。。afterThrow");
    }
    @After("execution(* *..BookService.*(..))")
    public void after(){
        System.out.println("最终通知。。。。。after");
    }

}

5.创建并书写springconfig类

package com.gao.config;


import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@ComponentScan("com.gao")
@EnableAspectJAutoProxy
public class SpringConfig {

}

6.测试类

package com.gao.servlet;

import com.gao.config.SpringConfig;
import com.gao.service.BookService;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestServlet {
    @Test
    public void test01(){
        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class);
        BookService bookService = context.getBean(BookService.class);

        bookService.write();
        System.out.println("我是分割线~~~~~~~~~");
        bookService.read();
        System.out.println("我是分割线~~~~~~~~~");
        bookService.listen();
        System.out.println("我是分割线~~~~~~~~~");
        bookService.speak();
    }

}

7.运行结果

Spring框架的练习_第9张图片

 

你可能感兴趣的:(spring,java,spring,boot)