使用@AspectJ的例子书99页
Tiger类
package com.experice;
public class Tiger {
public void walk(){
System.out.println("Tiger is walking..");
}
}
Fighter类
package com.experice;
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.experice.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 flight with Tiger...");
}
}
配置
<aop:aspectj-autoproxy/>
<bean id="fighter" class="com.experice.Fighter"/>
<bean id="tiger" class="com.experice.Tiger"/>
Application主类
package com.experice;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application {
public static void main(String[] args) {
@SuppressWarnings("resources")
ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
Tiger tiger=context.getBean(Tiger.class);
tiger.walk();
}
}
配置图片和相关的Bean
ImageCopy类
package com.experice;
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:/bg1.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();
}
}
Imagehandler类
package com.experice;
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.;
@Aspect
public class ImageHandler {
private static final Logger logger= LoggerFactory.getLogger(ImageHandler.class);
@Pointcut("execution(* com.experice.ImageCopy.copyImage())")
// 切点函数,必须为空
public void handleImage()throws IOException {
}
@Before(value="handleImage()")
public void pressTextOnImage() throws IOException {
logger.info("开始给图片添加水印");
File srcFile=new File("D:/bg.jpg");
// 创建一个BufferedImage对象
BufferedImage srcImg= ImageIO.read(new FileInputStream(srcFile));
int width=srcImg.getWidth();
int height=srcImg.getHeight();
System.out.println("原图宽:"+width+",原图稿:"+height);
File destFile=new File("D:/bg1.jpg");
Color color=new Color(0,0,0);
int size=18;
Font font=new Font("微软雅黑",Font.BOLD,size);
String text="个人图片";
ImageUtil.pressText(srcFile,destFile,"私密文件",color,font,(width-text.length()size)/2,height/2-size,1.0f);
}
@AfterReturning("handleImage()")
/ public void grayImage(){
logger.info("开始将图片转成黑白");
File srcFile = new File("E:/bg.jpg");
File destFile = new File("E:/bg1.jpg");
ImageUtil.gray(srcFile,destFile);
}*/
public void grayImage(){
logger.info("开始将图片变成黑白");
File srcFile=new File("E:/bg1.jpg");
File destFile=new File("E:/bg2.jpg");
ImageUtil.gray(srcFile,destFile);
}
}
配置
<aop:aspectj-autoproxy/>
<bean id="imageCopy" class="com.experice.ImageCopy"/>
<bean id="imageHandler" class="com.experice.ImageHandler"/>
ImageCopyApp类
package com.experice;
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("bean.xml");
ImageCopy imageCopy=context.getBean(ImageCopy.class);
try {
imageCopy.copyImage();
} catch (IOException e) {
e.printStackTrace();
}
}
}
二维码的制作
package com.experice;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import java.awt.*;
public class QrCodeTest {
public static void main(String[] args) {
QrConfig config = new QrConfig(300, 300);
config.setMargin(3);
Color foreColor =new Color(255,218,185);
Color bgColor = new Color(132,112,255);
int foreColorRGB = foreColor.getRGB();
int bgColorRGB = bgColor.getRGB();
config.setForeColor(foreColorRGB);
config.setBackColor(bgColorRGB);
// 设置边距,既二维码和背景之间的边距
config.setMargin(3);
// 设置前景色,既二维码颜色(青色)
config.setForeColor(Color.CYAN.getRGB());
// 设置背景色(灰色)
config.setBackColor(Color.GRAY.getRGB());
// 生成二维码到文件,也可以到流
QrCodeUtil.generate("http://hutool.cn/", config, FileUtil.file("e:/qrcode.jpg"));
下面这几行代码不能让背景变色
QrCodeUtil.generate(//
"http://hutool.cn/", //二维码内容
QrConfig.create().setImg("d:/bg.jpg"), //附带logo
FileUtil.file("e:/qrcode.jpg")//写出到的文件
);
下面几行可以让背景变色
QrCodeUtil.generate(//
"http://hutool.cn/", //二维码内容
QrConfig.create().setImg("d:/bg.jpg"), //附带logo
FileUtil.file("e:/qrcode.jpg")//写出到的文件
这个可以,但好像有点不明白
}
}