java 如何以编程方式使用 Spring 的 JdbcTemplate?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1779200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:56:47  来源:igfitidea点击:

How to programmatically use Spring's JdbcTemplate?

javaspringdependency-injectionspring-jdbc

提问by Marcus Leon

We use Spring's JdbcTemplatewhich is configured through Spring config as illustrated below. Is there a way to do this without injecting the data source? I'd like to just create the JdbcTemplateinstance programmatically and "initalize" the datasource using TheOracleDS.

我们使用JdbcTemplate通过 Spring config 配置的 Spring,如下图所示。有没有办法在不注入数据源的情况下做到这一点?我只想以JdbcTemplate编程方式创建实例并使用TheOracleDS.

Our current config:

我们目前的配置:

Java class

Java类

private JdbcTemplate jdbcTemplate;

@Resource(name = "myDataSource")
public void setDataSource(DataSource dataSource) {
     this.jdbcTemplate = new JdbcTemplate(dataSource);
}

Spring config

弹簧配置

<jee:jndi-lookup id="myDataSource" jndi-name="java:/TheOracleDS"/>

Oracle datasource config

Oracle 数据源配置

<xa-datasource>
      <jndi-name>TheOracleDS</jndi-name>
      ...
</xa-datasource>


Update: Reason I'm asking this is I'm not a total believer in dependency injection / having Spring manage beans..

更新:我问这个的原因是我不完全相信依赖注入/让 Spring 管理 bean..

采纳答案by oxbow_lakes

Just use a raw JNDI lookup:

只需使用原始 JNDI 查找:

public void setDataSourceName(String name) {
    InitialContext ctx = new InitialContext();
    jdbcTemplate = new JdbcTemplate((DataSource) ctx.lookup(name));
}

回答by Pascal Thivent

Not sure why you want to do that but... you could lookup the JDNI datasource with Spring's JndiDataSourceLookup:

不知道为什么要这样做,但是……您可以使用 Spring 查找 JDNI 数据源JndiDataSourceLookup

JndiDataSourceLookup lookup = new JndiDataSourceLookup();
lookup.setResourceRef(true); // if the lookup occurs in a J2EE container
DataSource ds = lookup.getDataSource(jndiName);

Or just perform a "manual" lookup using Sun's classes:

或者只是使用 Sun 的类执行“手动”查找:

Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jdbc/AcmeDB");

Then, just pass the datasource reference to the JdbcTemplateconstructor or call setDataSource(ds).

然后,只需将数据源引用传递给JdbcTemplate构造函数或调用setDataSource(ds).

But, as I said, I have no idea why you don't want to use injection.

但是,正如我所说,我不知道您为什么不想使用注入。

回答by Ken Bloom

Here's some sample code from a project I've written:

这是我编写的项目中的一些示例代码:

SimpleJdbcTemplate db;
DataSource dataSource = new SingleConnectionDataSource(System.getProperty(
         "lingcog.db.connectstring"),
      System.getProperty("lingcog.db.username"),
      System.getProperty("lingcog.db.password"), false);

db = new SimpleJdbcTemplate(dataSource);

Maybe my code would be simpler if I used injection, but this is a good example of how to do this without using injection.

如果我使用注入,也许我的代码会更简单,但这是一个很好的例子,说明如何在不使用注入的情况下做到这一点。

You can use an org.springframework.jdbc.datasource.lookup.JndiDataSourceLookupobject to find the data source you want by JDNI name.

您可以使用org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup对象通过 JDNI 名称查找您想要的数据源。

DataSource dataSource = new JndiDataSourceLookup().getDataSource("java:/TheOracleDS")
SimpleJdbcTemplate db=new SimpleJdbcTemplate(dataSource);