javaEE——Spring 四种切面技术(拦截)、获取Spring容器的两种办法

Spring AOP编程

切面(Aspect):简单的理解就是把那些与核心业务无关的代码提取出来,进行封装成一个或几个模块用来处理那些附加的功能代码。(如日志,事务,安全验证)我们把这个模块的作用理解为一个切面,其实切面就是我们写一个类,这个类中的代码原来是在业务模块中完成的,现在单独成一个或几个类。在业务模块需要的时候才织入。

连接点(Joinpoint):在程序执行过程中某个特定的点,比如某方法调用的时候或者处理异常的时候。
在Spring AOP中,一个连接点总是代表一个方法的执行。通过声明一个JoinPoint类型的参数可以使通知(Advice)的主体部分获得连接点信息。

切入点(Pointcut):本质上是一个捕获连接点的结构。在AOP中,可以定义一个pointcut,来捕获相关方法的调用

织入(Weaving):把切面(aspect)连接到其它的应用程序类型或者对象上,并创建一个被通知(advised)的对象。这些可以在编译时,类加载时和运行时完成。Spring和其它纯Java
AOP框架一样,在运行时完成织入。

通知(Advice):在切面的某个特定的连接点(Joinpoint)上执行的动作。通知有各种类型,其中包括“around”、“before”和“after”等通知。通知的类型将在后面部分进行讨论。许多AOP框架,包括Spring,都是以拦截器做通知模型,并维护一个以连接点为中心的拦截器链。

通知的类型:

前置通知(Before advice):在某连接点(join
point)之前执行的通知,但这个通知不能阻止连接点前的执行(除非它抛出一个异常)。

返回后通知(After returning advice):在某连接点(join
point)正常完成后执行的通知:例如,一个方法没有抛出任何异常,正常返回。

抛出异常后通知(After throwing advice):在方法抛出异常退出时执行的通知。

后置通知(After(finally)advice):当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。

环绕通知(Around Advice):包围一个连接点(join
point)的通知,如方法调用。这是最强大的一种通知类型。 环绕通知可以在方法调用前后完成自定义的行为。它也会选择是否继续执行连接点或直接返回它们自己的返回值或抛出异常来结束执行

1.RegexpMethodPointcutAdvisor切面技术

下面演示代码:

纯Java方式写AOP(拦截技术)

package cn.hncu.spring4x.aop;

import java.lang.reflect.Method;

import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.Test;
import org.springframework.aop.Advisor;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.JdkRegexpMethodPointcut;

import cn.hncu.spring4x.domain.Person;

public class AopDemo {
	@Test//纯Java的方式实现切面(拦截)技术
	public void demo1(){
		Person p=new Person();
		ProxyFactory factory=new ProxyFactory(); //该类的功能没有ProxyFactoryBean强
		factory.setTarget(p);//1 给代理工厂一个原型对象
		//切面 = 切点 + 通知
		//切点
		JdkRegexpMethodPointcut pointcut=new JdkRegexpMethodPointcut();
		pointcut.setPattern("cn.hncu.spring4x.domain.Person.run");
		//		pointcut.setPattern(".*run.*");ProxyFactory对setPattern无效
		Advice advice=new MethodInterceptor() {

			@Override
			public Object invoke(MethodInvocation invocation) throws Throwable {
				System.out.println("前面拦截");
				Object obj=invocation.proceed();
				System.out.println("后面拦截");
				return obj;
			}
		};
		//切面 = 切点 + 通知
		Advisor advisor=new DefaultPointcutAdvisor(pointcut, advice);

		factory.addAdvice(advice);

		Person p2=(Person) factory.getProxy();
//		p2.run();
//		p2.run(5);
		p2.say();
	}
	@Test//纯Java的方式实现切面(拦截)技术
	public void demo2(){
		ProxyFactoryBean factoryBean=new ProxyFactoryBean();
		factoryBean.setTarget(new Person());
		//切面 = 切点 + 通知
		//切点
		JdkRegexpMethodPointcut pointcut=new JdkRegexpMethodPointcut();
		pointcut.setPattern(".*run.*");

		//通知  前切面---不需要放行,原方法也能执行
		Advice beforeAdvice=new MethodBeforeAdvice() {
			@Override
			public void before(Method method, Object[] args, Object target)
					throws Throwable {
				System.out.println("beforeAdvice拦截");//正则表达式有效
			}
		};
		Advice afterReturning=new AfterReturningAdvice() {
			@Override
			public void afterReturning(Object returnValue, Method method,
					Object[] args, Object target) throws Throwable {
				System.out.println("afterReturning");
			}
		};

		Advice aroundAdvice=new MethodInterceptor() {

			public Object invoke(MethodInvocation invocation) throws Throwable {
				System.out.println("前面拦截");
				Object obj=invocation.proceed();
				System.out.println("后面拦截");
				return obj;
			}
		};
		Advisor advisor1=new DefaultPointcutAdvisor(pointcut, beforeAdvice);
		Advisor advisor2=new DefaultPointcutAdvisor(pointcut, afterReturning);
		Advisor advisor3=new DefaultPointcutAdvisor(pointcut, aroundAdvice);
		factoryBean.addAdvisors(advisor1,advisor2,advisor3);
		//2 给代理工厂一个切面 ---注意,添加的顺序的拦截动作执行的顺序是有关系的!!!
		//先加的切面,如果拦前面,就拦在最前面,如果拦后面,就拦在最后面.
		Person p = (Person) factoryBean.getObject(); //3 从代理工厂中获取一个代理后的对象
		//p.run();
		//p.run(0);
		p.say();
	}

}

