首页 > 其他分享 >spring aop xml方式

spring aop xml方式

时间:2023-04-26 22:02:25浏览次数:34  
标签:xml -- spring springframework context aop org import public


工程如图:

spring aop xml方式_System

spring aop xml方式_System_02

spring aop xml方式_spring_03


pom文件内容:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">


	<modelVersion>4.0.0</modelVersion>
	<groupId>com.springinaction</groupId>
	<artifactId>springidol-aop</artifactId>
	<version>3.0.0</version>
	<packaging>jar</packaging>

	<name>Spring in Action Spring Idol - AOP</name>
	<url>http://www.manning.com/walls4</url>

	<properties>
		<spring.group>org.springframework</spring.group>
		<spring.version>3.1.1.RELEASE</spring.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>${spring.group}</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>${spring.group}</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>${spring.group}</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.6.2</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.6.2</version>
		</dependency>

		<dependency>
			<groupId>${spring.group}</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.6</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>




声明前置和后置通知



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

	<bean id="eddie" class="com.springinaction.springidol.Instrumentalist">
		<property name="instrument">
			<bean class="com.springinaction.springidol.Guitar" />
		</property>
	</bean>

	<!--<start id="audience_bean" /> -->
	<bean id="audience" class="com.springinaction.springidol.Audience" />
	<!--<end id="audience_bean" /> -->

	<!--<start id="audience_aspect" /> -->
	<aop:config>
		<aop:aspect ref="audience"><!--<co id="co_refAudienceBean"/> -->

			<aop:before
				pointcut="execution(* com.springinaction.springidol.Performer.perform(..))"
				method="takeSeats" /> <!--<co id="co_beforePointcut"/> -->

			<aop:before
				pointcut="execution(* com.springinaction.springidol.Performer.perform(..))"
				method="turnOffCellPhones" /> <!--<co id="co_beforePointcut2"/> -->

			<aop:after-returning
				pointcut="execution(* com.springinaction.springidol.Performer.perform(..))"
				method="applaud" /> <!--<co id="co_afterPointcut"/> -->

			<aop:after-throwing
				pointcut="execution(* com.springinaction.springidol.Performer.perform(..))"
				method="demandRefund" /> <!--<co id="co_afterThrowingPointcut"/> -->

		</aop:aspect>
	</aop:config>
	<!--<end id="audience_aspect" /> -->

</beans>



接口:

package com.springinaction.springidol;

public interface Instrument {
	public void play();
}



package com.springinaction.springidol;

public interface Performer {
	void perform() throws PerformanceException;
}



实现:

package com.springinaction.springidol;

public class Guitar implements Instrument {
	public void play() {
		System.out.println("Strum strum strum");
	}
}



package com.springinaction.springidol;

public class Instrumentalist implements Performer {
	public void perform() throws PerformanceException {
		instrument.play();
	}

	private Instrument instrument;

	public void setInstrument(Instrument instrument) {
		this.instrument = instrument;
	}

	public Instrument getInstrument() {
		return instrument;
	}
}



Audience:

package com.springinaction.springidol;

public class Audience {
	public void takeSeats() {
		System.out.println("The audience is taking their seats.");
	}

	public void turnOffCellPhones() {
		System.out.println("The audience is turning off their cellphones");
	}

	public void applaud() {
		System.out.println("CLAP CLAP CLAP CLAP CLAP");
	}

	public void demandRefund() {
		System.out.println("Boo! We want our money back!");
	}
}




测试类:

package com.springinaction.springidol;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.springinaction.springidol.Performer;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("spring-idol.xml")
public class TestSpringIdol {
	@Autowired
	ApplicationContext context;
	
	@Test
	public void audienceShouldApplaud() throws Exception {
		Performer eddie = (Performer) context.getBean("eddie");
		eddie.perform();
	}
}



运行输出:

