【问题标题】:UserDao is null, I am wiring the bean wrong and need helpUserDao 为空,我的 bean 接线错误,需要帮助
【发布时间】:2010-07-18 04:20:36
【问题描述】:

我的 homecontroller 有一个使用 spring 正确连接的 UserService 对象(它使用没有 UserService 的方法渲染索引页面就好了)。

现在我设置了休眠模式,所以在 UserService 中我有一个 UserDao 对象,我正在尝试使用 spring 进行连接。

@Service
public class UserServiceImpl implements UserService{

    UserDao userDao;

    public String sayHello() {

        return "hello from user service impl part 2";

    }

    public String getTestUser() {





        return userDao.getById(1L).getUsername();

    }

}

所以我的 HomeController 正在调用 'sayHello' 方法,它工作正常,就像我说的那样。

@Controller
public class HomeController {

    @Autowired
    private UserService userService;




    @RequestMapping("/")
    public ModelAndView Index() {



        ModelAndView mav = new ModelAndView();

        mav.setViewName("index");
        mav.addObject("message", userService.sayHello());

        mav.addObject("username", userService.getTestUser());
        //userService.getTestUser();

        return mav;
    }

对 userService.getTestUser() 的调用失败,因为 UserDao 为空。

我的 app-config.xml 是:

<!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--<property name="packagesToScan" value="com.blah.core.db.hibernate"/>  -->
        <property name="configLocation" value="/WEB-INF/classes/hibernate.cfg.xml"/> 
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
                hibernate.connection.url=jdbc:mysql://localhost/blah
                hibernate.connection.username=dbuser
                hibernate.connection.password=123
                hibernate.query.substitutions=true 'Y', false 'N'
                hibernate.cache.use_query_cache=true
                hibernate.cache.use_second_level_cache=true
                hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
                hibernate.jdbc.batch_size=0
            </value>
        </property>
    </bean>

    <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->


     <bean id="userDao" class="com.blah.core.db.hibernate.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

为什么我的 UserDao 为空?我一定是接线有问题?

另外,如果我取消注释掉 name=packagesToScan 行,我是否真的需要像使用 UserDao 一样为每个 Dao 定义一个 bean? sessionFactory 会以某种方式连接吗?

【问题讨论】:

  • 关于您的第二个问题,您可以在 DAO 内的 entitymanager 声明中使用 @PersistenceContext 以避免必须将 sessionfactory 连接到每一个。

标签: java hibernate spring spring-mvc


【解决方案1】:

给userDao添加@AutoWired注解。

@Service
public class UserServiceImpl implements UserService{

    @AutoWired
    UserDao userDao;

    ...
}

并确保您已设置 &lt;context:component-scan/&gt; 来扫描您的 @Services 和 @Controllers 所在的包。

【讨论】:

  • 我的服务和控制器正在工作,因为我有自动扫描功能,无论如何感谢您的提示。
【解决方案2】:

正如 krock 所提到的,您的 UserDao 没有在您的 UserService 中正确“连接”。你有没有尝试过他的建议?

【讨论】:

    猜你喜欢
    • 2016-02-11
    • 2022-08-06
    • 2011-09-02
    • 1970-01-01
    • 2014-11-20
    • 2021-12-11
    • 2018-12-30
    • 1970-01-01
    • 2010-09-23
    相关资源
    最近更新 更多