我是春季靴的新手。
我创建了如下的Web应用程序。
SampleApplication.java

@SpringBootApplication
public class SampleApplication implements CommandLineRunner {

    @Autowired
    DataSource dataSource;

    @Autowired
    private UserRepository userRepository;

    Logger logger = LoggerFactory.getLogger(SampleApplication.class);


    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }

    @Transactional(readOnly = true)
    @Override
    public void run(String... args) throws Exception {
        UserLoginController userLoginController = new UserLoginController();

System.out.println("DATASOURCE = " + dataSource);
        logger.debug("\nUser: "+userRepository.findByName("user").getName());
        logger.debug(userLoginController.getUserInfo("user","pword"));
    }
}


UserRepository.java

@EnableJpaRepositories
@Repository
public interface UserRepository extends CrudRepository<User,Long> {

    User findByName(String name);

}


User.java

@Entity
@Table(name="USER")
public class User
  implements Serializable
{
  private static final long serialVersionUID = 1L;
  @Id
  @SequenceGenerator(name="USER_ID_GENERATOR", sequenceName="USER_SEQ")
  @GeneratedValue(strategy=GenerationType.AUTO, generator="USER_ID_GENERATOR")
  private Long id;
  @Column(name="NAME")
  private String name;
  @Column(name="PASSWORD")
  private Long password;
public Long getId()
  {
    return this.id;
  }

  public void setId(Long id)
  {
    this.id = id;
  }
 public String getName()
  {
    return this.name;
  }

  public void setName(String name)
  {
    this.name = name;
  }

  public String getPassword()
  {
    return this.password;
  }

  public void setPassword(String password)
  {
    this.password = password;
  }


UserLoginController.java

@Controller
public class UserLoginController {
    Logger logger = LoggerFactory.getLogger(UserLoginController.class);

    @RequestMapping(value = "/userLogin**", method = RequestMethod.POST)
    public @ResponseBody String getUserInfo(@RequestParam("mobile") String uName, @RequestParam("pin") String pWord){
        logger.debug("UserLoginController | uName: " + uName + " pWord: " + pWord);

        UserLoginService userLoginService = new UserLoginService();

        String user = null;

        try {
            logger.debug("UserLoginController | Validate user. uName: " + uName + " pWord: " + pWord);
            user = userLoginService.validateSysUser(uName, pWord);
        } catch (Exception e) {
            logger.debug("UserLoginController | Validate user Exception: " + e.getMessage()+", "+e.getStackTrace());
        }

        if (user != null) {
            logger.debug("UserLoginController | Returning aircash user");
            return user;
        } else {
            logger.debug("UserLoginController | Returning NULL");
            return null;
        }

    }
}


UserLoginService .java

public class UserLoginService {
    Logger logger = LoggerFactory.getLogger(UserLoginService.class);

    @Autowired
    DataSource dataSource;

    @Autowired
    private UserRepository userRepository;

    public String validateSysUser(String uName, String pWord) throws Exception{
        logger.debug("UserLoginService | uName: "+uName+" pWord: "+pWord);

        User user = new User();

        String auser = userRepository.findByName(uName).getName();


        if ((auser != null) && (validatePword(user.getPassword(),pWord)){
            logger.debug("UserLoginService | User found: "+user.getName());
            return auser;
        }else {
            logger.debug("UserLoginService | User not found");
            return null;
        }
    }

    public static boolean validatePword(String pWord,String pWord2){
        return BCrypt.checkpw(pWord2,pWord);
    }
}


问题是,当我运行应用程序时,这部分给了我预期的响应

logger.debug("\nUser: "+userRepository.findByName("user").getContactNo());


但是,下一行引发异常。我也尝试过使用邮递员给控制器打电话。但同样的例外。

logger.debug(userLoginController.getUserInfo("user","pword"));


从UserLoginService.java类引发该异常。异常消息显示为null

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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>robicash</groupId>
    <artifactId>distributorcommision</artifactId>
    <version>1.0</version>
    <name>distributorcommission</name>
    <description>commision for distributors</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-jdbc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.mindrot</groupId>
            <artifactId>jbcrypt</artifactId>
            <version>0.3m</version>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>com.oracle</groupId>-->
            <!--<artifactId>ojdbc8</artifactId>-->
            <!--<version>11.2.0.4</version>-->
        <!--</dependency>-->
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>2.6.0</version>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>12.1.0</version>
        </dependency>
    </dependencies>

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


我不明白为什么同一存储库在调用时会给出两个结果。

你能告诉我如何使它工作吗?

最佳答案

您不能实例化UserLoginService userLoginService = new UserLoginService();

您必须将其注入控制器:

@Autowired
UserLoginService userLoginService;


如果仅在Spring实例化然后注入时,Spring只会管理您的服务。

是的,凯斯部分正确。您必须在UserLoginService中添加@Service或@Component批注:

@Service
public class UserLoginService {

10-08 01:33