Java 弹簧不工作
当我在Eclipse IDE中将鼠标悬停在XML代码上时,出现了以下错误:Java 弹簧不工作,java,spring,dependency-injection,spring-java-config,constructor-injection,Java,Spring,Dependency Injection,Spring Java Config,Constructor Injection,当我在Eclipse IDE中将鼠标悬停在XML代码上时,出现了以下错误: cvc-complex-type.2.4.a: Invalid content was found starting with element 'constructor-args'. One of '{"http:// www.springframework.org/schema/beans":import, "http://www.springframework.org/schema/beans":alias,
cvc-complex-type.2.4.a: Invalid content was found starting with element 'constructor-args'. One of '{"http:// www.springframework.org/schema/beans":import, "http://www.springframework.org/schema/beans":alias, "http://www.springframework.org/schema/beans":bean, WC[##other:"http://www.springframework.org/ schema/beans"], "http://www.springframework.org/schema/beans":beans}' is expected.
以下是我在XML配置文件中编写的XML代码。我不明白这段代码中的错误
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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">
<!-- Define your beans here -->
<!-- dependency definition -->
<bean id="cheer-up" class="package.CheerUpClass"></bean>
<!-- Inversion of control -->
<bean id="Coach" class="package.CricketCoach"></bean>
<!-- constructor injection -->
<constructor-arg ref="cheer-up" />
</beans>
您也应该直接在IDE上得到一个错误
constructor arg
是一个可以存在于bean
元素下而不是顶层的元素。尝试在xml中使用以下声明。希望它能解决你的问题
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframew``ork.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">
您的构造函数arg标记位于bean标记之外。把它放在豆子标签里。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframew``ork.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">