Spring <context:component-scan base-package="groupid.aop"/> 无法定义

Spring <context:component-scan base-package="groupid.aop"/> cannot be defined

        <?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"


       xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd"

        >

<bean id="triangle1" class="groupid.aop.triangle">
<property name="name" value="triangle bean"></property>
</bean>
<bean id="circle1" class="groupid.aop.circle">
<property name="name" value="circle bean"></property>
</bean>
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <context:component-scan base-package="groupid.aop"/>

</beans>

我在 <context:component-scan base-package="groupid.aop"/> 这条线上遇到错误。我正在使用 Maven 为 spring 和 aop 依赖项添加依赖项。我还为上下文添加了命名空间。如果我遗漏了什么,请告诉我。

你的命名空间声明有误。 xml 应该可以解决问题:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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">
    <bean id="triangle1" class="groupid.aop.triangle">
        <property name="name" value="triangle bean" />
    </bean>
    <bean id="circle1" class="groupid.aop.circle">
        <property name="name" value="circle bean" />
    </bean>
    <aop:aspectj-autoproxy proxy-target-class="true" />
    <context:component-scan
        base-package="groupid.aop" />
</beans>

在你原来的 XML 中你有:

xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd          
                            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"

如您所见,您声明了两次 spring-beans(http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd)并且您使用了 3.0 上下文版本(http://www.springframework.org/schema/context/spring-context-3.0.xsd

希望对你有用

安杰洛