0

这里我正在尝试使用 Spring 的 AOP,这里 AOP 用于从数据源获取连接并关闭连接,代码如下,

这是学生的pojo如下,

 package com.database.pojo;
 import org.springframework.context.annotation.Scope;
 import org.springframework.stereotype.Component;

 @Component("student01Inj")
 @Scope("prototype")
 public class StudentPOJO {

    private Integer studentId;
    private String studentName;

    public StudentPOJO(){
        System.out.println(this.getClass().getName()+" initialised ....");
    }   

    public StudentPOJO(Integer studentId,String studentName){
        this.studentId = studentId;
        this.studentName = studentName;
    }

    public Integer getStudentId() {
        return studentId;
    }
    public void setStudentId(Integer studentId) {
        this.studentId = studentId;
    }
    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

 }

这是一个接口及其实现,我想在其中放置切入点方法,

接口StudentPOJOService.java如下,

 package com.database.pojo;
 import java.sql.Connection;
 import java.util.List;

 public interface StudentPOJOService {
    List<StudentPOJO> getStudents(Connection[] connection);
 }

上述接口的实现如下,

 package com.database.pojo;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.util.ArrayList;
 import java.util.List;
 import org.springframework.context.annotation.Scope;
 import org.springframework.stereotype.Component;


 @Component("student_pojo_ser_inj")
 @Scope("singleton")
 public class StudentPOJOServiceImpl implements StudentPOJOService{ 

    @Override
    public List<StudentPOJO> getStudents(Connection[] objects){
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<StudentPOJO>  studentList = null;
        try{
            studentList = new ArrayList<StudentPOJO>();
            con = (objects != null && objects.length >  0) ? objects[0] : null;
            if(con == null){
                System.out.println(" CON NULL ");
            }
            pstmt = con.prepareStatement(" select id,studentName from tblstudent ");
            rs = pstmt.getResultSet();
            while(rs.next()){
                StudentPOJO pojo = new StudentPOJO();
                pojo.setStudentId(rs.getInt("id"));
                pojo.setStudentName(rs.getString("studentName"));
                studentList.add(pojo);
            }
        }catch(Exception e){
            System.out.println(" XXXXX "+e);
        }
        return studentList;
    }
 }

这是我想在切入点调用方法的 AOP 相关类,

 package com.database.aop;
 import java.sql.Connection;
 import java.sql.SQLException;
 import org.aspectj.lang.ProceedingJoinPoint;
 import org.aspectj.lang.annotation.Around;
 import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Pointcut;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.context.annotation.Scope;
 import org.springframework.jdbc.datasource.DriverManagerDataSource;
 import org.springframework.stereotype.Component;

 @Aspect
 @Component("databaseWrapperINJ")
 @Scope("prototype")
 public class DatabaseWrapperEx01 {

    public DatabaseWrapperEx01 (){
        System.out.println(this.getClass().getName()+" intialised ..... ");
    }

    @Autowired
    @Qualifier("demoDatasourceInj")
    public DriverManagerDataSource dataSourceManager;

    private Connection con = null; 

    private void setConnection() throws SQLException{
        this.con = this.dataSourceManager.getConnection();
    }

    private void closeConn() throws Exception{
        if(this.con != null && !this.con.isClosed()){
            this.con.close();
        }
    }

    @Pointcut("execution(* com.database.pojo.StudentPOJOService.getStudents(..))")
    public void fetchStudent() throws Throwable{ }

    @Around("fetchStudent()")
    public void methodCallForStudentFetch(ProceedingJoinPoint jointPoint) throws Throwable{
        try{
            this.setConnection();
            jointPoint.proceed(new Connection[] { this.con });
            this.closeConn();
        }catch(Throwable e){
            System.out.println(e);
            this.closeConn();
        }
    }
 }

这是所有 spring 定义所在的 spring-data.xml,

 <?xml version="1.0" encoding="UTF-8"?>
 <beans     xmlns="http://www.springframework.org/schema/beans"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"       
        xmlns:util="http://www.springframework.org/schema/util"             
        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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

        <context:annotation-config />
        <context:component-scan  base-package="com.database" />

        <bean   id="demoDatasourceInj"
                class="org.springframework.jdbc.datasource.DriverManagerDataSource"
                p:driverClassName="org.apache.derby.jdbc.ClientDriver"
                p:url="jdbc:derby://localhost:1527/sun-appserv-samples"
                p:username="APP"
                p:password="APP" />

        <bean   id="proxyCreator_inj" class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
 </beans>                           

包含我将从命令提示符调用的 main 方法的类,

 package com.database.main;
 import java.sql.Connection;
 import java.util.List;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 import com.database.pojo.StudentPOJO;
 import com.database.pojo.StudentPOJOService;

 public class Ex02 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ApplicationContext ctx = null;
        StudentPOJOService service = null;
        List<StudentPOJO> studentList = null;
        Connection[] cons = null;
        try{
            ctx = new ClassPathXmlApplicationContext("spring-data.xml");
            service = (StudentPOJOService) ctx.getBean("student_pojo_ser_inj");
            studentList = service.getStudents(cons);
            System.out.println("     _ "+studentList.size());
        }catch(Exception e){
            System.out.println(e);
            e.printStackTrace();
        }
    }

 }

现在,当我尝试运行上面的类时,我得到了一个异常,

[java]  "methodCallForStudentFetch" called
[java]  org.springframework.aop.AopInvocationException: AOP configuration seems to be invalid: tried calling method 
[public abstract java.util.List com.database.pojo.StudentPOJOService.getStudents(java.sql.Connection[])]
on target [com.database.pojo.StudentPOJOServiceImpl@b96751]; 
nested exception is java.lang.IllegalArgumentException: argument type mismatch

在这里,我要做的是从 AOP 中的数据源获取连接,然后使用“joinpoint”的“proceed”方法将相同的连接传递给 getStudents 方法,但这会产生如上所示的异常,

任何人都可以告诉我哪里出错了,

等待回复,

4

0 回答 0