简体   繁体   中英

@Autowired is not found in my project with spring-boot

Description of my problem

I am trying to use @Autowired in my @RestController but the @Autowired is not found in my project. I am following it Building REST services with Spring

  1. What did i forget to put in my pom.xml?

I have written it because i didn't see anything like it about @Autowired DI.

POM.XML

My pom.xml with spring-boot, my @Autowired is not found in my project.

What did i forget to put in my pom.xml?

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.kenny</groupId>
    <artifactId>gsbookmarks</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>

My RestController 那就是我的@RestController而找不到@Autowired!

No import to @Autowired, why? 我没有导入到@Autowired。为什么?

@Autowired is part of spring-beans-XXX.jar. spring-boot-starter will have spring-beans as trasnsitive dependancy. So spring-beans*.jar must be in your classpath. This seems to be some issue of IDE.

  1. Try restarting your IDE. If you are using Intellij, choose following option- File->Invalidate Cache/Restart.

  2. Try to build the project first, so that all jars are downloaded and available in classpath.

  3. Try to manually add spring-beans*.jar in classpath, if none of above works.

I am not aware about Spring Boot but have you tried below one for import org.springframework.beans.factory.annotation.Autowired;

<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>your release version</version>
</dependency>

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