Share on Google+Share on Google+

Pointcut NameMethodMatch using BeanFactory

Advertisement
In this example you will learn how to match the method name using BeanFactory.

Pointcut NameMethodMatch using BeanFactory

A pointcut is a bunch of codes that picks out join points and exposes data from the execution context of those join points.Following is an example of NameMethodMatch using springConfig.xml.

SimpleBean.java

package roseindia.net.bean;

public class SimpleBean {
	String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void displayName() {
		System.out.println("Hello " + this.name);
	}
}

MainClaz.java

package roseindia.net.main;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import roseindia.net.bean.SimpleBean;

public class MainClaz {
	public static void main(String[] args) throws Exception {
		BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
				"springconfig/config.xml"));
		SimpleBean simpleBean = (SimpleBean) beanFactory.getBean("testBean");
		simpleBean.displayName();
	}
}

config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
  <bean id="testBean" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="target">
      <bean class="roseindia.net.bean.SimpleBean">
        <property name="name" value="Vinay" />
      </bean>
    </property>
    <property name="interceptorNames">
      <list>
        <idref bean="methodMatchPintcut" />
      </list>
    </property>
    <property name="proxyTargetClass" value="true" />
  </bean>
  <bean id="methodMatchPintcut"
    class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"
    singleton="false">
    <property name="advice" ref="concurrencyThrottleInterceptor" />
    <property name="mappedName" value="displayName" />
  </bean>
  <bean id="concurrencyThrottleInterceptor"
    class="org.springframework.aop.interceptor.ConcurrencyThrottleInterceptor"
    singleton="false">
    <property name="concurrencyLimit" value="10" />
  </bean>

</beans>

When you run this application it will display message as shown below:


Hello Vinay

Download this example code

Advertisements

Advertisement

Posted on: September 8, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.