Sunday, April 29, 2012

Spring JDBC

We'll now add the Spring framework to our JDBC project. We'll add dependency injection to our Main and DAO class. We'll also learn how to configure DataSource as a Spring bean and supply connection parameters to it in the XML file.


************************************************************
org.springframework.stereotype.Component



Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.
Other class-level annotations may be considered as identifying a component as well, typically a special kind of component: e.g. the @Repository annotation or AspectJ's @Aspect annotation.
org.springframework.beans.factory.annotation.Autowired


Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities.
Only one constructor (at max) of any given bean class may carry this annotation, indicating the constructor to autowire when used as a Spring bean. Such a constructor does not have to be public.
Fields are injected right after construction of a bean, before any config methods are invoked. Such a config field does not have to be public.
Config methods may have an arbitrary name and any number of arguments; each of those arguments will be autowired with a matching bean in the Spring container. Bean property setter methods are effectively just a special case of such a general config method. Such config methods do not have to be public.
In the case of multiple argument methods, the 'required' parameter is applicable for all arguments.
In case of a Collection or Map dependency type, the container will autowire all beans matching the declared value type. In case of a Map, the keys must be declared as type String and will be resolved to the corresponding bean names.
Note that actual injection is performed through a BeanPostProcessor which in turn means that you cannot use @Autowired to inject references into BeanPostProcessor or BeanFactoryPostProcessor types. Please consult the javadoc for the AutowiredAnnotationBeanPostProcessor class (which, by default, checks for the presence of this annotation). 
***************************************************************


package com.venkat;


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


import com.venkat.JdbcDao.JdbcDaoImpl;
import com.venkat.model.Login;


public class JDBCDemo {


/**
* @param args
* @throws Exception 
*/
public static void main(String[] args) throws Exception  {
// TODO Auto-generated method stub

ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
 JdbcDaoImpl dao=context.getBean("jdbcDaoImpl",JdbcDaoImpl.class);
Login login=dao.getLogin("venkat");

System.out.println(login.getPassword());
}


}
**************************************************************************
package com.venkat.JdbcDao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.sql.DataSource;

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

import com.venkat.model.Login;
@Component
public class JdbcDaoImpl {
@Autowired
private DataSource datasource;

public DataSource getDatasource() {
return datasource;
}


public void setDatasource(DataSource datasource) {
this.datasource = datasource;
}


public Login getLogin(String userid) throws Exception
{
Connection con=datasource.getConnection();
PreparedStatement ps=con.prepareStatement("Select * from LOGIN where userid=?");
ps.setString(1, userid);
Login l=null;
ResultSet rs=ps.executeQuery();
if(rs.next())
{
l=new Login(userid, rs.getString("password"));
}
rs.close();
ps.close();
return l;


}
}
********************************************************************
package com.venkat.model;

public class Login {
private String userid;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String password;
public Login(String userid, String password) {
super();
this.userid = userid;
this.password = password;
}
 
}
*****************************************************************************
<?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-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:annotation-config/>

<context:component-scan base-package="com.venkat"></context:component-scan>



<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:xe">
</property>
<property name="username" value="venkat"></property>
<property name="password" value="venkat"></property>
</bean></beans>

Saturday, April 28, 2012

Spring AOP XML configuration

We'll learn how to configure aspects, advice and pointcuts using the traditional XML way.



package com.venkat.Spring.Aop;


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


import com.venkat.Spring.Aop.service.ShapeService;


public class AopTest {


public static void main(String args[])
{
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
ShapeService service=context.getBean("shapeService",ShapeService.class);
service.getCircle();
}
}
****************************************************************************
package com.venkat.Spring.Aop.Aspect;

public @interface Loggable {

}
********************************************************************************
package com.venkat.Spring.Aop.Aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;


@Aspect
public class LoggingAspect {

// @Pointcut("execution(*  get*())")
// public void allGetters(){}
// @Around("@annotation(com.venkat.Spring.Aop.Aspect.Loggable)")
public Object myAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) 
{
Object returnValue=null;
try {
System.out.println("Before method Executed");
returnValue=proceedingJoinPoint.proceed();
System.out.println("After Method Executed....");
} catch (Throwable e) {
System.out.println("Around Advice throws exception");
}
System.out.println("After Finally executed........");
return returnValue;
}
}
*************************************************************************
package com.venkat.Spring.Aop.model;

