spring:无法调用服务,因为它为null

7dl7o3gd  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(470)

这个问题在这里已经有答案了

为什么我的spring@autowired字段为空(22个答案)
什么是nullpointerexception,如何修复它(12个答案)
三个月前关门了。
我想我和豆子泉有问题,我不知道怎么解决。这是我的密码:

package tse.poc.utils;

import java.time.LocalDateTime;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import lombok.extern.slf4j.Slf4j;
import tse.poc.dao.ProjectRepository;
import tse.poc.dao.TimeRepository;
import tse.poc.dao.UserRepository;
import tse.poc.domain.Project;
import tse.poc.domain.Time;
import tse.poc.domain.User;

@Configuration
@Slf4j
public class LoadDatabase {

    @Bean
    @Profile("!test")
    CommandLineRunner initDatabase(ProjectRepository projectRepository, TimeRepository timeRepository,
            UserRepository userRepository) {

        return args -> {
            initMainDatabase(userRepository, projectRepository, timeRepository);
            SummaryPDF spdf = new SummaryPDF();
            spdf.monthSummary(1L, "FEBRUARY");
            log.info("PDF has been initialized");
        };
    }
package tse.poc.utils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.pdf.PdfWriter;

import tse.poc.domain.User;
import tse.poc.service.UserService;

@Configuration
public class SummaryPDF {

    @Autowired
    private UserService userService;

    public void monthSummary(Long userId, String month) throws FileNotFoundException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream("iTextHelloWorld.pdf"));

        User user = this.userService.findUserOnId(userId);

        document.open();
        Font font = FontFactory.getFont(FontFactory.COURIER, 16, BaseColor.BLACK);

        Chunk chunk = new Chunk(user.getLastname().toUpperCase()+" "+user.getFirstname(), font);
        Chunk chunk2 = new Chunk(user.getRole(), font);
        Chunk chunk3 = new Chunk(month + " Summary", font);

        document.add(chunk);
        document.add(chunk2);
        document.add(chunk3);
        document.close();
    }

}
package tse.poc.service;

import java.util.List;

import org.springframework.stereotype.Service;

import tse.poc.domain.User;

public interface UserService {

    public List<User> findAllUsers();

    public User findUserOnId(Long userId);

    public User createUser(User user);

    public void deleteUser(User user);

    public User credentialsChecking(String email, String password);
}
package tse.poc.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tse.poc.dao.UserRepository;
import tse.poc.domain.User;
import tse.poc.service.UserService;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public List<User> findAllUsers() {

        return this.userRepository.findAll();
    }

    @Override
    public User findUserOnId(Long userId) {
        List<User> users = findAllUsers();

        for (User user : users) {
            if (user.getId() == userId) {
                return user;
            }
        }
        return null;
    }

    @Override
    public User createUser(User user) {

        return this.userRepository.save(user);
    }

    @Override
    public void deleteUser(User user) {
        this.userRepository.delete(user);
    }

    @Override
    public User credentialsChecking(String email, String password) {
        List<User> users = findAllUsers();

        for (User user : users) {
            if (user.getPassword() == password && user.getEmail() == email) {
                return user;
            }
        }
        return null;
    }

}

所以,我想我在我的类中使用了错误的注解,错误是:

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:807) ~[spring-boot-2.4.1.jar:2.4.1]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:788) ~[spring-boot-2.4.1.jar:2.4.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) ~[spring-boot-2.4.1.jar:2.4.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309) ~[spring-boot-2.4.1.jar:2.4.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298) ~[spring-boot-2.4.1.jar:2.4.1]
    at tse.poc.PocApplication.main(PocApplication.java:14) ~[classes/:na]
Caused by: java.lang.NullPointerException: Cannot invoke "tse.poc.service.UserService.findUserOnId(java.lang.Long)" because "this.userService" is null
    at tse.poc.utils.SummaryPDF.monthSummary(SummaryPDF.java:29) ~[classes/:na]
    at tse.poc.utils.LoadDatabase.lambda$0(LoadDatabase.java:29) ~[classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:804) ~[spring-boot-2.4.1.jar:2.4.1]
    ... 5 common frames omitted

你能解释一下哪个错误和如何解决吗?我已经在我的测试类中使用了userservice,但是我从来没有出现过这个错误,你能告诉我在summarypdf类中需要哪个注解吗?

ctrmrzij

ctrmrzij1#

UserService 不是一个 Bean . 添加 @Service 注解。它还需要实现服务。

相关问题