下面演示5种理由配置文件AOP

通知:AroundAdvice.java

package cn.hncu.spring4x.aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class AroundAdvice implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		System.out.println("前面拦拦....");
		Object resObj = invocation.proceed();//放行
		System.out.println("后面拦拦.....");
		return resObj;
	}

}

测试代码,

package cn.hncu.spring4x.aop;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AopXmlDemo {
	@Test//采用配置文件的方式使用切面拦截
	public void demo1(){
		ApplicationContext act=new ClassPathXmlApplicationContext("cn/hncu/spring4x/aop/1.xml");
		Cat cat=act.getBean("catProxide",Cat.class);//要从catProxide返回
		cat.run();
		cat.say();
		cat.run(6);
	}
	@Test//把切点和通知配置成 切面的内部bean
	public void demo2(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/spring4x/aop/2.xml");
		Cat cat = ctx.getBean("catProxide",Cat.class);
		cat.run();
		cat.say();
		cat.run(7);
	}
	@Test//直接在切面bean中配置“切点的正则表达式”,省去“切点bean”的配置
	public void demo3(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/spring4x/aop/3.xml");
		Cat cat = ctx.getBean("catProxide",Cat.class);
		cat.run();
		cat.say();
		cat.run(7);
	}
	@Test//自动代理
	public void demo4(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/spring4x/aop/4.xml");
		Cat cat = ctx.getBean(Cat.class);
		cat.run();
		cat.say();
		cat.run(7);
	}
	@Test//自己写的自动代理
	public void demo5(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/spring4x/aop/5.xml");
		Cat cat = ctx.getBean("cat",Cat.class);
//		cat.run();
//		cat.say();
//		cat.run(7);
	}

}

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

	<bean id="cat" class="cn.hncu.spring4x.aop.Cat"></bean>
	<!-- 切点 -->
	<bean id="pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
		<property name="pattern" value=".*run.*"></property>
	</bean>
	<!-- 通知 ,要自己写-->
	<bean id="advice" class="cn.hncu.spring4x.aop.AroundAdvice"></bean>

	<!-- 切面=切点+通知 -->
	<bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
		<property name="advice" ref="advice"></property>
		<property name="pointcut" ref="pointcut"></property>
	</bean>

	<bean id="catProxide" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="target" ref="cat"></property>
		<property name="interceptorNames">
			<list>
				<value>advisor</value>
			</list>
		</property>
	</bean>
</beans>

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

	<bean id="cat" class="cn.hncu.spring4x.aop.Cat"></bean>

	<!-- 切面=切点+通知 (把切点和通知写成内部bean)-->
	<bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
		<property name="advice">
			<bean class="cn.hncu.spring4x.aop.AroundAdvice"></bean>
		</property>
		<property name="pointcut">
			<bean class="org.springframework.aop.support.JdkRegexpMethodPointcut">
			 <property name="patterns">
			 	<list>
			 		<value>.*run.*</value>
			 		<value>.*say.*</value>
			 	</list>
			 </property>

			</bean>
		</property>
	</bean>

	<bean id="catProxide" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="target" ref="cat"></property>
		<property name="interceptorNames">
			<list>
				<value>advisor</value>
			</list>
		</property>
	</bean>
