spring & aop- hello world (一)

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)impl

package 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!

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章