Pointcut example

In this post we will see how to use pointcut. Below example explains the pointcut concept.
File: com.ram.model.Mobile.java
package com.ram.model;

public class Mobile {
 private String name;
 private int number;
 
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getNumber() {
  return number;
 }
 public void setNumber(int number) {
  this.number = number;
 }
 
}

File: com.ram.model.Landline.java
package com.ram.model;

public class Landline {
 private String name;
 private int number;
 
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getNumber() {
  return number;
 }
 public void setNumber(int number) {
  this.number = number;
 }
 
}

File: com.ram.service.Home.java
package com.ram.service;

import com.ram.model.Landline;
import com.ram.model.Mobile;

public class Home {
 private Mobile mobile;
 private Landline landline;
 
 public Mobile getMobile() {
  return mobile;
 }
 public void setMobile(Mobile mobile) {
  this.mobile = mobile;
 }
 public Landline getLandline() {
  return landline;
 }
 public void setLandline(Landline landline) {
  this.landline = landline;
 }
 
}

File: com.ram.aspect.PointcutAspect.java
package com.ram.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class PointcutAspect {
 
 @Before("allGetters()")
 public void AdviceOne(){
  System.out.println("First advice from the method AdviceOne ...");
 }
 
 @Before("allGetters()")
 public void AdviceTwo(){
  System.out.println("Second advice from the method AdviceTwo ...");
 }
 
 @Pointcut("execution(* get*())")
 public void allGetters() {}
}

File: WEB-INF/dispatcher-servlet.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
 
    <!-- Tells the spring that we are using aspects in our application -->   
    <aop:aspectj-autoproxy/>
       
  <bean name="mobile" class="com.ram.model.Mobile">
     <property name="name" value="Sony"></property>
     <property name="number" value="987654321"></property>
    </bean>
       
    <bean name="landline" class="com.ram.model.Landline">
     <property name="name" value="Optus"></property>
     <property name="number" value="0198765432"></property>
    </bean>
       
    <bean name="home" class="com.ram.service.Home" autowire="byName"/>
       
    <bean name="pointcutAspect" class="com.ram.aspect.PointcutAspect"/>
    
</beans>

File: com.ram.main.PointcutExample.java
package com.ram.main;

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

import com.ram.service.Home;

public class PointcutExample {
 public static void main(String[] args){
  ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
  Home home = context.getBean("home", Home.class);
  System.out.println(home.getLandline().getName());
  System.out.println(home.getLandline().getNumber());
 }
}

Run the PointcutExample.java program and you get the below output:

No comments:

Post a Comment