Spring 3 不注入 DAO bean

发布于 2024-12-16 18:08:18 字数 4250 浏览 4 评论 0原文

我正在使用 Spring 3 和 UserServiceImpl。该服务看不到 Dao bean。

这是我的 UserDao:

    @Repository
    public class UserDaoImpl implements UserDao {
        private SessionFactory sessionFactory;

        public SessionFactory getSessionFactory() {
            return this.sessionFactory;
        }

        @Autowired
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }

        private Session currentSession() {
            return sessionFactory.getCurrentSession();
        }
    }

然后是 Service bean:

    public class UserServiceImpl implements UserService {
        @Autowired
        private UserDao userDao;

        public void setUserDao(UserDao userDao) {
            this.userDao = userDao;
        }


        public String testeoDAO() {
            return userDao.funciona();
        }
    }

testeoDAO 方法仅用于测试 DAO。applicationContext

    <context:annotation-config/>

    <bean id="userDao" class="com.apress.usermanager.dao.hibernate.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean id="userService" class="com.apress.usermanager.service.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>


    <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
          autowire="byName">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="com.apress.usermanager"/>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
                hibernate.query.substitutions=true 'Y', false 'N'
                hibernate.cache.use_second_level_cache=true
                hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
                hibernate.hbm2ddl.auto=update
                hibernate.use_sql_comments=true
                hibernate.show_sql=true
                hibernate.current_session_context_class=thread
            </value>
        </property>
    </bean>


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="100"/>
        <property name="maxWait" value="1000"/>
        <property name="poolPreparedStatements" value="true"/>
        <property name="defaultAutoCommit" value="true"/>
        <property name="testOnBorrow" value="true"/>
        <property name="validationQuery" value="select 1=1"/>
    </bean>


    <bean id="staticDataImporter" class="com.apress.usermanager.support.StaticDataImporter" depends-on="sessionFactory">
        <property name="dataSource" ref="dataSource"/>
        <property name="staticData" value="classpath:default-data.xml"/>
    </bean>

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

和错误:

java.lang.NullPointerException 在 com.apress.usermanager.service.UserServiceImpl.testeoDAO(UserServiceImpl.java:49) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method.invoke(Method.java:597)

I'm using Spring 3 and UserServiceImpl. The service does not see the Dao bean.

This is my UserDao:

    @Repository
    public class UserDaoImpl implements UserDao {
        private SessionFactory sessionFactory;

        public SessionFactory getSessionFactory() {
            return this.sessionFactory;
        }

        @Autowired
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }

        private Session currentSession() {
            return sessionFactory.getCurrentSession();
        }
    }

Then the Service bean :

    public class UserServiceImpl implements UserService {
        @Autowired
        private UserDao userDao;

        public void setUserDao(UserDao userDao) {
            this.userDao = userDao;
        }


        public String testeoDAO() {
            return userDao.funciona();
        }
    }

The testeoDAO method had writeen only for testing the DAO.The applicationContext

    <context:annotation-config/>

    <bean id="userDao" class="com.apress.usermanager.dao.hibernate.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean id="userService" class="com.apress.usermanager.service.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>


    <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
          autowire="byName">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="com.apress.usermanager"/>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
                hibernate.query.substitutions=true 'Y', false 'N'
                hibernate.cache.use_second_level_cache=true
                hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
                hibernate.hbm2ddl.auto=update
                hibernate.use_sql_comments=true
                hibernate.show_sql=true
                hibernate.current_session_context_class=thread
            </value>
        </property>
    </bean>


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="100"/>
        <property name="maxWait" value="1000"/>
        <property name="poolPreparedStatements" value="true"/>
        <property name="defaultAutoCommit" value="true"/>
        <property name="testOnBorrow" value="true"/>
        <property name="validationQuery" value="select 1=1"/>
    </bean>


    <bean id="staticDataImporter" class="com.apress.usermanager.support.StaticDataImporter" depends-on="sessionFactory">
        <property name="dataSource" ref="dataSource"/>
        <property name="staticData" value="classpath:default-data.xml"/>
    </bean>

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

And the error :

java.lang.NullPointerException
at com.apress.usermanager.service.UserServiceImpl.testeoDAO(UserServiceImpl.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞笑我太脆弱 2024-12-23 18:08:18

我认为您需要添加

<context:component-scan base-package="com.a,com.b">
</context:component-scan>

扫描包 com.acom.b。这允许类路径扫描并向其添加所有@Component等。

@Service注释UserServiceImpl

问候。

I think you need to add

<context:component-scan base-package="com.a,com.b">
</context:component-scan>

To scan package com.a and com.b. This allows the classpath scan and add all @Component and so forth to it.

Annote UserServiceImpl with @Service.

Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文