gpt4 book ai didi

java - 如何在spring roo项目中调用.aj文件中的方法?

转载 作者:行者123 更新时间:2023-12-04 05:53:35 28 4
gpt4 key购买 nike

如何在spring roo项目中调用.aj文件中的方法?

例如如何从 Employee.java 中的 callMethod() 方法调用 Employee_Roo_Jpa_ActiveRecord.aj 中的方法“Employee.findAllEmployees()”?

文件:Employee.java

  1 package com.tap.domain;
2
3 import java.text.SimpleDateFormat;
4 import java.util.ArrayList;
5 import java.util.Calendar;
6
7 import javax.validation.constraints.Max;
8 import javax.validation.constraints.Min;
9
10 import org.aspectj.lang.annotation.Aspect;
11 import org.springframework.beans.factory.annotation.Value;
12 import org.springframework.roo.addon.javabean.RooJavaBean;
13 import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
14 import org.springframework.roo.addon.tostring.RooToString;
15 import org.springframework.stereotype.Component;
16
17 @RooJavaBean
18 @RooToString
19 @RooJpaActiveRecord
20 @Component("employee")
21 public class Employee {
22
23 private long id;
24
25 private String name;
26 public void callMethod() {
...
...
30 }

文件:Employee_Roo_Jpa_ActiveRecord.aj
  6 import java.util.List;
7 import javax.persistence.EntityManager;
8 import javax.persistence.PersistenceContext;
9 import org.springframework.transaction.annotation.Transactional;
10 import com.tap.domain.Employee;
11
12 privileged aspect Employee_Roo_Jpa_ActiveRecord {
13
14 @PersistenceContext
15 transient EntityManager Employee.entityManager;
...
27 public static List<Employee> Employee.findAllEmployees() {
28 return entityManager().createQuery("SELECT o FROM Employee o", Emplo yee.class).getResultList();
29 }
...

最佳答案

Roo 使用的机制是 AspectJ 的 inter-type declaration .

基本上:您的员工类将具有 findAllEmployees()方法在运行时,所以你可以调用它:

public void callMethod() {
List<Employee> allEmployees = findAllEmployees();
// now do something with the employees
}

关于java - 如何在spring roo项目中调用.aj文件中的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9782227/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com