Spring Auto Proxy Bean – BeanNameAutoProxyCreator

The BeanNameAutoProxyCreator automatically creates AOP proxies for beans with names matching literal values or wildcards. As with ProxyFactoryBean, there is an interceptorNames property rather than a list of interceptor, to allow correct behavior for prototype advisors. Named “interceptors” can be advisors or any advice type.

As with auto proxying in general, the main point of using BeanNameAutoProxyCreator is to apply the same configuration consistently to multiple objects, and with minimal volume of configuration. It is a popular choice for applying declarative transactions to multiple objects.

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

    <!-- Proxy class need this library -->

1. Business Logic and Implementation

We will first write our business logic and then we will add Spring AOP to profile our business methods.

Create an interface BusinessService and write a following method. We will intercept this via Advice


package com.kruders.spring.aop;
public interface BusinessService {
    void doSomeThing();

Now create a class BusinessImpl.java that implements the above interface.


package com.kruders.spring.aop;
import org.springframework.stereotype.Service;
public class BusinessImpl implements BusinessService {
    public void doSomeThing() {
        System.out.println("Do Something Here");

2. Configure Spring AOP

Create Spring-Business.xml and write the following code


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="businessService" class="com.kruders.spring.aop.BusinessImpl" />
    <!-- Advice -->
    <bean id="businessAdvice" class="com.kruders.spring.advice.BusinessAdvice" />
    <bean id="businessAdvisor"
		<property name="mappedName" value="doSomeThing" />
		<property name="advice" ref="businessAdvice" />
    <bean id="businessServiceProxy" 
		<property name="beanNames">
		<property name="interceptorNames">

Bean definitions whose names match, such as “*Service” in the above example, is plain old bean definition with the target class. An AOP proxy will be created automatically by theBeanNameAutoProxyCreator.

3. Advice Class

Now write a Advice which will profile our business method.


package com.kruders.spring.advice;

import java.util.Arrays;

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

public class BusinessAdvice implements MethodInterceptor {
	public Object invoke(MethodInvocation method) throws Throwable {
		System.out.println("Method name : " + method.getMethod().getName());
		System.out.println("Method arguments : " + Arrays.toString(method.getArguments()));
		System.out.println("Around method is called");
	    System.out.println("Around before is running");
		System.out.println("Around after is running");
		return null;

4. Helper Class

Create Main.java class that loads our Business bean from Spring Context and then calling our business method.


package com.kruders.spring.core;

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

import com.kruders.spring.aop.BusinessService;

public class Main {
	public static void main(String args[]) {
        ApplicationContext appContext = new ClassPathXmlApplicationContext("Spring-Business.xml");
        BusinessService businessService = (BusinessService)appContext.getBean("businessService");

5. Output

When you run the above example you’ll get an output like:

Method name : doSomeThing
Method arguments : []
Around method is called
Around before is running
Do Something Here
Around after is running

The folder structure of the example is shown below in Figure 20.1

Figure 20.1 Figure 20.1

You can download the source code of this example here.

2 Responses to Spring Auto Proxy Bean – BeanNameAutoProxyCreator