[英]Can @DataSourceDefinition annotation be used in Spring Context?
Can I use the new @DataSourceDefinition
instead of declaring the Datasource
in a Spring Context?我可以使用新的
@DataSourceDefinition
而不是在 Spring 上下文中声明Datasource
吗?
@DataSourceDefinition(name="java:global/MyApp/MyDataSource",
className="com.foobar.MyDataSource",
portNumber=6689,
serverName="myserver.com",
user="lance",
password="secret"
)
Using a URL:使用 URL:
@DataSourceDefinition(name="java:global/MyApp/MyDataSource",
className="org.apache.derby.jdbc.ClientDataSource",
url="jdbc:derby://localhost:1527/myDB",
user="lance",
password="secret"
)
An example lookup of the DataSource from an EJB:从 EJB 中查找 DataSource 的示例:
@Stateless
public class MyStatelessEJB {
@Resource(lookup="java:global/MyApp/myDataSource")
DataSource myDB;
...
}
According to documentation,根据文件,
http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/ http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/
spring doesn't know about this annotation. spring 不知道这个注释。
But, as always, you can use Spring's @Configuration
annotation to declare your datasource within code:但是,与往常一样,您可以使用 Spring 的
@Configuration
注释在代码中声明您的数据源:
Example:例子:
@Configuration
public class DatabaseConfig {
@Bean
public DataSource dataSource() {
// instantiate, configure and return DataSource
}
}
Other examples are here: http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/context/annotation/Configuration.html其他示例在这里: http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/context/annotation/Configuration.html
If you need access to JNDI resource, you can use Spring's jee namespace (xml configuration), explained in: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/xsd-config.html#xsd-config-body-schemas-jee , section C.2.3 The jee schema.如果需要访问JNDI资源,可以使用Spring的jee命名空间(xml配置),解释在: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/xsd- config.html#xsd-config-body-schemas-jee ,部分 C.2.3 jee 模式。
Example:例子:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/MyJdbcDataSource" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.