简体   繁体   中英

Passing DataSource object from a servlet to a JavaBean

I like the ease of using @Resource annotation to get a DataSource, but as far as I know, it's not possible to use it in a regular JavaBean. Would it be considered a bad practice if I pass the DataSource object from a servlet to a bean along with the other data to avoid having that lookup code in the bean?

It is certainly a bad practice to pass the data source to bean calls. You better use one of the dependency inversion frameworks, such as Spring or Guice. The former has utilities to inject required DataSources to configured beans, among to many other useful things.

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