采用annotation的方式配置Spring的IOC和AOP/采用XML的方式配置Spring的IOC和AOP

http://wosyingjun.iteye.com/blog/1837711


项目(包)列表:



 

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.       
  8.     <!--spring 配置文件位置-->  
  9.     <context-param>  
  10.         <param-name>contextConfigLocation</param-name>  
  11.         <param-value>classpath:spring.xml</param-value>  
  12.     </context-param>  
  13.     <!--spring 监听器-->  
  14.     <listener>  
  15.         <description>spring监听器</description>  
  16.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  17.     </listener>  
  18.       
  19.       
  20.   <welcome-file-list>  
  21.     <welcome-file>index.jsp</welcome-file>  
  22.   </welcome-file-list>  
  23. </web-app>  

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="  
  6.     http://www.springframework.org/schema/beans   
  7.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.     http://www.springframework.org/schema/tx  
  9.     http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  
  10.     http://www.springframework.org/schema/aop   
  11.     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  12.     http://www.springframework.org/schema/context     
  13.     http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  14.  ">  
  15.    
  16.    
  17.   <!-- 采用的annotation的方式进行AOP和 IOC -->  
  18.       
  19.     <!-- 配置了component-scan可以移除 -->  
  20.      <context:annotation-config />   
  21.     <!-- 启动对@AspectJ注解的支持 -->  
  22.     <aop:aspectj-autoproxy />   
  23.     <!-- 自动扫描包(自动注入) -->  
  24.     <context:component-scan base-package="yingjun" />   
  25.  </beans>  

 

Java代码   收藏代码
  1. package yingjun.aop;  
  2.   
  3. import org.aspectj.lang.ProceedingJoinPoint;  
  4. import org.aspectj.lang.annotation.AfterReturning;  
  5. import org.aspectj.lang.annotation.AfterThrowing;  
  6. import org.aspectj.lang.annotation.Around;  
  7. import org.aspectj.lang.annotation.Aspect;  
  8. import org.aspectj.lang.annotation.Before;  
  9. import org.aspectj.lang.annotation.Pointcut;  
  10. import org.springframework.stereotype.Component;  
  11.   
  12. @Component("aop")  
  13. @Aspect  
  14. public class AopMethod {  
  15.   
  16. @Pointcut("execution( * yingjun.service..*.*(..))")  
  17. public void myMethod(){}  
  18.   
  19. @Before("execution( * yingjun.service..*.*(..))")//表示yingjun.service下任何包下任何类任何返回值的任何方法  
  20. public void beforeMethod(){  
  21.     System.out.println("before method...AOP!!!!");  
  22. }  
  23. @AfterReturning("myMethod()")  
  24. public void AfterReturningMethod(){  
  25.     System.out.println("After  Returning  normal...AOP!!!!");  
  26. }  
  27. @AfterThrowing("myMethod()")  
  28. public void AfterThrowing(){  
  29.     System.out.println("After Throwing...AOP!!!!");  
  30. }  
  31. @Around("myMethod()")  
  32. public void Around(ProceedingJoinPoint pjp) throws Throwable{  
  33.     System.out.println("method around start!!!!");  
  34.     pjp.proceed();  
  35.     System.out.println("method around end!!!!");  
  36. }  
  37.   
  38. }  
  39.   
  40.   
  41. /*public class AopMethod { 
  42.      
  43.     public void beforeMethod(){ 
  44.         System.out.println("before method...AOP!!!!"); 
  45.     } 
  46.      
  47.     public void AfterReturningMethod(){ 
  48.         System.out.println("After  Returning  normal...AOP!!!!"); 
  49.     } 
  50.     public void AfterMethod(){ 
  51.         System.out.println("After  method...AOP!!!!"); 
  52.     } 
  53.     public void AfterThrowing(){ 
  54.         System.out.println("After Throwing...AOP!!!!"); 
  55.     } 
  56.      
  57.  
  58. }*/  

 

Java代码   收藏代码
  1. package yingjun.service;  
  2.   
  3.   
  4. import yingjun.model.User;  
  5.   
  6.   
  7.   
  8. public interface UserServiceI {  
  9.       
  10.     /*用户操作*/  
  11.     public void DoUser(User use);  
  12.       
  13.   
  14. }  

 

