在spring-config中自动装配jee-jdni-lookup数据源条目到@repository dao impl类

时间:2013-09-30 16:01:55

标签: spring jdbc annotations repository dao

我正在为DAO impl类使用@repository注释,但不能将@autowire或@resource在beanRefContext.xml配置中定义的数据源作为 jee:jndi-lookup标签

我有所有必需的标签条目,例如注释和组件包扫描

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/xyzName"
    cache="true" resource-ref="true" lookup-on-startup="false"
    proxy-interface="javax.sql.DataSource" /> 

我试过跟随@Repository impl类,

@Resource(mappedName="jdbc/xyzName")
private DataSource dataSource;

@Resource(name="datasource")
private DataSource dataSource;

@Autowired
private DataSource dataSource;

请帮忙!

######### Main xml file ##########
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
     <!-- This will automatically declare several post-processors
          including the AutowiredAnnotationBeanPostProcessor -->      
     <context:annotation-config/>
     <context:component-scan base-package="com.domain"/>
     <bean id="context" lazy-init="true"
        class="org.springframework.context.support.ClassPathXmlApplicationContext">
        <constructor-arg value="ejbBeans.xml" /> 
    </bean>     
</beans>

########## EJB beans xml file ################

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd 
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

    <tx:annotation-driven transaction-manager="someTransactionManager" />
    <bean
        class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor">
        <property name="alwaysUseJndiLookup" value="true" />
    </bean>

    <bean id="someTransactionManager"
        class="org.springframework.transaction.jta.WebSphereUowTransactionManager">
    </bean>

    <jee:jndi-lookup id="dataSource" jndi-name="jdbc/xyzName"
        cache="true" resource-ref="true" lookup-on-startup="false"
        proxy-interface="javax.sql.DataSource" />
</beans>

现在得到以下异常java.lang.IllegalArgumentException:'dataSource'或'jdbcTemplate'是必需的..以下是我的DAOImpl类的代码