public class Circle {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
System.out.println("Setter method is called.....");
throw(new RuntimeException());
}
public String setNameAndReturning(String name) {
this.name = name;
System.out.println("Setter Method and return the value method executed....");
return name;
}
}
***********************************************************************
package com.venkat.Spring.Aop.model;

public class Triangle {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
****************************************************************************
package com.venkat.Spring.Aop.service;

import com.venkat.Spring.Aop.Aspect.Loggable;
import com.venkat.Spring.Aop.model.Circle;
import com.venkat.Spring.Aop.model.Triangle;

public class ShapeService {

private Triangle triangle;
private Circle circle;
public Triangle getTriangle() {
return triangle;
}
public void setTriangle(Triangle triangle) {
this.triangle = triangle;
}
@Loggable
public Circle getCircle() {
return circle;
}
public void setCircle(Circle circle) {
this.circle = circle;
}
}
*********************************************************************************
<?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-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<aop:aspectj-autoproxy/>
<bean name="triangle" class="com.venkat.Spring.Aop.model.Triangle">
<property name="name" value="triangle name"/>
</bean>
<bean name="circle" class="com.venkat.Spring.Aop.model.Circle">
<!-- <property name="name" value="circle name"/>  -->
</bean>
<bean name="shapeService" class="com.venkat.Spring.Aop.service.ShapeService">
<property name="triangle" ref="triangle"/>
<property name="circle" ref="circle"/>
</bean>
<bean  name="loggingAdviceBean" class="com.venkat.Spring.Aop.Aspect.LoggingAspect"/>
<aop:config>
<aop:aspect id="loggingAdvice" ref="loggingAdviceBean">
<aop:pointcut  id="allGetters" expression="execution(*  get*())"/>
<aop:around  pointcut-ref="allGetters" method="myAroundAdvice"/>
</aop:aspect>
</aop:config>
</beans>

Friday, April 27, 2012

Naming Conventions and Custom Advice Annotations

 I share some thoughts on naming conventions, and how it helps in the case of writing aspects in Spring. We will also understand how to write our own custom annotations to apply advice to methods.

package com.venkat.Spring.Aop;

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

import com.venkat.Spring.Aop.service.ShapeService;

public class AopTest {

    public static void main(String args[])
    {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        ShapeService service=context.getBean("shapeService",ShapeService.class);
        service.getCircle();
    }
}
***************************************************************************
package com.venkat.Spring.Aop.Aspect;

public @interface Loggable {

}
**************************************************************************
package com.venkat.Spring.Aop.Aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import com.venkat.Spring.Aop.model.Circle;

@Aspect
public class LoggingAspect {
   
    public void loggingAdvice(JoinPoint joinpoint)
    {
        System.out.println("Run Advice..... get method Executed");
        System.out.println(joinpoint.toString());
        Circle c=(Circle)joinpoint.getTarget();
        System.out.println(c.getName());
    }
   
    @AfterReturning(pointcut="args(name)",returning="returnString")
    public void allStringMethodArguments(String name, String returnString){
    System.out.println("A setter method has been executed............"+name);
    }
   
    @AfterThrowing(pointcut="args(name)",throwing="ex")
    public void exceptionAdvice(String name, Exception ex){
    System.out.println("Exception is thrown ............"+ex);
    }
   
    @After("args(String)")
    public void afterAdvice(){
    System.out.println("After Advice is executed...........");
    }

    @Pointcut("execution(*  get*())")
    public void allGetters(){}
   
   
    @Around("@annotation(com.venkat.Spring.Aop.Aspect.Loggable)")
    public Object myAroundAdvice(ProceedingJoinPoint proceedingJoinPoint)
    {
        Object returnValue=null;
            try {
                System.out.println("Before method Executed");
                returnValue=proceedingJoinPoint.proceed();
                System.out.println("After Method Executed....");
            } catch (Throwable e) {
                System.out.println("Around Advice throws exception");
        }
        System.out.println("After Finally executed........");
              
        return returnValue;
    }
   
   
}
*******************************************************************************
package com.venkat.Spring.Aop.model;

public class Circle {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        System.out.println("Setter method is called.....");
        throw(new RuntimeException());
    }
   