Java代码   收藏代码
  1. package yingjun.service.impl;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.stereotype.Component;  
  5. import yingjun.dao.UserDaoI;  
  6. import yingjun.model.User;  
  7. import yingjun.service.UserServiceI;  
  8. @Component("userService")  
  9. public class UserServiceImpl implements UserServiceI {  
  10.     private UserDaoI userdao;  
  11.       
  12.     public void DoUser(User user) {  
  13.         userdao.SaveUser(user);  
  14.           
  15.     }  
  16.     public UserDaoI getUserdao() {  
  17.         return userdao;  
  18.     }  
  19.   
  20.     //@Autowired按byType自动注入,如果想用byName则使用@Qulifie,  
  21.     //而@Resource默认按name,name找不到,按类型  
  22.     @Autowired  
  23.     public void setUserdao(UserDaoI userdao) {  
  24.         this.userdao = userdao;  
  25.     }  
  26.   
  27. }  

 

Java代码   收藏代码
  1. package yingjun.dao;  
  2.   
  3. import yingjun.model.User;  
  4.   
  5.   
  6.   
  7. public interface UserDaoI   {  
  8.     public void AddUser(User user );      
  9.     public void DeleteUser(User user );  
  10.     public void SaveUser(User user );  
  11. }  

 

Java代码   收藏代码
  1. package yingjun.dao.impl;  
  2.   
  3. import org.springframework.stereotype.Component;  
  4. import org.springframework.stereotype.Repository;  
  5.   
  6. import yingjun.dao.UserDaoI;  
  7. import yingjun.model.User;  
  8.   
  9. @Component("userdao")  
  10. public class UserDaoImpl implements UserDaoI{  
  11.   
  12.       
  13.         public void AddUser(User user ) {  
  14.             System.out.println("增加用户成功");  
  15.               
  16.         }  
  17.         public void DeleteUser(User user ) {  
  18.             System.out.println("删除用户成功");  
  19.               
  20.         }  
  21.   
  22.         public void SaveUser(User user ) {  
  23.             System.out.println("保存用户成功");  
  24.                       
  25.     }  
  26.   
  27. }  

 

Java代码   收藏代码
  1. package yingjun.model;  
  2.   
  3. public class User {  
  4.     private int id;  
  5.     private String name;  
  6.       
  7.     public int getId() {  
  8.         return id;  
  9.     }  
  10.     public void setId(int id) {  
  11.         this.id = id;  
  12.     }  
  13.     public String getName() {  
  14.         return name;  
  15.     }  
  16.     public void setName(String name) {  
  17.         this.name = name;  
  18.     }  
  19. }  

 

Java代码   收藏代码
  1. package yingjun.test;  
  2.   
  3. import org.junit.Test;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6. import yingjun.model.User;  
  7. import yingjun.service.UserServiceI;  
  8.   
  9.   
  10.   
  11. public class SpringTest {  
  12.   
  13.     @Test  
  14.     public void springtest(){     
  15.         ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");  
  16.         UserServiceI us=(UserServiceI)ac.getBean("userService");  
  17.         User user=new User();  
  18.         us.DoUser(user);  
  19.     }  
  20.   
  21. }  
   运行结果:

=================================================================================================================

采用XML的方式配置Spring的IOC和AOP


http://wosyingjun.iteye.com/blog/1837682

项目(包)列表:

 

    

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.       
  8.     <!--spring 配置文件位置-->  
  9.     <context-param>  
  10.         <param-name>contextConfigLocation</param-name>  
  11.         <param-value>classpath:spring.xml</param-value>  
  12.     </context-param>  
  13.     <!--spring 监听器-->  
  14.     <listener>  
  15.         <description>spring监听器</description>  
  16.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  17.     </listener>  
  18.       
  19.       
  20.   <welcome-file-list>  
  21.     <welcome-file>index.jsp</welcome-file>  
  22.   </welcome-file-list>  
  23. </web-app>  

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="  
  6.     http://www.springframework.org/schema/beans   
  7.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.     http://www.springframework.org/schema/tx  
  9.     http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  
  10.     http://www.springframework.org/schema/aop   
  11.     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  12.     http://www.springframework.org/schema/context     
  13.     http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  14.  ">  
  15.   <bean id="AopMethod" class="yingjun.aop.AopMethod"> </bean>  
  16.    <bean id="u" class="yingjun.dao.impl.UserDaoImpl"> </bean>  
  17.    <bean id="userService" class="yingjun.service.impl.UserServiceImpl">  
  18.         <property name="userdao"  ref="u"></property>  
  19.    </bean>  
  20.   <aop:config>  
  21.         <aop:pointcut expression="execution( * yingjun.service..*.*(..))" id="servicepointcut"/>  
  22.         <aop:aspect id="myaspect" ref="AopMethod">  
  23.             <!--对应expression执行前运行  -->  
  24.             <aop:before method="beforeMethod" pointcut-ref="servicepointcut"/>  
  25.             <!--对应expression执行后返回正常运行  -->  
  26.             <aop:after-returning method="AfterReturningMethod" pointcut-ref="servicepointcut"/>  
  27.             <!--对应expression抛出异常运行  -->  
  28.             <aop:after-throwing method="AfterThrowing"  pointcut-ref="servicepointcut"/>  
  29.             <!--对应expression执行后运行(不管是否抛出异常都会执行)  -->  
  30.             <aop:after method="AfterMethod" pointcut-ref="servicepointcut"/>  
  31.         </aop:aspect>  
  32.           
  33.   </aop:config>  
  34.   
  35. </beans>  

 

