0

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. An example 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?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
David Carek
  • 1,103
  • 1
  • 12
  • 26
  • Please clarify why you need creating the equivalent of a `context.xml`. – taringamberini Apr 17 '15 at 10:06
  • The server that I'm working with is supposed to have the default context.xml on it since it has many other webapps running on it. – David Carek Apr 17 '15 at 12:04
  • And why you can't add your configuration to such default context? There are [lots of strategies](http://stackoverflow.com/questions/15064260/tomcat-jndi-configuration-best-practice/23368871#23368871) to do that. – taringamberini Apr 20 '15 at 06:39

2 Answers2

0

As far as I know there is no annotation equivalent to the following applicationContext.xml fragment:

<?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:

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.

jeff porter
  • 6,560
  • 13
  • 65
  • 123
taringamberini
  • 2,719
  • 21
  • 29
0

I needed to use @ContextConfiguration to point to the xml I wanted to use.

David Carek
  • 1,103
  • 1
  • 12
  • 26