Spring构造函数注入奇怪的错误

时间:2015-06-26 06:58:23

标签: spring

我在Eclipse中的Bean XML(root-context.xml)中的构造函数-arg依赖行下面遇到了BeanDefinitionDecorator错误,但是当使用setter注入属性元素时没有问题,感谢这里的任何人都可以提供建议,而不是即使在通过互联网搜索后也能识别根本原因。提前谢谢。

遇到错误

“此行找到了多个注释:

  • 找不到BeanDefinitionDecorator for element [constructor-arg]

  • cvc-complex-type.2.4.c:匹配的通配符是strict,但是找不到元素'constructor-arg'的声明。

  • 配置问题:无法找到BeanDefinitionDecorator for element [constructor-arg] Offending resource:file [C:/ Users / Administrator / 工作区/ EsmProject / SRC /主/ web应用/ WEB-INF /弹簧/根的context.xml]“
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:beans="http://www.springframework.org/schema/beans"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

        <beans:bean id="myObject" class="com.packapp.MyObject"></beans:bean>

        <beans:bean id="homeDAO" class="com.packapp.HomeDAOImpl">
           <constructor-arg><ref bean="myObject"/></constructor-arg>
        </beans:bean>

        <beans:bean id = "homeService" class="com.packapp.HomeServiceImpl">
           <beans:property name="homeDAO" ref="homeDAO"></beans:property>
        </beans:bean>

        <context:component-scan base-package="com.packapp" />
    </beans:beans>
public class MyObject {
    private String homeName;
    private String homeAddress;

    public String getHomeName(){
        return homeName;
    }
    public void setHomeName(String homeName){
        this.homeName = homeName;
    }
    public String getHomeAddress(){
        return homeAddress;
    }
    public void setHomeAddress(String homeAddress){
        this.homeAddress = homeAddress;
    }
}   
@Repository
public class HomeDAOImpl implements HomeDAO {
    private MyObject obj;
    public HomeDAOImpl(MyObject obj){
        this.obj = obj;
    }
    public String getAddress() {
        return this.obj.getHomeAddress();

    }
}
@Service
public class HomeServiceImpl implements HomeService {
    private HomeDAO homeDAO;
    public void setHomeDAO(HomeDAO homeDAO){
        this.homeDAO = homeDAO;
    }
    public String getAddress() {
        return this.homeDAO.getAddress();
    }

}

@Controller
@RequestMapping("ctrl2")
public class HomeController2 {
    private HomeService homeService;

    @Autowired(required=true)
    @Qualifier(value="homeService")
    public void setHomeService (HomeService hs){
        this.homeService = hs;
    }

    @RequestMapping(value = "/site2", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

        String formattedDate = dateFormat.format(date);

        model.addAttribute("serverTime", formattedDate + this.homeService.getAddress());

        return "home2";
    }
}

1 个答案:

答案 0 :(得分:3)

我认为<constructor-arg><ref bean="myObject"/></constructor-arg>

必须是<beans:constructor-arg><beans:ref bean="myObject"/></beans:constructor-arg>

因为spring-beans.xsd中有元素的前缀。