@Value annotation not resolved

10,406

Original Answer:

The @org.springframework.beans.factory.annotation.Value annotation can be found in the spring-beans jar. Adding the following dependency to your pom should fix the problem:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>${spring.version}</version>
</dependency>

Update:

I believe the problem is simply a classpath issue. The dependency org.springframework:spring:2.5.6, also has the package org.springframework.beans.factory.annotation, but without the Value annotation class. My presumption is that the eclipse classpath is mucked up. If you update the classpath (i.e. mvn eclipse:eclipse) your problem should be resolved.

Also do you need the old Spring dependency? If not then it is probably best to remove it.

As an aside, @BalajiV is absolutely correct you don't need an explicit dependency to spring-beans as it will be pulled in transiently via the spring-context dependency. Have to admit that personally if I have a compile time dependency to a class from a specific jar (in this case the Value class) then I always explicitly define a dependency to that jar in my pom instead of relying on another 3rd party dependency to provide it for me. I know it is unlikely to happen but if in a future release of spring-context they remove the dependency to spring-beans then my module would no longer work when I upgraded to the newer version.

Share:
10,406
SiddharthaRT
Author by

SiddharthaRT

In love with Python. Still advancing in small steps.

Updated on June 04, 2022

Comments

  • SiddharthaRT
    SiddharthaRT almost 2 years

    I'm creating a sample project as mentioned in MKyong's tutorial. I am using eclipse. When I created the project, I could not find any way to write a @Value annotation.

    I know it seems stupid, but I am not able to solve this problem at all. Can you please help?

    ...
    import org.springframework.beans.factory.annotation.*;
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.stereotype.Component;
    
    @Component ("newSession")
    public class Session implements DisposableBean, InitializingBean {
    
        @Value ("3232")
        private int id;
    ...
    

    pom.xml:

    ...
    <properties>
        <spring.version>3.0.5.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>2.5.6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- Spring 3 dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
    ....
    

    Apologies for the bad indentation (lots of copy-paste) and the silly question.