Aspect Filter : Spring Aspect « Spring « Java Tutorial






File: context.xml

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

    <context:component-scan base-package="bean">
        <context:include-filter type="annotation" expression="bean.Magic"/>
        <context:include-filter type="assignable" expression="bean.ComponentMarker"/>
        <context:include-filter type="aspectj" expression="* void bean.*Service*(..)"/>
    </context:component-scan>

</beans>

File: Main.java

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

public class Main {
  public static void main(String[] args) throws Exception {
    ApplicationContext ac = new ClassPathXmlApplicationContext(
        "context.xml");
    String[] beanNames = ac.getBeanDefinitionNames();
    for (String beanName : beanNames) {
      System.out.println(beanName + ": " + ac.getBean(beanName));
    }
  }
}

File: ComponentMarker.java

package bean;


public interface ComponentMarker {
}

File: Magic.java

package bean;


public @interface Magic {
}

File: MarkedBean.java

package bean;


public class MarkedBean implements ComponentMarker {

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("MarkedBean");
        sb.append("{}");
        return sb.toString();
    }
}

File: SimpleBean.java

package bean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

/**
 * @author janm
 */
@Magic
class SimpleBean {
    private Dependency dependency;

    @Autowired(required = true)
    public void setDependency(Dependency dependency) {
        this.dependency = dependency;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("SimpleBean");
        sb.append(ObjectUtils.identityToString(this));
        sb.append("{dependency=").append(dependency);
        sb.append('}');
        return sb.toString();
    }
}
@Component
class Dependency {

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("Dependency");
        sb.append(ObjectUtils.identityToString(this));
        sb.append("{}");
        return sb.toString();
    }
}
  Download:  Spring-AspectFilter.zip( 4,453 k)








28.50.Spring Aspect
28.50.1.Spring Tracing Aspect
28.50.2.Spring Aspect Logging
28.50.3.AspectJ Expression Pointcut
28.50.4.AspectJ AutoProxy
28.50.5.Aspect Filter
28.50.6.Aspect Annotation Pointcut Around After
28.50.7.Aspect Annotation
28.50.8.AOP Annotation
28.50.9.Annotation Scope
28.50.10.Spring AOP Examples