简体   繁体   中英

Can I use injected beans as arguments for another class' method?

I am working on a hibernate project and I'm moving some logic from a BLL class to a DTO, and I was wondering if it's possible to inject objects into a DTO? The code from the BLL class relied on a lot of imported beans, but when I tried importing them into my DTO object my applicationContext would mess up.

FlightHelper class:

public class FlightHelper {

    @Inject
    private InjectedClass injectedClass;

    public void testMethod(Flight flight) {
        ...code here
        flight.getPrice(injectedClass);
    }
}

Flight class:

public class Flight {
    public void getPrice(InjectedClass injectedClass) {
        ...code here
    }
}

Yes, you can.

However, the design is not very nice because you have a very strong interaction between Flight and FlightHelper classes.

是的,您可以,但是您可能还想将它们注入另一个类中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM