Announcement Announcement Module
Collapse
No announcement yet.
Annotation-based DI of multiple JNDI resource Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Annotation-based DI of multiple JNDI resource

    My application uses two data sources (container managed) exposed to the Spring container via jee-jndi-lookup. The two data sources are used by separate hierarchy of @Repository and @Service components. Since I am trying to keep my Spring configuration files lean with the use of annotations, I ended up being unsure about how the data sources will be wired to their respective @Repository components.

    The relevant snippet from my Spring configuration file:
    Code:
    <context:annotation-config />
    
    <context:component-scan base-package="my.package" />
    
    <jee:jndi-lookup id="omsData"
    		      jndi-name="jdbc/omsDB"
    		      resource-ref="true" 
    		      proxy-interface="javax.sql.DataSource" />
    
    <jee:jndi-lookup id="corpData"
    		      jndi-name="jdbc/corpDB"
    		      resource-ref="true" 
    		      proxy-interface="javax.sql.DataSource" />
    Here is how I am trying to get the DI magic to manifest in the respective DAO component implementations:
    OMSRepositoryJdbcImpl
    Code:
    @Repository
    public class OMSRepositoryJdbcImpl implements IOMSRepository {
    
    	private JdbcTemplate OMSRepositoryTemplate;
    
    	@Autowired
    	public OMSRepositoryJdbcImpl(DataSource omsData) {
    		this.OMSRepositoryTemplate= new JdbcTemplate(omsData);
    	}
    }
    CorpRepositoryJdbcImpl
    Code:
    @Repository
    public class CorpRepositoryJdbcImpl implements ICorpRepository {
    
    	private JdbcTemplate CorpRepositoryTemplate;
    
    	@Autowired
    	public CorpRepositoryJdbcImpl(DataSource corpData) {
    		this.CorpRepositoryTemplate= new JdbcTemplate(corpData);
    	}
    }
    If I had defined the data sources using standard <bean /> declarations, I would have used the following attribute to direct the DI magic.
    Code:
    autoWire="byName"
    But the <jee:jndi-lookup /> declaration does not seem to facilitate this control. How can I ensure that the two data sources are DI'd into the respective @Repository components correctly?

    Thanks!

    Ashwin

  • #2
    Annotation-based DI of multiple JNDI resources

    Using org.springframework.jndi.JndiObjectFactoryBean appears to resolve my issue. Note that I am able to specify the autowire="byName" attribute.

    Code:
    <bean id="jdbc/omsDB" class="org.springframework.jndi.JndiObjectFactoryBean" autowire="byName">
    	<property name="jndiName" value="jdbc/omsDB" />
    	<property name="resourceRef" value="true" />
    	<property name="expectedType" value="javax.sql.DataSource" />
    	<property name="proxyInterface" value="javax.sql.DataSource" />
    </bean>
    
    <bean id="jdbc/corpDB" class="org.springframework.jndi.JndiObjectFactoryBean" autowire="byName">
    	<property name="jndiName" value="jdbc/corpDB" />
    	<property name="resourceRef" value="true" />
    	<property name="expectedType" value="javax.sql.DataSource" />
    	<property name="proxyInterface" value="javax.sql.DataSource" />
    </bean>
    Last edited by webuser; Jul 20th, 2011, 01:35 PM. Reason: elaborate comments in post

    Comment

    Working...
    X