Can @DataSourceDefinition annotation be used in Spring Context?
Can I use the new @DataSourceDefinition
instead of declaring the Datasource
in a Spring Context?
@DataSourceDefinition(name="java:global/MyApp/MyDataSource",
className="com.foobar.MyDataSource",
portNumber=6689,
serverName="myserver.com",
user="lance"开发者_JAVA技巧,
password="secret"
)
Using a 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:
@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/
spring doesn't know about this annotation.
But, as always, you can use Spring's @Configuration
annotation to declare your datasource within code:
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
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.
Example:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/MyJdbcDataSource" />
精彩评论