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

  • Annotation-based DI of multiple JNDI resources

    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
    Try using @Qualifier with Autowired. Since both datasources are implementing same interface @Qualifier is required.

    Comment

    Working...
    X