@Repository 公共类UtilityDAOImpl扩展了JdbcDaoSupport实现         UtilityDAO {     final String className = UtilityDAOImpl.class.getName();

private JdbcTemplate jdbcTemplate ;

@Autowired
@Qualifier("dataSource")
public void setJdbcTemplate(DataSource dataSource) {
    this.jdbcTemplate = new JdbcTemplate(dataSource);
}

编辑:得到此异常

; nested exception is: org.springframework.beans.factory.access.BootstrapException: Unable to initialize group definition. Group resource name [classpath*:beanRefContext.xml], factory key [null]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ProgramEligiblityEventCheckController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com...domain.dao.vista.VistaEligiblityEventDAO com...domain.core.claim.ProgramEligiblityEventCheckController.vistaEligiblityEventDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'vistaEligiblityEventDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com...domain.dao..UtilityDAO com...domain.dao.vista.impl.VistaEligiblityEventDAOImpl.utilityDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UtilityDAOImpl' defined in file [C:\workspace_1.2\ejb12\ejbModule\com\\\domain\dao\\impl\UtilityDAOImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required
org.springframework.beans.factory.access.BootstrapException: Unable to initialize group definition. Group resource name [classpath*:beanRefContext.xml], factory key [null]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ProgramEligiblityEventCheckController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com...domain.dao.vista.VistaEligiblityEventDAO com...domain.core.claim.ProgramEligiblityEventCheckController.vistaEligiblityEventDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'vistaEligiblityEventDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com...domain.dao..UtilityDAO com...domain.dao.vista.impl.VistaEligiblityEventDAOImpl.utilityDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UtilityDAOImpl' defined in file [C:\workspace_1.2\ejb12\ejbModule\com\\\domain\dao\\impl\UtilityDAOImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required
    at org.springframework.beans.factory.access.SingletonBeanFactoryLocator.useBeanFactory(SingletonBeanFactoryLocator.java:386)
    at org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor.getBeanFactoryReference(SpringBeanAutowiringInterceptor.java:160)
    at org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor.getBeanFactory(SpringBeanAutowiringInterceptor.java:141)
    at org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor.doAutowireBean(SpringBeanAutowiringInterceptor.java:121)
    at org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor.autowireBean(SpringBeanAutowiringInterceptor.java:95)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:48)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:600)
    at com.ibm.ejs.container.interceptors.InterceptorProxy.invokeInterceptor(InterceptorProxy.java:227)
    at com.ibm.ejs.container.interceptors.InvocationContextImpl.proceed(InvocationContextImpl.java:566)
    at com.ibm.ejs.container.interceptors.InvocationContextImpl.doLifeCycle(InvocationContextImpl.java:264)
    at com.ibm.ejs.container.SessionBeanO.callPostConstructInterceptors(SessionBeanO.java:260)
    at com.ibm.ejs.container.StatelessBeanO.initialize(StatelessBeanO.java:409)
    at com.ibm.ejs.container.CMStatelessBeanOFactory.create(CMStatelessBeanOFactory.java:45)
    at com.ibm.ejs.container.EJSHome.createBeanO(EJSHome.java:1031)
    at com.ibm.ejs.container.EJSHome.createBeanO(EJSHome.java:1141)
    at com.ibm.ejs.container.activator.UncachedActivationStrategy.atActivate(UncachedActivationStrategy.java:84)
    at com.ibm.ejs.container.activator.Activator.activateBean(Activator.java:599)
    at com.ibm.ejs.container.EJSContainer.preInvokeActivate(EJSContainer.java:3964)
    at com.ibm.ejs.container.EJSContainer.EjbPreInvoke(EJSContainer.java:3349)
    at com..domain.ejb.facade.EJSRemote0SLExternalRequestDelegatorFacad_35bfd46c.processRequest(EJSRemote0SLExternalRequestDelegatorFacad_35bfd46c.java)
    at com..domain.ejb.facade._EJSRemote0SLExternalRequestDelegatorFacad_35bfd46c_Tie.processRequest(_EJSRemote0SLExternalRequestDelegatorFacad_35bfd46c_Tie.java)
    at com..domain.ejb.facade._EJSRemote0SLRequestDelegatorFacad_35bfd46c_Tie._invoke(_EJSRemote0SLExternalRequestDelegatorFacad_35bfd46c_Tie.java)
    at com.ibm.CORBA.iiop.ServerDelegate.dispatchInvokeHandler(ServerDelegate.java:622)
    at com.ibm.CORBA.iiop.ServerDelegate.dispatch(ServerDelegate.java:475)
    at com.ibm.rmi.iiop.ORB.process(ORB.java:513)
    at com.ibm.CORBA.iiop.ORB.process(ORB.java:1574)
    at com.ibm.rmi.iiop.Connection.respondTo(Connection.java:2841)
    at com.ibm.rmi.iiop.Connection.doWork(Connection.java:2714)
    at com.ibm.rmi.iiop.WorkUnitImpl.doWork(WorkUnitImpl.java:63)
    at com.ibm.ejs.oa.pool.PooledThread.run(ThreadPool.java:118)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1550)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ProgramEligiblityEventCheckController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com...domain.dao.vista.VistaEligiblityEventDAO com.domain.core.claim.ProgramEligiblityEventCheckController.vistaEligiblityEventDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'vistaEligiblityEventDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com...domain.dao..UtilityDAO com...domain.dao.vista.impl.VistaEligiblityEventDAOImpl.utilityDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UtilityDAOImpl' defined in file [C:\workspace_1.2\ejb12\ejbModule\com\\\domain\dao\\impl\UtilityDAOImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.context.access.ContextSingletonBeanFactoryLocator.initializeDefinition(ContextSingletonBeanFactoryLocator.java:143)
    at org.springframework.beans.factory.access.SingletonBeanFactoryLocator.useBeanFactory(SingletonBeanFactoryLocator.java:381)
    ... 32 more

有问题的课程

@Repository
public class TempUtilityDAOImpl extends JdbcDaoSupport implements
        TempUtilityDAO {
    final String className = TempUtilityDAOImpl.class.getName();

    private JdbcTemplate jdbcTemplate ;

    @Autowired
    @Qualifier("dataSource")
    public void setJdbcTemplate(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    private static String IS_DEALER_IN_Temp = "select count(id_dlr) from "
            + TempConstants.DB_NAME_Temp + "V_Temp_DLR where id_dlr = ? with UR";


            /**
     * Method verifies that Dealer is not an Canadian dealer.
     * 
     * @param dealerID
     * @return
     * @throws TempSystemException
     */
    public boolean verifyTempDealer(String dealerID) throws TempSystemException {

        boolean isTempDealer = false;
        final String methodName = "verifyTempDealer(String dealerID)";
        MSLog.debug(className, "Exiting Method : " + methodName);
        int rowcount = getJdbcTemplate().queryForInt(IS_DEALER_IN_Temp,
                dealerID);

        if (rowcount > 0) {
            isTempDealer = true;
        }

        MSLog.debug(className, "Exiting Method : " + methodName);
        return isTempDealer;
    }
}

1 个答案:

答案 0 :(得分:0)

我不知道

<bean id="context" lazy-init="true"
    class="org.springframework.context.support.ClassPathXmlApplicationContext">
    <constructor-arg value="ejbBeans.xml" /> 
</bean>  

可以将bean从一个上下文导入到另一个上下文。

你应该使用

<import resource="ejbBeans.xml"/>

ejbBeans.xml中声明的所有bean都将在import上下文中提供。