Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Spring中尝试AOP以从数据源获取连接_Java_Spring_Spring Aop - Fatal编程技术网

Java 在Spring中尝试AOP以从数据源获取连接

Java 在Spring中尝试AOP以从数据源获取连接,java,spring,spring-aop,Java,Spring,Spring Aop,这里我正在使用Spring尝试AOP,这里AOP用于从数据源获取连接并关闭连接,代码如下: package com.database.pojo; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component("student01Inj") @Scope("prototype") public class StudentP

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

 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;
    }

 }
 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;
    }
 }
这是学生的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;
    }

 }
 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;
    }
 }
这是一个接口及其实现,我希望 放置切入点方法

接口StudentPOJOService.java如下所示

 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;
    }

 }
 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;
    }
 }
这是spring-data.xml,其中包含了所有spring定义

 <?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>                           
在这里,我试图从AOP中的datasource获取连接,然后使用“joinpoint”的“继续”方法将相同的连接传递给getStudents方法, 但这就产生了如上所示的例外情况

谁能告诉我哪里出了问题


等待回复,

首先:Student是一个域对象,这些对象永远不应该由Spring连接!好的,关于这个设计,我将做一些修改,这只是一个实践例子,但我的问题是我的行“jointPoint.Procedue(new Connection[]{this.con}”);”在DriverManager DataSource.java类中,是否符合逻辑,我想从切入点传递连接,给我一些提示