简体   繁体   English

SpringBoot:Spring Boot应用程序中的自动关联依赖项

[英]SpringBoot : Autowired dependency in spring boot application

I am trying to develop a springboot application from scratch. 我正在尝试从头开始开发springboot应用程序。 As i am new to springboot i am not able to add autowired annotation ... seems like i am missing some dependency in pom.xml 由于我是springboot的新手,所以我无法添加自动装配的注释...好像我在pom.xml中缺少某些依赖项

here are the scenario 这是场景

pom.xml pom.xml

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>springboot</groupId>
  <artifactId>firstprogram</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
    </parent>
  <name>firstprogram Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <dependencies>  
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.3.6.Final</version>
</dependency>

  </dependencies>
  <build>
    <finalName>firstprogram</finalName>
  </build>
</project>

if i am trying to add dependency with spring mvc. 如果我想用spring mvc添加依赖项。 it is giving warning like spring boot is already adding those dependency. 它发出警告,例如spring boot已经添加了那些依赖项。

Make sure your service is annotated with the class @Component or @Service, and then when you use the service in another class, add the tag @Autowired to the class. 确保使用类@Component或@Service注释您的服务,然后在另一个类中使用该服务时,将标记@Autowired添加到该类中。

Also maybe @ComponentScan 也可能@ComponentScan

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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