Java Login

About javalogin.com

Hello guys,
javalogin.com is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.


     << Previous
Next >>     


PointCuts and JointPoints

JoinPoints: These are basically places in the actual business logic where you wish to insert some miscellaneous functionality that is necessary but not being part of the actual business logic. Some examples of JoinPints are: method call, method returning normally, method throwing an exception, instantiating an object, referring an object, etc... We will discuss JoinPoints in detail later.

Pointcuts are something like regular expressions which are used to identify joinpoints. Pontcuts are expressed using "pointcut expression language". Pointcuts are points of execution flow where the cross-cutting concern needs to be applied. There is a difference between Joinpoint and Pointcut; Joinpoints are more general and represents any control flow where we 'may choose to' introduce a cross-cutting concern while pointcuts identifies such joinpoints where 'we want to' introduce a cross-cutting concern.

Example of Pointcut

The following example defines a pointcut named 'pointcutGetter' that will match the execution of every method which will match signature public String getName() available in the project

Rose.java

package org.vtechnology.model;

public class Rose {

private String name;

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

Lotus.java

package org.vtechnology.model;

public class Lotus {

private String name;

public String getName() {
return name;
}

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

Flower.java

package org.vtechnology.service;

import org.vtechnology.model.Lotus;
import org.vtechnology.model.Rose;

public class Flower {

private Rose rose;
private Lotus lotus;
public Rose getRose() {
return rose;
}
public void setRose(Rose rose) {
this.rose = rose;
}
public Lotus getLotus() {
return lotus;
}
public void setLotus(Lotus lotus) {
this.lotus = lotus;
}
}

LoggingAOP.java

package org.vtechnology.aop;

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

@Aspect
public class LoggingAOP {

@Before("pointcutGetter()")
public void loggingAdvice(){
System.out.println("Logging Advice Run before getName Method");
}

@Before("pointcutGetter()")
public void secondAdvice(){
System.out.println("Second advice Run before getName Method");
}

@Pointcut("execution(public String getName())")
public void pointcutGetter(){}

}

spring.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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<aop:aspectj-autoproxy/>

<bean id="rose" class="org.vtechnology.model.Rose">
<property name="name" value="Flower Name is Rose"/>
</bean>

<bean id="lotus" class="org.vtechnology.model.Lotus">
<property name="name" value="Flower Name is Lotus"/>
</bean>

<bean name="flower" class="org.vtechnology.service.Flower"
autowire="byName"/>

<bean class="org.vtechnology.aop.LoggingAOP"/>

</beans>

AopMain.java

package org.vtechnology.main;

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

public class AopMain {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext
("spring.xml");

Flower flower = context.getBean("flower", Flower.class);
System.out.println(flower.getRose().getName());
System.out.println(flower.getLotus().getName());
}
}


     << Previous
Next >>