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

时间:2018-09-13 06:24:14

标签: spring maven spring-aop maven-dependency-plugin

        <?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依赖项添加了依赖项。我还为上下文添加了名称空间。请让我知道我是否缺少任何东西。

1 个答案:

答案 0 :(得分:1)

您在名称空间声明中错了。 此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

我希望它有用

天使