0

I have encountered below BeanDefinitionDecorator error at below constructor-arg dependency line in Bean XML (root-context.xml) in Eclipse, but no issue when using setter injection with property element, appreciate if anyone here could provide advice, as not able to identify the root cause even after search through Internet. Thanks in advance.

Error encountered

"Multiple annotations found at this line:

  • Cannot locate BeanDefinitionDecorator for element [constructor-arg]

  • cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'constructor-arg'.

  • Configuration problem: Cannot locate BeanDefinitionDecorator for element [constructor-arg] Offending resource: file [C:/Users/Administrator/ workspace/EsmProject/src/main/webapp/WEB-INF/spring/root-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";
    }
}
17
  • No it isn't a weird exception, you have another root namespace. You have bound the beans namespace to use, so prefix constructor-arg with beans: just like the property and bean element. Commented Jun 26, 2015 at 7:29
  • @M. Deinum, Thanks, after changed to beans, still encountered error, i.e. "Multiple annotations found at this line. Cannot locate BeanDefinitionParser for element [ref]. cvc-complex-type.2.4.c. The matching wildcard is strict, but no declaration can be found for element 'ref'." Commented Jun 26, 2015 at 7:46
  • The sample applies for the ref element. It applies to all elements that would occur in the spring-beans.xsd. However instead of using the ref element, just add it as an attribute to the constructor-arg. <beans constructor-arg ref="myObject" /> easier to read and less xml. Commented Jun 26, 2015 at 7:49
  • Thanks M. Deinum, Solved, but turned to BeanInstantiationException: Could not instantiate bean class [com.packapp.HomeDAOImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.packapp.HomeDAOImpl.<init>(), eventhough I have declared the constructor in class as shown in question. Commented Jun 26, 2015 at 8:48
  • Because you also have @Repository, which means component scanning will detect the bean. Due to the lack of @Autowired on the constructor it expects a default constructor. You basically have 2 instances. Either remove the xml configuration and add @Autowired to the constructor, or remove component-scanning. Commented Jun 26, 2015 at 8:58

1 Answer 1

3

I think <constructor-arg><ref bean="myObject"/></constructor-arg>

must be <beans:constructor-arg><beans:ref bean="myObject"/></beans:constructor-arg>

because you have a prefix for elements in spring-beans.xsd.

Sign up to request clarification or add additional context in comments.

8 Comments

@Leanne can you add this as an update to your question please?
Thanks for your response. Added into my question. So far still encountered the error, eventhough it is just a simple dependency setting. =(. Need expert here for help.
@Leanne see my updated answer. you also need beans: before ref.
Solved with <beans:constructor-arg ref="myObject" />. But got different error now, i.e. BeanInstantiationException: Could not instantiate bean class [com.packapp.HomeDAOImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.packapp.HomeDAOImpl.<init>(), eventhough I have declared the constructor in class as shown in question.
@Leanne Your class is annotated as @Repository, so you need a no arg constructor. or add an @Autowired annotation to your constructor public HomeDAOImpl(MyObject obj){
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.