【问题标题】:I am trying to get values from the function but failing to get. I tried adding componentscan but I failed我试图从函数中获取值但未能获取。我尝试添加组件扫描,但我失败了
【发布时间】:2017-11-20 12:17:26
【问题描述】:

我试图从函数中获取值但失败了。我尝试添加组件扫描,但失败了。

主要方法

package org.vik.springstarter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@SpringBootApplication
public class MyMain {
     public static void main(String[] args) {
        SpringApplication.run(MyMain.class, args);
    }
}

用户类

package org.vik.data;

public class UserData {
    private Integer Id;
    private String name;
    private String address;

    public UserData(Integer Id,String name,String address){
        this.Id = Id;
        this.name = name;
        this.address = address;
    }
    public Integer getId() {
        return Id;
    }
    public void setId(Integer id) {
        Id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

用户服务

package org.vik.data;
import java.util.Arrays;
import java.util.List;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Service
@Component
public class UserService {

    private List<UserData> userlist = Arrays.asList(
        new UserData(1,"Vik","sdfdsf"),
        new UserData(1,"Abani","sdfdsf"),
        new UserData(1,"Abrar","sdfdsf")
    );

    public List<UserData> getAlluser(){
        return userlist;
    }
 }

用户控制器

package org.vik.data;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    private UserService userservice;
    @RequestMapping("/hello")
    public List<UserData> Hello(){
        return userservice.getAlluser();
    }
    @RequestMapping("/vik")
    public String hi(){
        return "hi";
    }
}

***************************应用程序启动失败


说明:

org.vik.springstarter.controller.HelloController 中的字段 userservice 需要一个 'org.vik.data.UserService' 类型的 bean,它不能 找到了。

行动:

考虑在你的 配置。

【问题讨论】:

    标签: java spring spring-boot spring-boot-maven-plugin


    【解决方案1】:

    您的 Application.class 位于不同的包结构中:

    包 org.vik.springstarter;

    只需将您的 Application.class 移动到您的 Service 的相同结构中:

    org.vik

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2019-06-24
      • 1970-01-01
      • 2022-06-16
      • 2017-02-06
      • 1970-01-01
      • 2011-08-25
      相关资源
      最近更新 更多