Reputation: 1
How can I call advised method from a before advice? I am using Spring 3.0.5 and XML configuration.
I have written following code:
public interface Arithmatic {
public int add(int a,int b);//Advised method
public int sub(int a,int b);//Advised method
}
public class ArithmaticImpl implements Arithmatic {
public int add(int a,int b){
return (a+b);
}
public int sub(int a,int b){
return (a-b);
}
}
and my AOP class is
public class ValidateNumber{
public validateNumber(int a,int b){
if (a<0||b<0) {
// how can show validation message?
} else {
//how can i call Advised methods?
}
}
}
Congiguration file is
<aop:config>
<aop:aspect ref="validatenumber">
<aop:before pointcut="execution(* com.arithematic.Arithmatic.add(int,int)) and args(a,b)" method="add" arg-names="a,b"/>
</aop:aspect>
</aop:config>
Upvotes: 0
Views: 771
Reputation: 16050
Why not try with the simpler approach first:
<aop:config>
<aop:pointcut id="mypointcut" expression="execution(* com.arithematic.*.*(..))" />
<aop:advisor advice-ref="validatenumber" pointcut-ref="mypointcut" />
</aop:config>
Make that work, and thengo on to the more advanced pointcuts and match AspectJ expressions.
(Also: You spell "arithmetic" as "Arithmatic" in your class and "arithematic" in the package name - make sure these actually match between XML and code to rule out a simple typo error causing your aspect to not work :-)
Cheers,
Upvotes: 0
Reputation: 315
Below is the sample example i had tried to test spring AOP.
SpringConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
">
<bean id="beanPostProcessor" class="springTest.SpringBeanPostProcessor" />
<bean id="contestPostProcessor" class="springTest.SpringPostProcessor" />
<bean id="emp" class="springTest.Employee">
<property name="id" value="1"/>
<property name="name" value="sunil"/>
</bean>
<bean id="loginCheckAspect" class="springTest.Aspect" init-method="initAspect"/>
<bean id="msg" class="springTest.LoadMessages" >
<property name="id" value="1" />
</bean>
<aop:config>
<aop:pointcut id="login" expression="execution(* *.showMsg*(..))"/>
<aop:aspect id="loginAspect" ref="loginCheckAspect" >
<aop:before method="checkLogin" pointcut-ref="login" />
</aop:aspect>
</aop:config>
</beans>
LoadMessages.class
package springTest;
public class LoadMessages {
int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public LoadMessages() {
}
public void showMsg(){
System.out.println("#################################################");
System.out.println("Welcome neel");
System.out.println();
}
public void getInfo(){
System.out.println("#################################################");
System.out.println("Welcome neel");
System.out.println();
}
}
Aspect.java
package springTest;
import java.io.DataInputStream;
import java.io.IOException;
public class Aspect {
public void checkLogin() throws IOException{
DataInputStream ds=new DataInputStream(System.in);
//BufferedInputStream bf=new BufferedInputStream(System.in);
System.out.println("\n\n** Credential Check Point using AOP login aspect ** ");
System.out.print("Please login to view employee details");
System.out.print("\nUser Name : ");
String userName=ds.readLine();
String pass;
if(userName!=null && userName.equals("neel")){
System.out.print("Password : ");
pass=ds.readLine();
if(pass!=null && pass.equals("77")){
System.out.println();
}else{
throw new IOException("Invalid Password");
}
}else{
throw new IOException("Invalid User Name");
}
}
public void initAspect(){
//System.out.println("@@@@@@ Init method of aspect @@@@@");
}
}
SpringBeanPostProcessor.java
package springTest;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class SpringBeanPostProcessor implements BeanPostProcessor {
public Object postProcessAfterInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("Initialization Completes "+arg1);
return arg0;
}
public Object postProcessBeforeInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("Initialization....");
return arg0;
}
}
SpringPostProcessor.java
package springTest;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
public class SpringPostProcessor implements BeanFactoryPostProcessor {
public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0)
throws BeansException {
System.out.println("Initialization Context Succssefull.");
}
}
Hope this will help you.
Upvotes: 1