• [Java] Spring_1500_AOP_Annotation


    15, <aop:aspectj-autoproxy />

      把逻辑织入到原来那个方法里面去。
    @Aspect    说明这是一个切面类

    @Component 初始化这个类

    package com.bjsxt.aop;

    LogInterceptor        很好!很好!写的!

    package com.bjsxt.aop;
    
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.stereotype.Component;
    
    @Aspect
    @Component
    public class LogInterceptor {
    		
    	@Before("execution(public void com.bjsxt.dao.impl.UserDAOImpl.save(com.bjsxt.model.User))")  // 这就是织入点语法
    	public void before() {
    		System.out.println("method start");
    	}
    	@AfterReturning("execution(public * com.bjsxt.dao..*.*(..))")
    	public void afterReturning() {
    		System.out.println("method after returning");
    	}
    
    }
    //

    package com.bjsxt.aop;

    LogInterceptor        2, 很好!很好!写的!

    package com.bjsxt.aop;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;
    
    @Aspect
    @Component
    public class LogInterceptor {
    	@Pointcut("execution(public * com.bjsxt.service..*.add(..))")
    	public void myMethod(){};
    	
    	@Before("myMethod()")
    	public void before() {
    		System.out.println("method before");
    	}
    	
    	@Around("myMethod()")
    	public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
    		System.out.println("method around start");
    		pjp.proceed();
    		System.out.println("method around end");
    	}
    }
    //

    package com.bjsxt.dao;

    interface UserDAO

    package com.bjsxt.dao;
    
    import com.bjsxt.model.User;
    
    public interface UserDAO { // 和数据库打交道
    	public void save(User u); // 访问  mysql 的代码
    }

    package com.bjsxt.dao.impl;

    UserDAOImpl

    package com.bjsxt.dao.impl;
    
    import org.springframework.stereotype.Component;
    
    import com.bjsxt.dao.UserDAO;
    import com.bjsxt.model.User;
    
    @Component("u") 
    public class UserDAOImpl implements UserDAO {
    
    	public void save(User user) {
    		//Hibernate
    		//JDBC
    		//XML
    		//NetWork
    		System.out.println("user saved!");
    		//throw new RuntimeException("exeption!");
    	}
    
    }

    packagecom.bjsxt.model;

    User  (用户类 : 具有 名字 和 密码 两个属性)

    package com.bjsxt.model;
    
    public class User {
    	private String username;
    	private String password;
    
    	public String getUsername() {
    		return username;
    	}
    
    	public void setUsername(String username) {
    		this.username = username;
    	}
    
    	public String getPassword() {
    		return password;
    	}
    
    	public void setPassword(String password) {
    		this.password = password;
    	}
    
    }

    package com.bjsxt.service;
    UserService  
    用户服务层
    package com.bjsxt.service;
    import javax.annotation.Resource;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Component;
    
    import com.bjsxt.dao.UserDAO;
    import com.bjsxt.model.User;
    
    
    @Component("userService")
    public class UserService {
    	
    	private UserDAO userDAO;  
    	
    	public void init() {
    		System.out.println("init");
    	}
    	
    	public void add(User user) {
    		userDAO.save(user);
    	}
    	public UserDAO getUserDAO() {
    		return userDAO;
    	}
    	
    	@Resource(name="u")
    	public void setUserDAO( UserDAO userDAO) {
    		this.userDAO = userDAO;
    	}
    	
    
    	
    	public void destroy() {
    		System.out.println("destroy");
    	}
    }
    beans.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:context="http://www.springframework.org/schema/context"
      
    	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.springframework.org/schema/context     
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">   
        <context:annotation-config/>
        <context:component-scan base-package="com.bjsxt"/>     
    	
     	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
     	
    </beans>

    Test 目录

    package com.bjsxt.service;

    UserServiceTest

    package com.bjsxt.service;
    
    import static org.junit.Assert.*;
    
    import org.junit.Test;
    
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.bjsxt.dao.UserDAO;
    import com.bjsxt.model.User;
    
    public class UserServiceTest {
    
    	@Test
    	public void testAdd() throws Exception {
    		
    		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
    		
    		UserService service = (UserService) ctx.getBean("userService"); // new UserService();
    		
    		service.add(new User());
    		
    		ctx.destroy();
    	}
    }

    UserServiceTest 2

    package com.bjsxt.service;
    
    import static org.junit.Assert.*;
    
    import org.junit.Test;
    
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.bjsxt.dao.UserDAO;
    import com.bjsxt.model.User;
    
    public class UserServiceTest {
    
    	@Test
    	public void testAdd() throws Exception {
    		
    		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
    		
    		UserService service = (UserService) ctx.getBean("userService"); // new UserService();
    		System.out.println(service.getClass());
    		service.add(new User());
    		
    		ctx.destroy();
    	}
    }
    

      写程序推荐,面向接口编程。

    输出如下 :



  • 相关阅读:
    [AGC030F] Permutation and Minimum
    [GYM102904B]Dispatch Money
    [ZOJ3989]Triangulation
    [HDU6094]Rikka with KMatch
    CF739E Gosha is hunting
    [国家集训队]Tree I
    [BZOJ3864]Hero meet devil
    编程是一门实践性的科学
    IE下的flash加载中文URL文件 问题
    多线程1号
  • 原文地址:https://www.cnblogs.com/robbychan/p/3786815.html
Copyright © 2020-2023  润新知