</beans>

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

	<bean id="cat" class="cn.hncu.spring4x.aop.Cat"></bean>

	<!--//直接在切面bean中配置“切点的正则表达式”,省去“切点bean”的配置 用到这个类 org.springframework.aop.support.RegexpMethodPointcutAdvisor -->
	<bean id="advisor"
		class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
		<property name="advice">
			<bean class="cn.hncu.spring4x.aop.AroundAdvice"></bean>
		</property>
		<property name="patterns">
			<list>
				<value>.*run.*</value>
			</list>
		</property>
	</bean>

	<bean id="catProxide" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="target" ref="cat"></property>
		<property name="interceptorNames">
			<list>
				<value>advisor</value>
			</list>
		</property>
	</bean>
</beans>

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

	<bean id="cat" class="cn.hncu.spring4x.aop.Cat"></bean>

	<!--//直接在切面bean中配置“切点的正则表达式”,省去“切点bean”的配置 用到这个类 org.springframework.aop.support.RegexpMethodPointcutAdvisor -->
	<bean id="advisor"
		class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
		<property name="advice">
			<bean class="cn.hncu.spring4x.aop.AroundAdvice"></bean>
		</property>
		<property name="patterns">
			<list>
				<value>.*run.*</value>
			</list>
		</property>
	</bean>
	<!-- 自动代理 -->
	<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
</beans>

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

	<bean id="cat" class="cn.hncu.spring4x.aop.Cat"></bean>

	<!--//直接在切面bean中配置“切点的正则表达式”,省去“切点bean”的配置 用到这个类 org.springframework.aop.support.RegexpMethodPointcutAdvisor -->
	<bean id="advisor"
		class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
		<property name="advice">
			<bean class="cn.hncu.spring4x.aop.AroundAdvice"></bean>
		</property>
		<property name="patterns">
			<list>
				<value>.*run.*</value>
			</list>
		</property>
	</bean>
	<!-- 自动代理 -->

	<bean class="cn.hncu.spring4x.aop.MyAutoProxy"></bean>
</beans>

第五种方法,模拟自动代理

MyAutoProxy.java

package cn.hncu.spring4x.aop;

import org.springframework.aop.Advisor;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class MyAutoProxy implements BeanPostProcessor,ApplicationContextAware{
	private ApplicationContext applicationContext;
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext=applicationContext;//保证是同一个容器

	}
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		System.out.println(bean+"postProcessBeforeInitialization");
		return bean; //直接放行(一定要)
	}
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {
		System.out.println(bean+"postProcessAfterInitialization");
		ProxyFactoryBean factoryBean=new ProxyFactoryBean();
		factoryBean.setTarget(bean);
		Advisor advisor=applicationContext.getBean("advisor", Advisor.class);
		factoryBean.addAdvisor(advisor);
		return factoryBean.getObject();
	}

}

2.AspectJExpressionPointcut切面技术

纯java代码演示:

Person.java

package cn.hncu.spring4x.aspectj;

public class Person {
	public void run(){
		System.out.println("run............");
	}
	public void run(int i){
		System.out.println(i+"run............");
	}
	public int run(String str,int i){
		System.out.println(str+"run............"+i);
		return 0;
	}
	public void run(String str){
		System.out.println(str+"run............");
	}
	public void say() {
		System.out.println("say............");
	}
	public Person say(String str) {
		System.out.println("say..........str.");
		return null;
	}
}

package cn.hncu.spring4x.aspectj;

import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.Test;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.aop.support.DefaultPointcutAdvisor;

public class AspectjDemo {

