[英]Error creating bean with name 'viewResolver' Spring
Entity实体
@Entity
public class Card {
@Id
private Long id;
private String full_name;
private String login;
private int balance;
public Card() {
}
public Card(String full_name, String login, int balance) {
this.full_name = full_name;
this.login = login;
this.balance = balance;
}
public Card(Long id, String full_name, String login, int balance) {
this.id = id;
this.full_name = full_name;
this.login = login;
this.balance = balance;
}
public String getFull_name() {
return full_name;
}
public String getLogin() {
return login;
}
public int getBalance() {
return balance;
}
@Override
public String toString() {
......... }
}
Repository存储库
@Repository
public interface CardRepository extends JpaRepository<Card, Long>{
@Query(value = "SELECT us.id, ...", nativeQuery = true)
List<Card> findAll();
}
Service服务
public interface CardService {
List<Card> findAll();
}
Impl实施
@Service
public class CardServiceImpl implements CardService {
@Autowired
private CardRepository repository;
@Override
public List<Card> findAll() {
List<ECard> list = new ArrayList<>();
return repository.findAll();
}
}
Controller Controller
@RestController
@RequestMapping("/card")
public class CardController {
@Autowired
private CardService cardService;
public @ResponseBody List<Card> getAllCards() {
return cardService.findAll();
}
@RequestMapping( value = "/card", method = RequestMethod.GET)
public List<Card> cardList() {
return cardService.findAll();
}
}
I built a service like this.我建立了这样的服务。 It works great.
它工作得很好。 But I would like to save the data it receives to an xlsx file.
但我想将它收到的数据保存到 xlsx 文件中。 This file should be saved when the application starts.
此文件应在应用程序启动时保存。 I wanted to add the Workbook library instead of the existing Controller class, or rather its contents.
我想添加工作簿库而不是现有的 Controller class,或者更确切地说是它的内容。
Something like that:像这样的东西:
public class SaveCards {
public static void saveCards() throws IOException {
CardServiceImpl cardService = new CardServiceImpl();
saveExcel(cardService.findAll(), "fileName.xlsx");
}
private static void saveExcel(List<Card> list, String fileName) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Cards");
sheet.setColumnWidth(0, 6000);
sheet.setColumnWidth(1, 4000);
Row header = sheet.createRow(0);
CellStyle headerStyle = workbook.createCellStyle();
headerStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
XSSFFont font = ((XSSFWorkbook) workbook).createFont();
font.setFontName("Arial");
font.setFontHeightInPoints((short) 16);
font.setBold(true);
headerStyle.setFont(font);
Cell headerCell = header.createCell(0);
headerCell.setCellValue("full_name");
headerCell.setCellStyle(headerStyle);
headerCell = header.createCell(1);
headerCell.setCellValue("login");
headerCell.setCellStyle(headerStyle);
headerCell = header.createCell(2);
headerCell.setCellValue("balance");
headerCell.setCellStyle(headerStyle);
CellStyle style = workbook.createCellStyle();
style.setWrapText(true);
int ix_row=2;
for (Card card : list) {
Row row = sheet.createRow(ix_row);
Cell cell = row.createCell(0);
cell.setCellValue(eCard.getFull_name());
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue(eCard.getLogin());
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue(eCard.getBalance());
cell.setCellStyle(style);
ix_row++;
}
FileOutputStream outputStream = new FileOutputStream(fileName);
workbook.write(outputStream);
workbook.close();
}
}
However, if I changed the Controller class to the SaveCards class, I got an error: Error creating bean with name 'viewResolver' Spring However, if I changed the Controller class to the SaveCards class, I got an error: Error creating bean with name 'viewResolver' Spring
Now, the following comes out: Started DemonewApplication in 11.253 seconds (JVM running for 11.882).现在,出现以下情况:在 11.253 秒内启动了 DemonewApplication(JVM 运行时间为 11.882)。 And that's all, no action takes place, the data received by the select is not saved to the xlsx file.
就是这样,没有任何动作发生,select 接收到的数据不会保存到 xlsx 文件中。 What am I doing wrong?
我究竟做错了什么?
I understand that the saveCards method in the SaveCards class has to be called somewhere, but I can't figure out where?我知道 SaveCards class 中的 saveCards 方法必须在某个地方调用,但我不知道在哪里?
You can extend org.springframework.boot.ApplicationRunner
to run your code at Spring Boot startup.您可以扩展
org.springframework.boot.ApplicationRunner
以在 Spring Boot 启动时运行您的代码。
@Component
class SaveCardsStartupRunner extends ApplicationRunner
{
@Override
public void run(ApplicationArguments args)
{
SaveCards.saveCards();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.