简体   繁体   English

将DataSource对象从servlet传递到JavaBean

[英]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. 我喜欢使用@Resource批注轻松获得数据源,但是据我所知,不可能在常规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? 如果我将Servlet中的DataSource对象与其他数据一起传递给Bean,以避免在Bean中包含该查找代码,那将是一种不好的做法吗?

It is certainly a bad practice to pass the data source to bean calls. 将数据源传递给bean调用肯定是一个坏习惯。 You better use one of the dependency inversion frameworks, such as Spring or Guice. 最好使用依赖项反转框架之一,例如Spring或Guice。 The former has utilities to inject required DataSources to configured beans, among to many other useful things. 前者具有实用程序,可将所需的数据源注入到配置的Bean中,以及其他许多有用的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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