当前位置 : 主页 > 编程语言 > java >

注解实现aop

来源:互联网 收集:自由互联 发布时间:2021-06-28
接口 package com.liu.aop_anno;public interface IUserDao {public void insert();} 实现类 package com.liu.aop_anno;import org.springframework.stereotype.Component;@Componentpublic class UserDao implements IUserDao{@Overridepublic void ins
接口
package com.liu.aop_anno;

public interface IUserDao {

	public void insert();
}
实现类
package com.liu.aop_anno;

import org.springframework.stereotype.Component;

@Component
public class UserDao implements IUserDao{

	@Override
	public void insert() {
		System.out.println("============保存============");
	}

}
切面类
package com.liu.aop_anno;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class Aop {
	
	@Pointcut("execution (* com.liu.aop_anno.UserDao.*(..))")
	public void testPc(){
		
	}
	
	@Before("testPc()")
	public void beginBus(){
		System.out.println("=========开启事务==========");
	}
	
	@After("testPc()")
	public void endBus(){
		System.out.println("=========结束事务===========");
	}
}
测试类
package com.liu.aop_anno;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class App {

	Logger logger = Logger.getLogger(App.class);
	
	
	@Test
	public void save(){
		
		ApplicationContext ac 
			= new ClassPathXmlApplicationContext("com/liu/aop_anno/applicationContext.xml");
		IUserDao userDao = (IUserDao)ac.getBean("userDao");
		userDao.insert();
	}
}
spring配置
 

 
    
    
  
    
	
  

 
log4j配置
log4j.rootLogger =debug,console, file
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p %c.%M()-%m%n

#-----------------------------------------------------------
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:/log/haha.log
log4j.appender.file.MaxFileSize=1024KB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %p %c.%M()-%m%n
上一篇:常用日期操作方法
下一篇:email
网友评论