	@Test//纯java方式
	public void demo1(){
		ProxyFactoryBean factoryBean=new ProxyFactoryBean();
		factoryBean.setTarget(new Person());

		//声明一个aspectj切点
		AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
		//参数用切点语言来写
//		pointcut.setExpression("execution( void cn.hncu.spring4x.aspectj.Person.run() )");//拦: 空参空返回值的run方法
//		pointcut.setExpression("execution( void cn.hncu.spring4x.aspectj.Person.*() )");//拦:  空参空返回值的任意方法
//		pointcut.setExpression("execution( void cn.hncu.spring4x.aspectj.Person.*(String) )"); //拦:  只有1个String类型参数,空返回值的任意方法
//		pointcut.setExpression("execution( void cn.hncu.spring4x.aspectj.Person.*(*) )"); //拦:  有1个参数(类型不限),空返回值的任意方法
//		pointcut.setExpression("execution( void cn.hncu.spring4x.aspectj.Person.*(..) )"); //拦:  任意(个数和类型)参数,空返回值的任意方法
//		pointcut.setExpression("execution( void cn.hncu.spring4x.aspectj.Person.*(*,..) )"); //拦:  至少有1个参数(类型不限),空返回值的任意方法

//		pointcut.setExpression("execution( * cn.hncu.spring4x.aspectj.Person.*(*,*) )"); //拦:  有2个参数(类型不限),任意返回值的任意方法
		pointcut.setExpression("execution( cn.hncu.spring4x.aspectj.Person cn.hncu.spring4x.aspectj.Person.*(*,..) )"); //拦:  至少有1个参数(类型不限),返回值类型是Person的任意方法——不是基础数据类型要用全名
		pointcut.setExpression("execution( * cn.hncu..**son.*(..) )"); //拦: cn.hncu包下,类名以"son"结束,   函数、返回类型和参数任意
		Advice advice=new MethodInterceptor() {
			@Override
			public Object invoke(MethodInvocation invocation) throws Throwable {
				System.out.println("前面拦拦");
				Object obj=invocation.proceed();
				System.out.println("后面拦拦");
				return obj;
			}
		};
		//切面=切点+通知
		Advisor advisor=new DefaultPointcutAdvisor(pointcut, advice);
		factoryBean.addAdvisor(advisor);
		Person p=(Person) factoryBean.getObject();
		p.run();
		p.run(5);
		p.run("有返回值有int0",5);
		p.run("没有返回值");
		p.say();
	}
}

xml配置文件演示

测试代码

package cn.hncu.spring4x.aspectj;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AspectjXmlDemo {
	@Test
	public void demo(){
		ApplicationContext act=new ClassPathXmlApplicationContext("cn/hncu/spring4x/aspectj/aspectj.xml");
		Person p=act.getBean("person", Person.class);
		p.run();
		p.run(5);
		p.run("有返回值有int0",5);
		p.run("没有返回值");
		p.say();
	}
	@Test
	public void demo2(){
		ApplicationContext act=new ClassPathXmlApplicationContext("cn/hncu/spring4x/aspectj/aspectj2.xml");
		Person p=act.getBean("person", Person.class);
		p.run();
		p.run(5);
		p.run("有返回值有int0",5);
		p.run("没有返回值");
		p.say();
	}
}

AroundAdvice通知

package cn.hncu.spring4x.aspectj;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class AroundAdvice implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		System.out.println("前面拦拦....");
		Object resObj = invocation.proceed();//放行
		System.out.println("后面拦拦.....");
		return resObj;
	}

}

aspectj.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:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	<bean id="person" class="cn.hncu.spring4x.aspectj.Person"></bean>
	<bean id="advice" class="cn.hncu.spring4x.aspectj.AroundAdvice"></bean>
	<bean id="pointcut" class="org.springframework.aop.aspectj.AspectJExpressionPointcut">
		<property name="expression" value="execution( * cn.hncu..*son.*(*,..) )"></property>
	</bean>
	<!-- 切面=切点+通知 (※※采用面向切点语言进行配置切面)-->
	<bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
		<property name="advice" ref="advice"></property>
		<property name="pointcut" ref="pointcut"></property>
	</bean>
	<!-- 自动代理 -->
	<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
</beans>

aspectj2.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:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	<bean id="person" class="cn.hncu.spring4x.aspectj.Person"></bean>
	<!-- 切面=切点+通知 (※※采用面向切点语言进行配置切面)org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor -->
	<bean id="advisor"
		class="org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor">
		<property name="expression" value="execution( * cn.hncu..*son.*(*,..) )"></property>
		<property name="advice">
			<bean id="advice" class="cn.hncu.spring4x.aspectj.AroundAdvice"></bean>
		</property>
	</bean>
	<!-- 自动代理 -->
	<bean
		class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
