2 回答
TA貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個贊
首先,@Transient來自 JPA ,與 Spring 無關(guān)。
其次,為了能夠讓Spring注入bean Employee,Employee還需要注冊為spring bean。但實(shí)際上,您可以認(rèn)為 Employee 是由 JPA 實(shí)現(xiàn)在幕后使用“new”創(chuàng)建的。這就是為什么 spring 不能自動將其他 bean 連接到它。
如果您確實(shí)需要這樣做,您可以AspectJ按照文檔中的描述進(jìn)行操作。
我個人沒有嘗試這種方法,因?yàn)槟憧梢院唵蔚刈屇鉙alaryService接受Employee作為其參數(shù)之一來計算他的工資,這比該方法更簡單且易于理解AspectJ。
public interface SalaryService {
public double computeSalary(Employee employee , List<Benefit> benefits, List<Deduction> deductions);
}
客戶端代碼如下所示:
@Service
public class EmployeeService {
@Autowired
private SalaryService salaryService;
@Transactional
public void computeEmployeeSalary(Integer employeeId){
Employee employee = entityManager.find(Employee.class , employeeId);
salaryService.computeSalary(employee, .... ,.....);
}
}
TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個贊
實(shí)體對象是由 JPA 實(shí)現(xiàn)(如 Hibernate)創(chuàng)建的,而不是由 spring 管理的。
它們既不是單例也不是原型,所以一般來說,您不能在實(shí)體 bean 的屬性上使用自動裝配(因?yàn)樽詣友b配只能在 spring bean 上完成)。
添加回答
舉報
