Este conteúdo não está disponível no idioma selecionado.

7.5. XML (or MetaData) Annotations Support


AOP support is a prime feature in JBoss Microcontainer. You can use AOP aspects and plain beans in any combination. Example 7.10, “Intercepting a Method based on Annotation” attempts to intercept a method invocation based on an annotation. The annotation can come from anywhere. It might be a true class annotation or an annotation added through the xml configuration.

Example 7.10. Intercepting a Method based on Annotation

<interceptor xmlns="urn:jboss:aop-beans:1.0" name="StopWatchInterceptor" class="org.jboss.demos.ioc.annotations.StopWatchInterceptor"/>

<bind xmlns="urn:jboss:aop-beans:1.0" pointcut="execution(* @org.jboss.demos.ioc.annotations.StopWatchLog->*(..)) OR execution(* *->@org.jboss.demos.ioc.annotations.StopWatchLog(..))">
  <interceptor-ref name="StopWatchInterceptor"/>
</bind>
</interceptor>
			
			
			
			

Copy to Clipboard Toggle word wrap
public class StopWatchInterceptor implements Interceptor {
    ...
	public Object invoke(Invocation invocation) throws Throwable
    {
	Object target = invocation.getTargetObject();
	long time = System.currentTimeMillis();
	log.info("Invocation [" + target + "] start: " + time);
	try
	    {
		return invocation.invokeNext();
	    }
	finally
	    {
		log.info("Invocation [" + target + "] time: " + (System.currentTimeMillis() - time));
	    }
    }
}    
			
			
			
			

Copy to Clipboard Toggle word wrap

Example 7.11. A true class annotated executor

<bean name="AnnotatedExecutor" class="org.jboss.demos.ioc.annotations.AnnotatedExecutor">
			
			
			
			

Copy to Clipboard Toggle word wrap
public class AnnotatedExecutor implements Executor {
    ...
	@StopWatchLog // <-- Pointcut match!
	    public void execute() throws Exception {
	delegate.execute();
    }
}
			
			
			
			

Copy to Clipboard Toggle word wrap

Example 7.12. Simple executor with XML annotation

<bean name="SimpleExecutor" class="org.jboss.demos.ioc.annotations.SimpleExecutor">
  <annotation>@org.jboss.demos.ioc.annotations.StopWatchLog</annotation> // <-- Pointcut match!
</bean>
			
			
			
			

Copy to Clipboard Toggle word wrap
public class SimpleExecutor implements Executor {
    private static Random random = new Random();
    public void execute() throws Exception
    {
	Thread.sleep(Math.abs(random.nextLong() % 101));
    }
}
			
			
			
			

Copy to Clipboard Toggle word wrap
After adding executor invoker beans, you can see the executors in action during employment, by looking for log output such as Example 7.13, “Executor Logging Output”.

Example 7.13. Executor Logging Output

		  JBoss-MC-Demo  INFO [15-12-2008 13:57:39] StopWatch - Invocation [org.jboss.demos.ioc.annotations.AnnotatedExecutor@4d28c7] start: 1229345859234
		  JBoss-MC-Demo  INFO [15-12-2008 13:57:39] StopWatch - Invocation [org.jboss.demos.ioc.annotations.AnnotatedExecutor@4d28c7] time: 31
		  JBoss-MC-Demo  INFO [15-12-2008 13:57:39] StopWatch - Invocation [org.jboss.demos.ioc.annotations.SimpleExecutor@1b044df] start: 1229345859265
		  JBoss-MC-Demo  INFO [15-12-2008 13:57:39] StopWatch - Invocation [org.jboss.demos.ioc.annotations.SimpleExecutor@1b044df] time: 47
Copy to Clipboard Toggle word wrap
Voltar ao topo
Red Hat logoGithubredditYoutubeTwitter

Aprender

Experimente, compre e venda

Comunidades

Sobre a documentação da Red Hat

Ajudamos os usuários da Red Hat a inovar e atingir seus objetivos com nossos produtos e serviços com conteúdo em que podem confiar. Explore nossas atualizações recentes.

Tornando o open source mais inclusivo

A Red Hat está comprometida em substituir a linguagem problemática em nosso código, documentação e propriedades da web. Para mais detalhes veja o Blog da Red Hat.

Sobre a Red Hat

Fornecemos soluções robustas que facilitam o trabalho das empresas em plataformas e ambientes, desde o data center principal até a borda da rede.

Theme

© 2025 Red Hat