zoukankan      html  css  js  c++  java
  • Sping--AOP--XML

    IoC: annotation

    AOP: XML

    XML比annotation用的多.

    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:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop-2.5.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>
    

    LogInterceptor.java:

    package com.bjsxt.aop;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    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");
    	}
    	
    }
    

    UserServiceTest.java:

    package com.bjsxt.service;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.bjsxt.model.User;
    
    //Dependency Injection
    //Inverse of Control
    public class UserServiceTest {
    
    	@Test 
    	public void testAdd() throws Exception {
    		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
    		
    		UserService service = (UserService)ctx.getBean("userService");
    		System.out.println(service.getClass());
    		service.add(new User());
    		
    		ctx.destroy();
    		
    	}
    }
    

      

    UserService.java:

    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");
    	}
    }
    

      

    UserDAOImpl.java:

    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!");
    	}
    
    }
    

    UserDAO.java:

    package com.bjsxt.dao;
    import com.bjsxt.model.User;
    
    
    public interface UserDAO {
    	public void save(User user);
    }
    

    User.java:

    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;
    	}
    }
    

      

    XML方式好? annotation方式好?

    XML必须掌握, 因为 

      

    ---恢复内容结束---

  • 相关阅读:
    jdk git maven Jenkins的配置
    jar包部署脚本
    Mybaits和Spring的那点事
    HashMap三两事
    排序算法学习
    MySQL杂谈
    一个最简单的微服务架构
    SpringBoot全家桶
    nginx上传
    使用xshell在linux服务器上部署node项目
  • 原文地址:https://www.cnblogs.com/wujixing/p/5462160.html
Copyright © 2011-2022 走看看