    public String setNameAndReturning(String name) {
        this.name = name;
        System.out.println("Setter Method and return the value method executed....");
        return name;
    }
}
********************************************************************************
package com.venkat.Spring.Aop.model;

public class Triangle {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
******************************************************************************
package com.venkat.Spring.Aop.service;

import com.venkat.Spring.Aop.Aspect.Loggable;
import com.venkat.Spring.Aop.model.Circle;
import com.venkat.Spring.Aop.model.Triangle;

public class ShapeService {

    private Triangle triangle;
    private Circle circle;
    public Triangle getTriangle() {
        return triangle;
    }
    public void setTriangle(Triangle triangle) {
        this.triangle = triangle;
    }
    @Loggable
    public Circle getCircle() {
        return circle;
    }
    public void setCircle(Circle circle) {
        this.circle = circle;
    }
}
******************************************************************************
<?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-2.0.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<aop:aspectj-autoproxy/>
    <bean name="triangle" class="com.venkat.Spring.Aop.model.Triangle">
    <property name="name" value="triangle name"/>
    </bean>
    <bean name="circle" class="com.venkat.Spring.Aop.model.Circle">
<!--     <property name="name" value="circle name"/>  -->
    </bean>
    <bean name="shapeService" class="com.venkat.Spring.Aop.service.ShapeService">
    <property name="triangle" ref="triangle"/>
    <property name="circle" ref="circle"/>
    </bean>
    <bean  name="loggingAdvice" class="com.venkat.Spring.Aop.Aspect.LoggingAspect"/>
</beans>

Around Advice Type

This example covers the Around advice type. We'll learn how to use it, and we'll also look at some of the unique and powerful features that are specific to this advice type.

package com.venkat.Spring.Aop;

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

import com.venkat.Spring.Aop.service.ShapeService;

public class AopTest {

    public static void main(String args[])
    {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        ShapeService service=context.getBean("shapeService",ShapeService.class);
        service.getCircle();
    }
}
**************************************************************************
package com.venkat.Spring.Aop.Aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import com.venkat.Spring.Aop.model.Circle;

@Aspect
public class LoggingAspect {
   
    public void loggingAdvice(JoinPoint joinpoint)
    {
        System.out.println("Run Advice..... get method Executed");
        System.out.println(joinpoint.toString());
        Circle c=(Circle)joinpoint.getTarget();
        System.out.println(c.getName());
    }
   
    @AfterReturning(pointcut="args(name)",returning="returnString")
    public void allStringMethodArguments(String name, String returnString){
    System.out.println("A setter method has been executed............"+name);
    }
   
    @AfterThrowing(pointcut="args(name)",throwing="ex")
    public void exceptionAdvice(String name, Exception ex){
    System.out.println("Exception is thrown ............"+ex);
    }
   
    @After("args(String)")
    public void afterAdvice(){
    System.out.println("After Advice is executed...........");
    }

    @Pointcut("execution(*  get*())")
    public void allGetters(){}
   
   
    @Around("allGetters()")
    public Object myAroundAdvice(ProceedingJoinPoint proceedingJoinPoint)
    {
        Object returnValue=null;
            try {
                System.out.println("Before method Executed");
                returnValue=proceedingJoinPoint.proceed();
                System.out.println("After Method Executed....");
            } catch (Throwable e) {
                System.out.println("Around Advice throws exception");
        }
        System.out.println("After Finally executed........");
               
        return returnValue;
    }
   
   
}
***************************************************************************
package com.venkat.Spring.Aop.model;

public class Circle {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        System.out.println("Setter method is called.....");
        throw(new RuntimeException());
    }
   
    public String setNameAndReturning(String name) {
        this.name = name;
        System.out.println("Setter Method and return the value method executed....");
        return name;
    }
}
****************************************************************************
package com.venkat.Spring.Aop.model;

