[Java] Spring_1600_AOP_XML

package com.bjsxt.aop;

LogInterceptor        这里有源码。 其实 : XML 的好处是没有源码的时候照样,可以AOP,所以IOC-Annotation 比较好, AOP-XML比较好

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"/>

    <bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean>
	<aop:config>
		<aop:aspect id="logAspect" ref="logInterceptor">
			<aop:before method="before" pointcut="execution(public * com.bjsxt.service..*.add(..))" />
		</aop:aspect>
	</aop:config>
</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();
	}
}

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

输出如下 :


原文地址:https://www.cnblogs.com/robbychan/p/3786814.html