2014-4-19 12:32:42 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners
信息: @TestExecutionListeners is not present for class [class com.springinaction.springidol.TestSpringIdol]: using defaults.
2014-4-19 12:32:43 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/springinaction/springidol/spring-idol.xml]
2014-4-19 12:32:43 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@111a3ac: startup date [Sat Apr 19 12:32:43 CST 2014]; root of context hierarchy
2014-4-19 12:32:43 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@23e5d1: defining beans [eddie,audience,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,org.springframework.aop.aspectj.AspectJPointcutAdvisor#2,org.springframework.aop.aspectj.AspectJPointcutAdvisor#3,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
The audience is taking their seats.
The audience is turning off their cellphones
Strum strum strum
CLAP CLAP CLAP CLAP CLAP




声明环绕通知


AroundAudience:

package com.springinaction.springidol;

import org.aspectj.lang.ProceedingJoinPoint;

public class AroundAudience {
	public void watchPerformance(ProceedingJoinPoint joinpoint) {
		try {
			System.out.println("The audience is taking their seats.");
			System.out.println("The audience is turning off their cellphones");
			long start = System.currentTimeMillis();

			joinpoint.proceed();

			long end = System.currentTimeMillis();
			System.out.println("CLAP CLAP CLAP CLAP CLAP");
			System.out.println("The performance took " + (end - start) + " milliseconds.");
		} catch (Throwable t) {
			System.out.println("Boo! We want our money back!");
		}
	}
}



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

	<bean id="eddie" class="com.springinaction.springidol.Instrumentalist">
		<property name="instrument">
			<bean class="com.springinaction.springidol.Guitar" />
		</property>
	</bean>

	<!-- <start id="audience_bean" /> -->
	<bean id="audience" class="com.springinaction.springidol.AroundAudience" />
	<!-- <end id="audience_bean" /> -->

	<!-- <start id="audience_aspect" /> -->
	<aop:config>
		<aop:aspect ref="audience">
			<aop:pointcut id="performance"
				expression="execution(* com.springinaction.springidol.Performer.perform(..))" />

			<aop:around pointcut-ref="performance" method="watchPerformance" />
		</aop:aspect>
	</aop:config>
	<!-- <end id="audience_aspect" /> -->

</beans>



测试类:

package com.springinaction.springidol;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("spring-idol-around.xml")
public class TestSpringIdolAround {
	@Autowired
	ApplicationContext context;

	@Test
	public void audienceShouldApplaud() throws Exception {
		Performer eddie = (Performer) context.getBean("eddie");
		eddie.perform();
	}
}



运行输出:

2014-4-19 12:40:12 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners
信息: @TestExecutionListeners is not present for class [class com.springinaction.springidol.TestSpringIdolAround]: using defaults.
2014-4-19 12:40:13 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/springinaction/springidol/spring-idol-around.xml]
2014-4-19 12:40:13 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@dd20f6: startup date [Sat Apr 19 12:40:13 CST 2014]; root of context hierarchy
2014-4-19 12:40:13 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@177b3cd: defining beans [eddie,audience,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,performance,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
The audience is taking their seats.
The audience is turning off their cellphones
Strum strum strum
CLAP CLAP CLAP CLAP CLAP
The performance took 0 milliseconds.




前置、后置、环绕


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

	<bean id="eddie" class="com.springinaction.springidol.Instrumentalist">
		<property name="instrument">
			<bean class="com.springinaction.springidol.Guitar" />
		</property>
	</bean>

	<!--<start id="audience_bean" /> -->
	<bean id="audience" class="com.springinaction.springidol.AroundAudience2" />
	<!--<end id="audience_bean" /> -->

	<!--<start id="audience_aspect" /> -->
	<aop:config>
		<aop:aspect ref="audience">
			<aop:pointcut id="performance"
				expression="execution(* com.springinaction.springidol.Performer.perform(..))" /> <!--<co id="co_defPointcut"/> -->

			<aop:before pointcut-ref="performance" method="takeSeats" /> <!--<co id="co_refPointcut"/> -->
			<aop:before pointcut-ref="performance" method="turnOffCellPhones" /> <!--<co id="co_refPointcut"/> -->
			<aop:after-returning pointcut-ref="performance"
				method="applaud" /> <!--<co id="co_refPointcut"/> -->
			<aop:after-throwing pointcut-ref="performance"
				method="demandRefund" /> <!--<co id="co_refPointcut"/> -->
		</aop:aspect>
	</aop:config>
	<!--<end id="audience_aspect" /> -->

	<!--<start id="audience_around_advice" /> -->
	<aop:config>
		<aop:aspect ref="audience">
			<aop:pointcut id="performance2"
				expression="execution(* com.springinaction.springidol.Performer.perform(..))" />

			<aop:around pointcut-ref="performance2" method="watchPerformance" /> <!--<co id="co_around"/> -->
		</aop:aspect>
	</aop:config>
	<!--<end id="audience_around_advice" /> -->
</beans>




AroundAudience2:

package com.springinaction.springidol;

import org.aspectj.lang.ProceedingJoinPoint;

public class AroundAudience2 extends Audience {

	public void watchPerformance(ProceedingJoinPoint joinpoint) {
		try {
			long start = System.currentTimeMillis();
			joinpoint.proceed();
			long end = System.currentTimeMillis();
			System.out.println("The performance took " + (end - start) + " milliseconds.");
		} catch (Throwable t) {
			System.out.println("Boo! We want our money back!");
		}
	}
}




测试类:

package com.springinaction.springidol;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.springinaction.springidol.Performer;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("spring-idol-2.xml")
public class TestSpringIdol2 {
	@Autowired
	ApplicationContext context;
	
	@Test
	public void audienceShouldApplaud() throws Exception {
		Performer eddie = (Performer) context.getBean("eddie");
		eddie.perform();
	}
}



运行输出:

2014-4-19 12:51:47 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners
信息: @TestExecutionListeners is not present for class [class com.springinaction.springidol.TestSpringIdol2]: using defaults.
2014-4-19 12:51:47 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/springinaction/springidol/spring-idol-2.xml]
2014-4-19 12:51:47 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@b89838: startup date [Sat Apr 19 12:51:47 CST 2014]; root of context hierarchy
2014-4-19 12:51:47 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@c4fe76: defining beans [eddie,audience,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,org.springframework.aop.aspectj.AspectJPointcutAdvisor#2,org.springframework.aop.aspectj.AspectJPointcutAdvisor#3,performance,org.springframework.aop.aspectj.AspectJPointcutAdvisor#4,performance2,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
The audience is taking their seats.
The audience is turning off their cellphones
Strum strum strum
The performance took 0 milliseconds.
CLAP CLAP CLAP CLAP CLAP




为通知传递参数


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

	<bean id="volunteer" class="com.springinaction.springidol.Volunteer" />
	<bean id="magician" class="com.springinaction.springidol.Magician" />

	<!--<start id="mindreading_aspect"/> -->
	<aop:config>
		<aop:aspect ref="magician">
			<aop:pointcut id="thinking"
				expression="execution(* com.springinaction.springidol.Thinker.thinkOfSomething(String)) and args(thoughts)" />

			<aop:before pointcut-ref="thinking" method="interceptThoughts"
				arg-names="thoughts" />
		</aop:aspect>
	</aop:config>
	<!--<end id="mindreading_aspect"/> -->

</beans>



Thinker:

package com.springinaction.springidol;

public interface Thinker {
	void thinkOfSomething(String thoughts);
}



Volunteer:


package com.springinaction.springidol;

public class Volunteer implements Thinker {
	private String thoughts;

	public void thinkOfSomething(String thoughts) {
		this.thoughts = thoughts;
	}

	public String getThoughts() {
		return thoughts;
	}
}



MindReader:


package com.springinaction.springidol;

public interface MindReader {
	void interceptThoughts(String thoughts);

	String getThoughts();
}



Magician:


package com.springinaction.springidol;

public class Magician implements MindReader {
	private String thoughts;

	public void interceptThoughts(String thoughts) {
		System.out.println("Intercepting volunteer's thoughts");
		this.thoughts = thoughts;
	}

	public String getThoughts() {
		return thoughts;
	}
}



测试类:

package com.springinaction.springidol;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("sneaky-magician.xml")
public class TestSneakyMagician {
	@Autowired
	ApplicationContext context;

	@Autowired
	Thinker thinker;

	@Autowired
	MindReader mindReader;

	@Test
	public void audienceShouldApplaud() throws Exception {
		String thoughts = "this is spring aop";
		thinker.thinkOfSomething(thoughts);
		System.out.println(mindReader.getThoughts());
	}
}



运行输出:

2014-4-19 13:08:43 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners
信息: @TestExecutionListeners is not present for class [class com.springinaction.springidol.TestSneakyMagician]: using defaults.
2014-4-19 13:08:43 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/springinaction/springidol/sneaky-magician.xml]
2014-4-19 13:08:43 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@b89838: startup date [Sat Apr 19 13:08:43 CST 2014]; root of context hierarchy
2014-4-19 13:08:44 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1a99561: defining beans [volunteer,magician,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,thinking,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
Intercepting volunteer's thoughts
this is spring aop




引入新方法


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

	<bean id="eddie" class="com.springinaction.springidol.Instrumentalist">
		<property name="instrument">
			<bean class="com.springinaction.springidol.Guitar" />
		</property>
	</bean>

	<!--<start id="audience_bean"/> -->
	<bean id="audience" class="com.springinaction.springidol.Audience" />
	<!--<end id="audience_bean"/> -->

	<!--<start id="audience_aspect"/> -->
	<aop:config>
		<aop:aspect ref="audience">
			<aop:pointcut id="performance"
				expression="execution(* com.springinaction.springidol.Performer.perform(..))" />
			<aop:before pointcut-ref="performance" method="takeSeats()" />
			<aop:before pointcut-ref="performance" method="turnOffCellPhones" />
			<aop:after-returning pointcut-ref="performance"
				method="applaud" />
			<aop:after-throwing pointcut-ref="performance"
				method="demandRefund" />
		</aop:aspect>

		<!--<start id="contestant_introduction"/> -->
		<aop:aspect>
			<aop:declare-parents types-matching="com.springinaction.springidol.Performer+"
				implement-interface="com.springinaction.springidol.Contestant"
				default-impl="com.springinaction.springidol.GraciousContestant" />
		</aop:aspect>
		<!--<end id="contestant_introduction"/> -->

		<!-- <start id="delegate_ref"/> <aop:declare-parents types-matching="com.springinaction.springidol.Performer+" 
			implement-interface="com.springinaction.springidol.Contestant" delegate-ref="contestantDelegate" 
			/> <end id="delegate_ref"/> <start id="contestant_delegate"/> <bean id="contestantDelegate" 
			class="com.springinaction.springidol.GraciousContestant" /> <end id="contestant_delegate"/> -->

	</aop:config>
	<!--<end id="audience_aspect" /> -->

</beans>



Contestant:


package com.springinaction.springidol;

public interface Contestant {
	void receiveAward();
}



GraciousContestant:


package com.springinaction.springidol;

public class GraciousContestant implements Contestant {

	public void receiveAward() {
		System.out.println("Why, thank you all very much!");
	}
}



测试类:

package com.springinaction.springidol;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("spring-idol-introductions.xml")
public class TestSpringIdolIntroductions {
	@Autowired
	ApplicationContext context;

	@Autowired
	Performer performer;
	
	Contestant contestant;

	@Test
	public void audienceShouldApplaud() throws Exception {
		performer.perform();
		contestant = (Contestant) performer;
		contestant.receiveAward();
	}
}



运行输出:

2014-4-19 13:18:47 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners
信息: @TestExecutionListeners is not present for class [class com.springinaction.springidol.TestSpringIdolIntroductions]: using defaults.
2014-4-19 13:18:47 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/springinaction/springidol/spring-idol-introductions.xml]
2014-4-19 13:18:47 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@b89838: startup date [Sat Apr 19 13:18:47 CST 2014]; root of context hierarchy
2014-4-19 13:18:47 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@b8c8e6: defining beans [eddie,audience,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,org.springframework.aop.aspectj.AspectJPointcutAdvisor#2,org.springframework.aop.aspectj.AspectJPointcutAdvisor#3,performance,org.springframework.aop.aspectj.DeclareParentsAdvisor#0,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
The audience is taking their seats.
The audience is turning off their cellphones
Strum strum strum
CLAP CLAP CLAP CLAP CLAP
Why, thank you all very much!





标签:xml,--,spring,springframework,context,aop,org,import,public
From: https://blog.51cto.com/u_1002776/6229005

相关文章

  • <packaging>war</packaging>在pom.xml文件里添加这个会导致404的出现
    Maven,javaweb我在pom.xml文件里面添加了这行代码<packaging>war</packaging>就会导致jsp界面的运行失败但是还无道理呀,以往都可以运行成功的啊我通过一行一行注释代码创建项目找到的答案,找了我好久。<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.......
  • spring jdbc 声明式事务
    Spring的声明式事务顾名思义就是采用声明的方式来处理事务。这里所说的声明,就是指在配置文件中申明。用在Spring配置文件中声明式的处理事务来代替代码式的处理事务。这样的好处是,事务管理不侵入开发的组件,具体来说,业务逻辑对象就不会意识到正在事务管理之中,事实上也应该如此,因为事......
  • SpringSecurity从入门到精通:认证成功处理器&认证失败处理器
    认证成功处理器  认证失败处理器  ......
  • 第三十章:XML
    学习要点:1.IE中的XML2.DOM2中的XML3.跨浏览器处理XML随着互联网的发展,Web应用程序的丰富,开发人员越来越希望能够使用客户端来操作XML技术。而XML技术一度成为存储和传输结构化数据的标准。所以,本章就详细探讨一下JavaScript中使用XML的技术。对于什么是XML,干什么用......
  • XXE(xml外部实体攻击)
    1、概念XXE(XMLExternalEntity)是指xml外部实体攻击漏洞。XML外部实体攻击是针对解析XML输入的应用程序的一种攻击。当包含对外部实体的引用的XML输入被弱配置XML解析器处理时,就会发生这种攻击。这种攻击通过构造恶意内容,可导致读取任意文件、执行系统命令、探测内网端口、攻击内......
  • [转]前端传嵌套对象参数给spring mvc
    在使用springmvc开发web应用时,感觉springmvc的controller方法能自动将参数注入到方法的参数对象中,极大的方便了开发。但是,在遇到有嵌套对象的时候,比如订单对象有个属性是用户对象,就不好处理了。一种情况是,传递的参数都是作为post方法的请求体,我们可以用RequestBody注解。但是当条......
  • SpringSecurity从入门到精通:其他权限校验方法&自定义权限校验方法
    其他权限校验方法我们前面都是使用@PreAuthorize注解,然后在在其中使用的是hasAuthority方法进行校验。SpringSecurity还为我们提供了其它方法例如:hasAnyAuthority,hasRole,hasAnyRole等。​这里我们先不急着去介绍这些方法,我们先去理解hasAuthority的原理,然后再去学......
  • 5.spring 中的 bean 是线程安全的吗?
    https://www.mianshigee.com/question/10477vxbhttps://www.zhihu.com/pin/1365332853371297792  ......
  • 4.Spring中IOC创建对象的两种方式和创建时机
    https://blog.csdn.net/jike11231/article/details/116357864 创建时机1:默认的在Spring容器启动的时候2:在getBean的时候创建对象(就是拿到配置文件中的标识符的时候创建对象)在Spring的配置文件bean中有一个属性lazy-init=“default/true/false”(1)如果lazy-init为"default/f......
  • ajax中responseText与responseXML区别
    源:http://lou888.blog.hexun.com/46543491_d.html评:一、ajax中responseText与responseXML区别1、"responseText”属性以字符串形式返回HTTP响应;“responseXML”属性以XML形式返回HTTP响应。functiongetTel(){vartelText=document.getElement......