第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何自動裝配瞬態(tài)屬性?

如何自動裝配瞬態(tài)屬性?

絕地?zé)o雙 2023-07-13 13:52:41
我有一個實(shí)體,例如 Employee,其中包含 @Transient 對象薪資,該薪資將從相關(guān)表/實(shí)體 DailyTimeRecord (DTR) 派生。DTR 對象數(shù)據(jù)檢索使用連接,并且也在 Employee 對象中自動裝配。DTR 對象列表將作為計算薪資對象值的基礎(chǔ)。new應(yīng)避免使用using關(guān)鍵字,并讓IoC容器創(chuàng)建對象。另外,我希望避免使用new關(guān)鍵字,以盡量減少代碼的耦合性,并盡可能確保未來的兼容性和支持可擴(kuò)展性。因此,我有接口 Salary 并由 SalaryImpl 類實(shí)現(xiàn)。但是每次我嘗試在瞬態(tài)屬性 Salary 上運(yùn)行自動裝配的代碼時,它總是為空。我在這里找到了根本原因new當(dāng)它是瞬態(tài)屬性時,我將如何創(chuàng)建一個避免使用關(guān)鍵字的對象?實(shí)體類? ?@Entity? ?Class Employee implements Serializable {? ? ? ? ? //Attributes from DB here? ? ? ? ? @OneToMany? ? ? ? ? @JoinColumn(name="empNumber", referencedColumnName = "empNumber")? ? ? ? ? private List<DTR> dtr;? ? ? ? ? @Autowired? ? ? ? ? @Transient? ? ? ? ? private Salary salary;? ? ? ? ? //getters ang setters here? ? ? ? ? public double computeSalary(){? ? ? ? ? }? ?}薪資接口? ?public interface Salary {? ? ? ? ? public double computeSalary(List<Benefit> benefits, List<Deduction> deductions);? ?}薪資接口基礎(chǔ)/實(shí)現(xiàn)類? ?@Service? ?public class SalaryImpl implements Salary, Serializable {? ? ? ? ? //other attributes here? ? ? ? ? //getter and setters? ? ? ? ? //other methods? ? ? ? ? @Override? ? ? ? ? public double computeSalary(List<Benefit> benefits, List<Deduction> deductions){? ? ? ? ? ? ? ? ?return 0;? ? ? ? ? }? ?}
查看完整描述

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, .... ,.....);

    }


}


查看完整回答
反對 回復(fù) 2023-07-13
?
泛舟湖上清波郎朗

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 上完成)。


查看完整回答
反對 回復(fù) 2023-07-13
  • 2 回答
  • 0 關(guān)注
  • 175 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號