9、AOP继续学习

2019-03-11  本文已影响0人  youi_e050

一、实战:使用@AspectJ注解的例子

下面使用一个简单有趣的例子来演示Spring AOP的用法,演绎一段“武松打虎”的故事情节——武松(Fighter)在山里等着老虎(Tiger)出现,只要发现老虎出来,就打老虎。

1、首先定义老虎Tiger类

package com.spring.Aop;

public class Tiger {
    public void walk(){
        System.out.println("Tiger is walking");
    }
}

2、定义切面和配置

package com.spring.Aop;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class Fighter {

    @Pointcut("execution(* com.spring.Aop.Tiger.walk())")
    public void foundTiger(){

    }
    @Before(value="foundTiger()")
    public void foundBefore(){
        System.out.println("Fighter wait for tiger...");
    }
    @AfterReturning("foundTiger()")
    public void foundAfter(){
        System.out.println("Fighter fight with tiger...");
    }
}
<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--启动AspectJ支持-->
    <aop:aspectj-autoproxy/>
    <!--定义bean-->
    <bean id="fighter" class="com.spring.Aop.Fighter"/>
    <bean id="tiger" class="com.spring.Aop.Tiger"/>

</beans>
package com.spring.Aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Applocation {
    public static void main(String[] args) {
        @SuppressWarnings("rescource")
        ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
        Tiger tiger = context.getBean(Tiger.class);
        tiger.walk();
    }
}

运行结果:


运行结果

二、图片处理的AOP程序

使用Hutool的工作集
hutool的网址:http://hutool.cn/
对图片进行水印,黑白,裁剪,旋转等

2.1添加图片水印和转换成黑白色

1.业务类,实现图片拷贝

package com.spring.Image;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * 业务类,实现图片拷贝
 */
public class ImageCopy {
    private static final Logger logger = LoggerFactory.getLogger(ImageCopy.class);
     public void copyImage()throws IOException{
         logger.info("开始将图片从D盘复制到E盘...");
         File srcFile = new File("D:/bg.jpg");
         File destFile = new File("E:/bg1.jpg");
         InputStream in =new FileInputStream(srcFile);
         OutputStream out =new FileOutputStream(destFile);
         byte[]bytes = new byte[(int) srcFile.length()];
         int len;
         while((len = in.read(bytes))!= -1){
             out.write(bytes,0,len);

         }
         in.close();
         out.close();

     }
}

2、图片处理类

package com.spring.Image;

import cn.hutool.core.util.ImageUtil;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 图片处理类
 */
@Aspect
public class ImageHandler {
    private static final Logger logger= LoggerFactory.getLogger(ImageHandler.class);
    //切点函数,必须为空
    @Pointcut("execution(* com.spring.Image.ImageCopy.copyImage())")
    public void handleImage(){

    }
    @Before(value="handleImage()")
    public void pressTextOnImage() throws IOException {
        logger.info("开始给图片添加水印");
        File srcFile=new File("D:/bg.jpg");
        File destFile=new File("D:/bg1.jpg");
//        创建字节输入流一个BufferedImage对象
        InputStream input = new FileInputStream(srcFile);
        BufferedImage srcImg = ImageIO.read(input);
        int width = srcImg.getWidth();
        int height = srcImg.getHeight();
        System.out.println("原图宽"+width+",y原图高:"+height);
        Color color = new Color(211,71,38);
        int size =60;
        Font font = new Font("微软雅黑",Font.BOLD,size);
        String text="@微信号:hj";
        ImageUtil.pressText(srcFile,destFile,text,color,font,(width-text.length() * size)/2,height/2-size,1.0f);
    }
    @AfterReturning("handleImage()")
    public void grayImage(){
        logger.info("开始将图片转成黑白");
        File srcFile =new File("E:/bg1.jpg");
        File destFile =new File("E:/bg1.jpg");
        ImageUtil.gray(srcFile,destFile);
    }

}

3、主应用类 ImageCopyApp

package com.spring.Image;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class ImageCopyApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        ImageCopy imageCopy= context.getBean(ImageCopy.class);
        try {
            imageCopy.copyImage();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

运行结果:


image.png
image.png

这是添加的水印

image.png

这是转成黑白

2.2 生成二维码,简书的主页

package com.spring.Image;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;

import java.awt.*;

public class jianshu {
    public static void main(String[] args) {
        QrConfig config =new QrConfig(300,300);
        config.setMargin(3);
        config.setImg("e:/2.png");//二维码的中间logo路径
        Color foreColor = new Color(87, 174, 228);
        Color bgColor =new Color(255, 250, 227);
        int foreColorRGB=foreColor.getRGB();
        int bgColorRGB=bgColor.getRGB();
//        设置前景色
         config.setForeColor(foreColorRGB);
//         设置背景色
         config.setBackColor(bgColorRGB);
//         生成二维码到文件,也可以到流
        QrCodeUtil.generate(//
                "https://www.jianshu.com/u/997fa4d92222", //二维码内容

               config, FileUtil.file("e:/qrcodeWithLogo.jpg")//写出到的文件
        );

    }

}

运行图片:


二维码
上一篇 下一篇

猜你喜欢

热点阅读