可以访问findAll规范方法的自定义存储库

时间:2017-06-26 16:47:22

标签: java spring

需要在存储库层中执行自定义动态搜索。无法通过查询方法或查询注释进行查询。 这个动态搜索需要调用由pleJpaRepository spring class实现的findAll(Specification,Paging)

    #modules
import time
#variables

testScores = []
#avg = 0.0

#functions
def calculate_average():
    x = 0
    print("Enter test scores")
    while (x <=4): #limits number of inputs to 5 test scores as required per instructions for lab
        test = input('Enter test score: ')
        #if test == "":
            #break
        x += 1
        testScores.append(float(test)) #adds input from test var into testScores list as a float
    time.sleep(1)
    print("","calculating average", ".........", "", sep = "\n")
    time.sleep(1)
    global avg #makes the var avg global so as to keep data stored so other functions can access the data
    avg = float(sum(testScores)/len(testScores))#takes all the data points stored in the list testScores and adds them up via sum ,and then divdes by (len)gth (aka number of data points)
    print("%.2f" % avg)#displays avg limiting to two decimal points rounding to the nearest 100th


def determine_grade(): #fucntion calls the var avg (which was made global in function 1) and converts it into a letter grade.
    if (avg >= 90.0 and avg <= 100.0):
        print("Grade = A")
    elif (avg >= 80.0 and avg <= 89.9):
        print("Grade = B")
    elif (avg >= 70.0 and avg <= 79.9):
        print("Grade = C")
    elif (avg >= 60.0 and avg <= 69.9):
        print("Grade = D")
    elif (avg <= 59.9):
        print("Grade = F")
def main():
    print("Beginning Program","", sep = "\n")
    time.sleep(1)
    calculate_average()
    determine_grade()
    time.sleep(1)
    print("","End of program...", "Have a good day...", "Program Terminated", sep = "\n")

main()

当我运行应用程序时,我得到

com.dwengo.cards.repository.CardsRepositoryImpl中构造函数的参数0

1 个答案:

答案 0 :(得分:3)

Spring检测到bean需要构造的CardsRepositoryImpl,但是在构造函数中没有定义@Autowired或@Inject。所以spring尝试使用default(no args)构造函数创建一个实例。它失败是因为需要两个参数而第一个(索引0)是Class类型。

将@Autowired添加到构造函数后,尝试解析依赖项,并且找不到类型为Class的bean。完全可以理解。此外,Class参数在这里是无用的。

更改您的定义:

public class CardsRepositoryImpl extends SimpleJpaRepository<Cards, Long> implements CardsRepository {
    private final EntityManager em;
    @Autowired
    public CardsRepositoryImpl(EntityManager em) {
        super(Cards.class, em);
    }

    @Override
    public Page<Cards> customSearch(CardSearch CardSearch, Pageable page) {
        Specification<Cards> specification = (Root<Cards> root, CriteriaQuery<?> cq, CriteriaBuilder cb) -> {
        ..
        }
        return this.findAll(specification, page);
    }
}