1、正确的
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--注解扫描-->
<context:component-scan base-package="com.cl"></context:component-scan>
</beans>
2、错误的
报错:通配符的匹配很全面, 但无法找到元素 ‘context:component-scan’ 的声明。
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--注解扫描-->
<context:component-scan base-package="com.cl"></context:component-scan>
</beans>
两者最大的区别是:
正确:http:
//www.springframework.org/schema/context/spring-context.xsd
报错:https:
//www.springframework.org/schema/context/spring-context.xsd
3、总结
直接拿正确的xml复制下就行,尽量不要自己添加。否则错误的话,自己找的话很费时!!!