</beans>
时间: 2024-08-10 19:17:12

javaEE——Spring 四种切面技术(拦截)、获取Spring容器的两种办法的相关文章

strus2中获取表单数据 两种方式 属性驱动 和模型驱动

strus2中获取表单数据 两种方式 属性驱动 和模型驱动 属性驱动 /** * 当前请求的action在栈顶,ss是栈顶的元素,所以可以利用setValue方法赋值* 如果一个属性在对象栈,在页面上可以根据name属性进行回显*/ /** * 属性驱动实现的条件:* 1.当前请求的action在栈顶,所以action中的属性就暴漏出来了* 2.获取页面上表单的元素,整合成一个map * 3.调用setValue方法赋值*/ 1 package cn.itcast.struts2.sh; 2 3

JAVA EE 项目经常使用知识 之AJAX技术实现select下拉列表联动的两种使用方法(让你真正理解ajax)

ajax 下拉列表联动的使用方法. ajax的定义: AJAX 是一种用于创建高速动态网页的技术. 通过在后台与server进行少量数据交换,AJAX 能够使网页实现异步更新.这意味着能够在不又一次载入整个网页的情况下,对网页的某部分进行更新. ajax效果的一个样例: 区域为空的时候,维护人情况: 选了一个区域后的情况:(选 舒城县 联带出来的维护人员 小刘) 一.原生态的js实现 XMLHttpRequest 是 AJAX 的基础 XMLHttpRequest 对象 全部现代浏览器均支持 X

js+jquery动态设置/增加/删除/获取元素属性的两种方法集锦对比(动态onclick属性设置+动态title设置)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html140/strict.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title>

获取body标签的两种方法

获取body标签的两种方法 引用body标签有两做法: 第一种:使用DOM Core 即引用某个给定文档的第一个(也是仅有的一个)body标签 代码如下: document.getElementsByTagName("body")[0] 第二种:使用HTML-DOM,即引用某个给定文档的body属性: 代码如下: doucumnet.body

JavaWeb后台从input表单获取文本值的两种方式

JavaWeb后台从input表单获取文本值的两种方式 #### index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <input type="text" name="n

Spring Boot2 系列教程(十五)定义系统启动任务的两种方式

在 Servlet/Jsp 项目中,如果涉及到系统任务,例如在项目启动阶段要做一些数据初始化操作,这些操作有一个共同的特点,只在项目启动时进行,以后都不再执行,这里,容易想到web基础中的三大组件( Servlet.Filter.Listener )之一 Listener ,这种情况下,一般定义一个 ServletContextListener,然后就可以监听到项目启动和销毁,进而做出相应的数据初始化和销毁操作,例如下面这样: public class MyListener implements

微信网页开发之获取用户unionID的两种方法--基于微信的多点登录用户识别

假设网站A有以下功能需求:1,pc端微信扫码登录:2,微信浏览器中的静默登录功能需求,这两种需求就需要用到用户的unionID,这样才能在多个登录点(终端)识别用户.那么这两种需求下用户的unionID该如何获取呢? 1,先看pc端的解决方案 以snsapi_login为scope发起网页授权,先拿网站应用的appid和secret用授权接口获取"网页授权access_token",再利用"网页授权access_token"通过"拉取用户信息的api接口&

Spring容器托管两种方法

Spring最核心的就是IOC(控制反转)和AOP(面向切面编程). IOC可以理解为把Spring当做一个容器,用来管理各种service.dao等.不用再去手动new. 将service.dao等注册到spring容器中,有两种办法: 1.在xml中定义bean,比如: <bean class="com.xxx.trade.common.xxx.xxx"/> 2.通过注解. 常用的注解有 @Controller  主要是controller层. @Service  业务

JavaWeb获取请求参数的两种方式

第一种:通过获取缓冲流读取参数 public void registerUser(HttpServletRequest request, HttpServletResponse response)throws  Exception  { //直接获取请求参数,通过缓冲流读取数据  BufferedReader br=request.getReader();  String inputLine = "";  String json = "";      while (