package cn.edu.hpu.dao.Impl; import cn.edu.hpu.dao.UserDao; import cn.edu.hpu.model.User; public class UserDaoImpl implements UserDao{ public void save(User u) { System.out.println("save start...."); System.out.println("add success!!"); System.out.println("save end...."); } }
package cn.edu.hpu.dao.Impl; import cn.edu.hpu.model.User; public class UserDaoImpl2 extends UserDaoImpl{ @Override public void save(User u) { System.out.println("save start...."); super.save(u); System.out.println("save end...."); } }在beans.xml初始化UserDaoImpl2
package cn.edu.hpu.dao.Impl; import cn.edu.hpu.dao.UserDao; import cn.edu.hpu.model.User; public class UserDaoImpl3 implements UserDao{ private UserDao userDao=new UserDaoImpl(); public void save(User u) { System.out.println("save start...."); userDao.save(u); System.out.println("save end...."); } }好处:可以互相之间来回的组合(可以换成new UserDaoImpl2();)
package cn.edu.hpu.aop; //日志的拦截器 public class LogIntercept { public void beforeMethod(){ System.out.println("save start..."); } public void afterMethod(){ System.out.println("save end..."); } }
package cn.edu.hpu.dao.Impl; import cn.edu.hpu.aop.LogIntercept; import cn.edu.hpu.dao.UserDao; import cn.edu.hpu.model.User; public class UserDaoImpl4 implements UserDao{ private UserDao userDao=new UserDaoImpl(); public void save(User u) { new LogIntercept().beforeMethod(); userDao.save(u); new LogIntercept().afterMethod(); } }
<!--这里的Log标签是假想的,不存在--> <beans> <bean id="u" class="cn.edu.hpu.dao.Impl.UserDaoImpl" /> <Log method="beforeMethod"/> <Log method="afterMethod"/> </bean> <bean id="userService" class="cn.edu.hpu.service.UserService" > <property name="userDAO" bean="u"/> </bean> </beans>这里就可以将日志逻辑注入了。
下一篇总结我会利用动态代理实现AOP,并解释AOP:http://blog.csdn.net/acmman/article/details/44276427
转载请注明出处:http://blog.csdn.net/acmman/article/details/44276369