日期:2014-05-19 浏览次数:20732 次
public class CreateAop { public void doBeofore(){ System.out.println("--------权限验证"); } public void doAfter(){ System.out.println("--------记录日志"); } }
public interface UserDao { public void insert(String name); public void test(); }
public class UserDaoImpl implements UserDao { @Override public void insert(String name) { System.out.println("你好,"+name); } @Override public void test() { System.out.println("test success......"); } }
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.spingframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <bean id="userdaoImpl" class="Imp.UserDaoImpl"></bean> <bean id="CA" class="Aop.CreateAop"></bean> <!-- 配置AOP --> <aop:config> <!-- 配置切面 --> <aop:aspect id="ap" ref="CA"> <!-- 配置切入点 :expression={返回任意类型或不返回 ,这个包下的任意类的任意方法和任意参数}--> <aop:pointcut expression="execution(* Imp.*.*(..))" id="ep"/> <!-- 配置通知 --> <aop:after method="doAfter" pointcut-ref="ep"/> <aop:before method="doBeofore" pointcut-ref="ep"/> </aop:aspect> </aop:config> </beans>
public static void main(String[] args) { ClassPathResource cpr=new ClassPathResource("applicationContext.xml"); XmlBeanFactory factory=new XmlBeanFactory(cpr); UserDao userdao=(UserDao)factory.getBean("userdaoImpl"); userdao.insert("young"); }
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userdao=(UserDao)factory.getBean("userdaoImpl"); userdao.insert("young");
------解决方案--------------------
你的包名还是大写的啊。你按住ctrl 看能点进去这两个类吗?<bean id="userdaoImpl" class="Imp.UserDaoImpl"></bean&