简体   繁体   中英

Upgrade from spring 4.3.2 to 5.0.2 is causing missing @AliasFor error

When I try to upgrade a spring web-mvc application from version 4.3.2 to 5.0.2, I get an error in servlet xml. The error happens in the following line,

<context:component-scan base-package="com.mypackage" />

Error is as follows,

Error occured processing XML '@AliasFor declaration on attribute [value] in annotation [org.springframework.stereotype.Controller] is missing required 'attribute' value.'. See Error Log for more details

This is how my servlet xml looks like.

<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.2.xsd">


<security:global-method-security secured-annotations="enabled"/>

<context:component-scan base-package="com.mypackage" />

<mvc:view-controller path="/" view-name="index"/>

<mvc:resources mapping="/resources/**" location="/resources/" />

</beans>

If I use 5.0.0 this error is not happening. As it is complaining about a missing annotation in org.springframework.stereotype.Controller, I tried to find out what is newly added or missing in the Controller. I could see that a new line is added in version 5.0.2 and it has a missing 'attribute'.

@AliasFor(annotation = Component.class)

I am sure I am missing something in my bean class definition which is required from version 5.0.2.

I'd check for three things.

First, you need to make sure that your dependency management is right. You can run mvn dependency:tree and check that all Spring dependencies are using the same version. If you want to avoid issues like this in the future, you can use the Spring Framework BOM and let it manage versions for Spring Framework artifacts:

<dependencyManagement>
    <dependencies>
            <dependency> 
                <groupId>org.springframework</groupId> 
                <artifactId>spring-framework-bom</artifactId> 
                <version>${spring.version}</version> 
                <type>pom</type> 
                <scope>import</scope> 
            </dependency> 
        </dependencies> 
</dependencyManagement>

If your dependency management is right, this might be a corrupted JAR. You can clear things and re-download dependencies with mvn dependency:purge-local-repository .

Finally, using versioned schemas in your XML is not advised anymore - in fact, it's not supported anymore as of Spring Framework 5.0, see SPR-13499 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM