1. 首先下载准备需要的包:
1)spring framework 3.0.x: http://www.springsource.com/download/community?sid=1220898
spring3.0 以上与前面的版本目录不同,下载解压后不再是一个spring.jar包,而是根据不同的需要将其分开在dist目录下。用户可以根据需要选择。 安全起见,可将 所有jar包全部导入工程。
此外,还需在工程中添加以下包:aspectj.weaver.jar,aopalliance.jar,commons-logging.jar
这三个包可以在其dependency里找到。或者自己下载。
2. Hello world Example just to test spring bean.
hello world bean
package test.cmei.spring.common;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class HelloWorld {
private String msg;
public void setMsg(String msg){
this.msg=msg;
}
public void sayHello(){
System.out.println(msg);
}
public static void main(String[] args){
Resource res=new ClassPathResource("bean.xml");
BeanFactory factory=new XmlBeanFactory(res);
HelloWorld hello=(HelloWorld)factory.getBean("helloBean");
hello.sayHello();
}
}
xml 配置
<?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: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-2.0.xsd
http://www.springframework.org/schema/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
>
<beans>
<bean id="helloBean" class="test.cmei.spring.common.HelloWorld">
<property name="msg" value="It is msgs"></property>
</bean>
</beans>
3. Spring AOP
参考: http://apps.hi.baidu.com/share/detail/33101217
两种方式实现:一、编写切面,二、通过xml配置
一、编写切面
(1) interface
package test.cmei.spring.common; public interface IUserManager { public void delUser(int id); public String findUser(int id); public void saveUser(String username,String password); public void updateUser(int id); }
(2)implpackage test.cmei.spring.common; public class UserManageImpl implements IUserManager{ public void delUser(int id){ System.out.println("+++++++++++delUser++++++++"); } public String findUser(int id){ System.out.println("+++++++++++++findUser+++++++"); return null; } public void saveUser(String username,String password){ System.out.println("+++++++++++++saveUser+++++++"); } public void updateUser(int id){ System.out.println("+++++++++++++updateUser+++++++"); } }
(3)切面package test.cmei.spring.aop; import org.aspectj.lang.annotation.*; @Aspect public class CheckAspect { @Pointcut("execution(* test.cmei.spring.common.UserManageImpl.save*(..))||execution(* test.cmei.spring.common.UserManageImpl.del*(..))") private void allSaveMethod(){ } @Before("allSaveMethod()") public void checkUser(){ System.out.println("============checkuser============"); } }
(4) mainTest
package test.cmei.spring.common; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args){ BeanFactory factory=new ClassPathXmlApplicationContext("aopconfig.xml"); IUserManager userManager=(IUserManager)factory.getBean("userManager"); userManager.saveUser("cat", "123"); userManager.delUser(1); userManager.updateUser(1); } }
(5) aopconfig.xml<?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: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-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" > <bean id="checkAspect" class="test.cmei.spring.aop.CheckAspect"></bean> <bean id="userManager" class="test.cmei.spring.common.UserManageImpl"></bean> <aop:aspectj-autoproxy /> </beans>
运行结果:
============checkuser============
+++++++++++++saveUser+++++++
============checkuser============
+++++++++++delUser++++++++
+++++++++++++updateUser+++++++
二、通过xml配置切面
上述(1)(2)(4)文件不变
(3)
package test.cmei.spring.common; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args){ // ApplicationContext context=new ClassPathXmlApplicationContext("aopconfig.xml"); // Monkey monkey=(Monkey) context.getBean("monkey"); // monkey.stealPeaches("laosun"); BeanFactory factory=new ClassPathXmlApplicationContext("aop.xml"); IUserManager userManager=(IUserManager)factory.getBean("userManager"); userManager.saveUser("cat", "123"); userManager.delUser(1); userManager.updateUser(1); } }
(5) 加入切面配置信息
<?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: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-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" > <bean id="checkAspect" class="test.cmei.spring.aop.CheckAspect" /> <bean id="userManager" class="test.cmei.spring.common.UserManageImpl" /> <aop:config> <!-- 配置切面appect , ref切面类 --> <aop:aspect id="check" ref="checkAspect"> <!-- 配置切入点pointcut, 定义一个表达式 --> <aop:pointcut id="allSaveMethod" expression="execution(* test.cmei.spring.common.UserManageImpl.save*(..))"/> <!-- 设置before advice, 用checkAspect中的一个方法,并定位到相位的切入点pointcut --> <aop:before method="checkUser" pointcut-ref="allSaveMethod"/> </aop:aspect> </aop:config> </beans>
运行结果:
============checkuser============
+++++++++++++saveUser+++++++
+++++++++++delUser++++++++
+++++++++++++updateUser+++++++
如果顺利的话,到此结束了。
如果不顺利的话,请看下一章。将探讨我在此过程中碰到的一些问题。
hope to help!