- AspectJ是一个面向切面的框架,它扩展了Java语言
12.1启用@AspectJ
可以通过XML或Java配置来启用@AspectJ支持,配置@AspectJ
<aspectj.version>1.9.2</aspectj.version>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
12.2声明Aspect
在启用@AspectJ支持下,在应用上下文中定义的任意带有一个@AspectJ注解的切面的bean都将被Spring自动识别并用于配置Spring AOP。
<aop:aspectj-autoproxy/>
<bean id="fighter" class="com.spring.aop.Fighter"/>
<bean id="tiger" class="com.spring.aop.Tiger"/>
@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");
}
}
注意:切点方法必须为空
12.3定义主应用
public class Application {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext context=new ClassPathXmlApplicationContext("Spring.xml");
Tiger tiger=context.getBean(Tiger.class);
tiger.walk();
}
}
运行结果为:
12.4使用hutool进行一个图片练习
- 创建一个ImageCopy类,来实现图片的拷贝
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:/110.jpg");
File destFile=new File("E:/110.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类
创建一个前置方法为图片添加水印
再添加一个后置方法把图片变成黑白色
@Aspect
public class ImageHandler {
private static final Logger logger= LoggerFactory.getLogger(ImageHandler.class);
@Pointcut("execution(* com.spring.aop.ImageCopy.copyImage())")
public void handleImage(){}
@Before(value = "handleImage()")
public void pressTextOnImage() throws IOException {
logger.info("开始给图片添加水印");
File srcFile=new File("D:/100.jpg");
BufferedImage srcImg= ImageIO.read(new FileInputStream(srcFile));
int height=srcImg.getHeight();
int width=srcImg.getWidth();
File destFile=new File("D:/110.jpg");
Color color =new Color(211,71,38);
int size=15;
Font font=new Font("微软雅黑",Font.BOLD,size);
String text="微信";
ImageUtil.pressText(srcFile,destFile,text,color,font,width/2-text.length()*size,height/2-size,0.8f);
}
@AfterReturning("handleImage()")
public void grayImage(){
logger.info("开始讲图片转成黑白色");
File srcFile=new File("E:/110.jpg");
File destFile=new File("E:/112.jpg");
ImageUtil.gray(srcFile,destFile);
}
}
- 定义主应用程序ImageCopyApp
public class ImageCopyApp {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("com/spring/aop/Spring-aop.xml");
ImageCopy imageCopy=context.getBean(ImageCopy.class);
try {
imageCopy.copyImage();
} catch (IOException e) {
e.printStackTrace();
}
}
}
二维码练习
- 建一个QrcodeTest类
public class QrCodetest {
public static void main(String[] args) {
QrConfig config=new QrConfig(300,300);
config.setMargin(3);
config.setImg("d:/timg.jpg");
Color foreColor=new Color(238,110,115);
int foreColorRGB=foreColor.getRGB();
config.setForeColor(foreColorRGB);
config.setBackColor(Color.white.getRGB());
QrCodeUtil.generate("https://www.jianshu.com/u/a32873d23481",config,FileUtil.file("D:/qrcode.jpg"));
}
}
更多hutool工具请访问:hutool官网
依赖添加
<hutool.version>4.5.1</hutool.version>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>