public class Triangle {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
*****************************************************************************
<?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-2.0.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<aop:aspectj-autoproxy/>
    <bean name="triangle" class="com.venkat.Spring.Aop.model.Triangle">
    <property name="name" value="triangle name"/>
    </bean>
    <bean name="circle" class="com.venkat.Spring.Aop.model.Circle">
<!--     <property name="name" value="circle name"/>  -->
    </bean>
    <bean name="shapeService" class="com.venkat.Spring.Aop.service.ShapeService">
    <property name="triangle" ref="triangle"/>
    <property name="circle" ref="circle"/>
    </bean>
    <bean  name="loggingAdvice" class="com.venkat.Spring.Aop.Aspect.LoggingAspect"/>
</beans>

After Advice Types

We'll learn about the After Advice types: After (finally), AfterReturning and AfterThrowing.

package com.venkat.Spring.Aop;

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

import com.venkat.Spring.Aop.service.ShapeService;

public class AopTest {

    public static void main(String args[])
    {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        ShapeService service=context.getBean("shapeService",ShapeService.class);
        service.getCircle().setNameAndReturning("Dummy Circle return");
    }
}
*************************************************************************
package com.venkat.Spring.Aop.Aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;

import com.venkat.Spring.Aop.model.Circle;

@Aspect
public class LoggingAspect {
   
    public void loggingAdvice(JoinPoint joinpoint)
    {
        System.out.println("Run Advice..... get method Executed");
        System.out.println(joinpoint.toString());
        Circle c=(Circle)joinpoint.getTarget();
        System.out.println(c.getName());
    }
   
    @AfterReturning(pointcut="args(name)",returning="returnString")
    public void allStringMethodArguments(String name, String returnString){
    System.out.println("A setter method has been executed............"+name);
    }
   
    @AfterThrowing(pointcut="args(name)",throwing="ex")
    public void exceptionAdvice(String name, Exception ex){
    System.out.println("Exception is thrown ............"+ex);
    }
   
    @After("args(String)")
    public void afterAdvice(){
    System.out.println("After Advice is executed...........");
    }

}
*******************************************************************
package com.venkat.Spring.Aop.model;

public class Circle {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        System.out.println("Setter method is called.....");
        throw(new RuntimeException());
    }
   
    public String setNameAndReturning(String name) {
        this.name = name;
        System.out.println("Setter Method and return the value method executed....");
        return name;
    }
}
***************************************************************************
package com.venkat.Spring.Aop.model;

public class Triangle {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
******************************************************************************
package com.venkat.Spring.Aop.service;

import com.venkat.Spring.Aop.model.Circle;
import com.venkat.Spring.Aop.model.Triangle;

public class ShapeService {

    private Triangle triangle;
    private Circle circle;
    public Triangle getTriangle() {
        return triangle;
    }
    public void setTriangle(Triangle triangle) {
        this.triangle = triangle;
    }
    public Circle getCircle() {
        return circle;
    }
    public void setCircle(Circle circle) {
        this.circle = circle;
    }
}
*******************************************************************************
<?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-2.0.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<aop:aspectj-autoproxy/>
    <bean name="triangle" class="com.venkat.Spring.Aop.model.Triangle">
    <property name="name" value="triangle name"/>
    </bean>
    <bean name="circle" class="com.venkat.Spring.Aop.model.Circle">
<!--     <property name="name" value="circle name"/>  -->
    </bean>
    <bean name="shapeService" class="com.venkat.Spring.Aop.service.ShapeService">
    <property name="triangle" ref="triangle"/>
    <property name="circle" ref="circle"/>
    </bean>
    <bean  name="loggingAdvice" class="com.venkat.Spring.Aop.Aspect.LoggingAspect"/>
</beans>

Thursday, April 26, 2012

Spring JoinPoints and Advice Arguments

We'll now learn about join points, and how we can use arguments in the advice methods to get information about join points.

package com.venkat.Spring.Aop;

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

import com.venkat.Spring.Aop.service.ShapeService;

public class AopTest {

    public static void main(String args[])
    {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        ShapeService service=context.getBean("shapeService",ShapeService.class);
        service.getCircle().setName("Dummy Circle");
        System.out.println(service.getCircle().getName());
    }
}
************************************************************************
package com.venkat.Spring.Aop.Aspect;

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

import com.venkat.Spring.Aop.model.Circle;

@Aspect
public class LoggingAspect {
   
    @Before("allCircleMethods()")
    public void loggingAdvice(JoinPoint joinpoint)
    {
        System.out.println("Run Advice..... get method Executed");
        System.out.println(joinpoint.toString());
        Circle c=(Circle)joinpoint.getTarget();
        System.out.println(c.getName());
    }
   
//    @Before("args(name)")
//    public void allStringMethodArguments(String name){
//       
//        System.out.println("A setter method has been executed............"+name);
//    }
   