Java代码   收藏代码
  1. package yingjun.aop;  
  2.   
  3.   
  4.   
  5. public class AopMethod {  
  6.   
  7.   
  8.   
  9.     public void beforeMethod(){  
  10.         System.out.println("before method...AOP!!!!");  
  11.     }  
  12.       
  13.     public void AfterReturningMethod(){  
  14.         System.out.println("After  Returning  normal...AOP!!!!");  
  15.     }  
  16.     public void AfterMethod(){  
  17.         System.out.println("After  method...AOP!!!!");  
  18.     }  
  19.     public void AfterThrowing(){  
  20.         System.out.println("After Throwing...AOP!!!!");  
  21.     }  
  22.       
  23.   
  24. }  

 

Java代码   收藏代码
  1. package yingjun.service;  
  2.   
  3.   
  4. import yingjun.model.User;  
  5.   
  6.   
  7.   
  8. public interface UserServiceI {  
  9.       
  10.     /*用户操作*/  
  11.     public void DoUser(User use);  
  12.       
  13.   
  14. }  

 

Java代码   收藏代码
  1. package yingjun.service.impl;  
  2.   
  3.   
  4. import yingjun.dao.UserDaoI;  
  5. import yingjun.model.User;  
  6. import yingjun.service.UserServiceI;  
  7.   
  8. public class UserServiceImpl implements UserServiceI {  
  9.     private UserDaoI userdao;  
  10.       
  11.     public void DoUser(User user) {  
  12.         userdao.SaveUser(user);  
  13.           
  14.     }  
  15.     public UserDaoI getUserdao() {  
  16.         return userdao;  
  17.     }  
  18.   
  19.   
  20.   
  21.     public void setUserdao(UserDaoI userdao) {  
  22.         this.userdao = userdao;  
  23.     }  
  24.   
  25.   
  26.   
  27.   
  28. }  

 

Java代码   收藏代码
  1. package yingjun.dao;  
  2.   
  3. import yingjun.model.User;  
  4.   
  5.   
  6.   
  7. public interface UserDaoI   {  
  8.     public void AddUser(User user );      
  9.     public void DeleteUser(User user );  
  10.     public void SaveUser(User user );  
  11. }  

 

Java代码   收藏代码
  1. package yingjun.dao.impl;  
  2.   
  3. import yingjun.dao.UserDaoI;  
  4. import yingjun.model.User;  
  5.   
  6.   
  7. public class UserDaoImpl implements UserDaoI{  
  8.   
  9.       
  10.         public void AddUser(User user ) {  
  11.             System.out.println("增加用户成功");  
  12.               
  13.         }  
  14.         public void DeleteUser(User user ) {  
  15.             System.out.println("删除用户成功");  
  16.               
  17.         }  
  18.   
  19.         public void SaveUser(User user ) {  
  20.             System.out.println("保存用户成功");  
  21.                       
  22.     }  
  23.   
  24. }  

 

Java代码   收藏代码
  1. package yingjun.model;  
  2.   
  3. public class User {  
  4.     private int id;  
  5.     private String name;  
  6.       
  7.     public int getId() {  
  8.         return id;  
  9.     }  
  10.     public void setId(int id) {  
  11.         this.id = id;  
  12.     }  
  13.     public String getName() {  
  14.         return name;  
  15.     }  
  16.     public void setName(String name) {  
  17.         this.name = name;  
  18.     }  
  19. }  

 

Java代码   收藏代码
  1. package yingjun.test;  
  2.   
  3. import org.junit.Test;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6. import yingjun.model.User;  
  7. import yingjun.service.UserServiceI;  
  8.   
  9. public class SpringTest {  
  10.   
  11.     @Test  
  12.     public void springtest(){     
  13.         ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");  
  14.         UserServiceI us=(UserServiceI)ac.getBean("userService");  
  15.         User user=new User();  
  16.         us.DoUser(user);  
  17.     }  
  18.   
  19. }  
 运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值