java spring Boot application not working bean错误

xjreopfe  于 2023-06-04  发布在  Java
关注(0)|答案(1)|浏览(133)

我是Sping Boot 的新手,在编写登录验证API时出现以下错误:
找不到com.Productwebsite.controller.UserController中的字段userservice需要类型为“com.Productwebsite.service.UserService”的Bean。
进样点具有以下注解:- @org.springframework.beans.factory.annotation.Autowired(required=true)
控制器类:

package com.Productwebsite.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.Productwebsite.service.UserService;

@RestController
public class UserController {
    
    @Autowired
    private UserService userservice;
    
    @GetMapping("user/{username}/{password}")
    public int UserLogin(@PathVariable("username")String username1, @PathVariable("password")String password1) {
        int flag = userservice.loginValidation(username1, password1);
        if (flag == 0) {
            return 0;
        }
        return flag;
    }
}

型号类别:

package com.Productwebsite.model;

public class users {
    String username;
    String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    @Override
    public String toString() {
        return "users [username=" + username + ", password=" + password + "]";
    }

    public users(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }

    public users() {
        super();
        // TODO Auto-generated constructor stub
    }
}

服务接口:

package com.Productwebsite.service;

import org.springframework.stereotype.Repository;

@Repository
public interface UserService {
    
    public int loginValidation(String username,String password);
}

服务实现类:

package com.Productwebsite.serviceImp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.stereotype.Service;

import com.Productwebsite.Dbutil.dbutil;
import com.Productwebsite.service.UserService;

@Service
public class ServiceImp implements UserService {
    Connection connection;
    int flag = 0;

    public ServiceImp() throws SQLException {
        connection = dbutil.getConnection();
    }

    @Override
    public int loginValidation(String username, String password) {
        try {
            PreparedStatement statement 
 = connection.prepareStatement("SELECT * FROM users WHERE username='"+username+"'"); 
            ResultSet rs=statement.executeQuery();
            
            while(rs.next()) {
                if (rs.getString(1).equals(username)&&rs.getString(2).equals(password)) {
                    flag=1;
                }
                else {
                    System.out.println("Invalid username/password");
                }
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return flag;
    }
}
0mkxixxg

0mkxixxg1#

@ComponentScan添加到com.Productwebsite并重新启动应用程序。

@ComponentScan(basePackages = "com.Productwebsite")
@SpringBootApplication
public class YourMainClass{
  ...
}

如果上述更改不起作用,请尝试以下操作:
通过添加bean名称更改注解:

@Repository("userservice")

相关问题