    @Before("args(String)")
    public void allStringMethodArguments(){
   
    System.out.println("A setter method has been executed............");
    }
   
   
   
    @Pointcut("within(com.venkat.Spring.Aop.model.*)")
    public void allCircleMethods(){}
   
   

}
***************************************************************************
package com.venkat.Spring.Aop.model;

public class Circle {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
***************************************************************************
package com.venkat.Spring.Aop.model;

public class Triangle {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
*************************************************************************
package com.venkat.Spring.Aop.service;

import com.venkat.Spring.Aop.model.Circle;
import com.venkat.Spring.Aop.model.Triangle;

public class ShapeService {

    private Triangle triangle;
    private Circle circle;
    public Triangle getTriangle() {
        return triangle;
    }
    public void setTriangle(Triangle triangle) {
        this.triangle = triangle;
    }
    public Circle getCircle() {
        return circle;
    }
    public void setCircle(Circle circle) {
        this.circle = circle;
    }
}
**********************************************************************
<?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-2.0.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<aop:aspectj-autoproxy/>
    <bean name="triangle" class="com.venkat.Spring.Aop.model.Triangle">
    <property name="name" value="triangle name"/>
    </bean>
    <bean name="circle" class="com.venkat.Spring.Aop.model.Circle">
    <property name="name" value="circle name"/>
    </bean>
    <bean name="shapeService" class="com.venkat.Spring.Aop.service.ShapeService">
    <property name="triangle" ref="triangle"/>
    <property name="circle" ref="circle"/>
    </bean>
    <bean  name="loggingAdvice" class="com.venkat.Spring.Aop.Aspect.LoggingAspect"/>
</beans>

Spring Pointcut WithinAnnotation

we'll learn about a few other Pointcut expressions that can be used to advice different methods
package com.venkat.Spring.Aop;

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

import com.venkat.Spring.Aop.service.ShapeService;

public class AopTest {

    public static void main(String args[])
    {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        ShapeService service=context.getBean("shapeService",ShapeService.class);
        System.out.println(service.getCircle().getName());
    }
}
************************************************************************
package com.venkat.Spring.Aop.Aspect;

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

@Aspect
public class LoggingAspect {
   
    @Before("allGetters() && allCircleMethods()")
    public void loggingAdvice()
    {
        System.out.println("Run Advice..... get method Executed");
    }
   
    @Before("allGetters()")
    public void secondAdvice()
    {
        System.out.println("Second Advice Executed .............");
    }
   
    @Pointcut("execution(public * get*())")
    public void allGetters()
    {}

    @Pointcut("within(com.venkat.Spring.Aop.model.*)")
    public void allCircleMethods(){}
}
*********************************************************************************
package com.venkat.Spring.Aop.model;

public class Circle {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
*********************************************************************************
package com.venkat.Spring.Aop.model;

public class Triangle {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
**********************************************************************************
package com.venkat.Spring.Aop.service;

import com.venkat.Spring.Aop.model.Circle;
import com.venkat.Spring.Aop.model.Triangle;

public class ShapeService {

    private Triangle triangle;
    private Circle circle;
    public Triangle getTriangle() {
        return triangle;
    }
    public void setTriangle(Triangle triangle) {
        this.triangle = triangle;
    }
    public Circle getCircle() {
        return circle;
    }
    public void setCircle(Circle circle) {
        this.circle = circle;
    }
}
*******************************************************************************
<?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-2.0.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<aop:aspectj-autoproxy/>
    <bean name="triangle" class="com.venkat.Spring.Aop.model.Triangle">
    <property name="name" value="triangle name"/>
    </bean>
    <bean name="circle" class="com.venkat.Spring.Aop.model.Circle">
    <property name="name" value="circle name"/>
    </bean>
    <bean name="shapeService" class="com.venkat.Spring.Aop.service.ShapeService">
    <property name="triangle" ref="triangle"/>
    <property name="circle" ref="circle"/>
    </bean>
    <bean  name="loggingAdvice" class="com.venkat.Spring.Aop.Aspect.LoggingAspect"/>
</beans>