问答详情
源自:4-2 Spring Bean装配之Autowired注解说明-1

为什么使用Required注解会有异常?

@Required

public void setInjectionDAO(InjectionDAO injectionDAO) {

this.injectionDAO = injectionDAO;

}


org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'injectionServiceImpl' defined in file [D:\Myeclipse\Workspace\SpringCourse\bin\test8\InjectionServiceImpl.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'injectionDAO' is required for bean 'injectionServiceImpl'

提问者:去南极给企鹅洗热水澡 2016-10-25 22:01

个回答

  • kaiser1992
    2019-08-21 21:45:50

    既然配置required ,类的实例化应该是有先后顺序的吧。

  • 慕粉4000715
    2016-12-02 09:39:22

    @Required注解适用于bean属性的setter方法并且它指示,受影响的bean属性必须在配置时被填充在XML配置文件中,否则容器将抛出BeanInitializationException.

    也就是说,你要用@Required注解setter方法,就必须在Spring 配置文件中设置该属性

  • dupengchuan
    2016-10-28 10:46:42

    'injectionDAO'这个Bean名字匹配吗?