[英]How to set up a context.xml for spring with annotations?
I am using Tomcat to host a webapp and would like to know if I can use Spring annotations to create the equivalent of a context.xml
in the conf folder of tomcat. 我正在使用Tomcat托管Web应用程序,并且想知道是否可以使用Spring批注在tomcat的conf文件夹中创建context.xml
的等效项。 An example context.xml
: 一个示例context.xml
:
<?xml version="1.0" encoding="UTF-8"?> <!-- The contents of this file will be loaded for each web application -->
<Context> <!-- Default set of monitored resources -->
<ResourceLink name="jdbc/SomeDB" global="jdbc/SomeDB" type="javax.sql.DataSource"/>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
If this is possible to do with annotations can you show me how it is done preferably with an example? 如果可以通过注释进行操作,那么可以通过示例向我展示如何完成此操作吗?
As far as I know there is no annotation equivalent to the following applicationContext.xml
fragment: 据我所知,没有等效于以下applicationContext.xml
片段的注释:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">
<jee:jndi-lookup
id="someDbDataSource"
jndi-name="jdbc/SomeDB"
resource-ref="true" />
...
because it is very easy inject the datasource into your DAOs: 因为很容易将数据源注入到您的DAO中:
public class SomeDbJdbcDaoSupport {
public static final String SOME_DB_DATA_SOURCE = "someDbDataSource";
private JdbcTemplate jdbcTemplate;
@Autowired
@Qualifier(SOME_DB_DATA_SOURCE)
public void setSomeDbDataSource(DataSource dataSource) {
if (jdbcTemplate == null || dataSource != jdbcTemplate.getDataSource()) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
}
...
Datasource configuration is written into the server configuration files because the server manages a pool of connections, but if you used a different pool manager like c3po you would define datasource properties in spring configuration files avoiding any datasource server configurations. 数据源配置被写入服务器配置文件中,因为服务器管理连接池,但是如果您使用诸如c3po之类的其他池管理器,则会在spring配置文件中定义数据源属性,从而避免任何数据源服务器配置。
我需要使用@ContextConfiguration指向我要使用的xml。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.