Java 如何为带有注释的spring设置context.xml?
我正在使用Tomcat托管一个webapp,我想知道我是否可以使用Spring注释在Tomcat的conf文件夹中创建一个等价的Java 如何为带有注释的spring设置context.xml?,java,xml,spring,tomcat,Java,Xml,Spring,Tomcat,我正在使用Tomcat托管一个webapp,我想知道我是否可以使用Spring注释在Tomcat的conf文件夹中创建一个等价的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 s
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>
WEB-INF/WEB.xml
如果可以通过注释实现这一点,您能否通过一个示例向我展示如何更好地实现这一点?据我所知,没有与以下
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" />
...
数据源配置写入服务器配置文件,因为服务器管理连接池,但是,如果您使用了不同的池管理器,您可以定义数据源属性以避免任何数据源服务器配置。我需要使用@ContextConfiguration来指向我想要使用的xml。请澄清为什么需要创建与
context.xml
等效的内容。我正在使用的服务器是因为有许多其他Web应用程序在其上运行,所以无法在其上使用default context.xml。为什么不能将配置添加到此类默认上下文中?有很多方法可以做到这一点。
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);
}
}
...