Spring 尽管在所有地方都使用了必填字段,但@Autowired 服务奇怪地为空
Spring @Autowired service is strangely null inspite of using required fields all over
我正在使用 pojo 来处理表单请求,并且正在尝试使用 Autowired 访问服务。但无论我尝试更改什么,它都会返回 null。我会发布我所有的代码,请看一下。
类别添加表格:
在此页面中,我尝试使用 categoryService,但它似乎是空的(null),当我尝试使用该服务中的任何函数时,我得到 nullpointerexception。我没有尝试在任何地方初始化自动装配 class。
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Component
public class CategoryAddForm {
private Long id;
private String title;
private String description;
private String language;
private String image_url;
@Autowired
CategoryService categoryService;
public Category save() {
Category category;
if(this.id==null) {
if(this.language==null || this.language.trim().equals("en")) {
category = Category.builder()
.title_en(this.title)
.description_en(this.description)
.image_url(this.image_url)
.en_available(true)
.count(0l).build();
System.out.println("New for English: \n id: "+this.id+" \n title:"+this.title+"\n description: "+this.description+"\n language: "+this.language);
System.out.println(categoryService);
// categoryService.save(category);
}
else {
category = Category.builder()
.title_np(this.title)
.description_np(this.description)
.np_available(true)
.en_available(false)
.count(0l).build();
System.out.println("New for Nepali: \n id: "+this.id+" \n title:"+this.title+"\n description: "+this.description+"\n language: "+this.language);
System.out.println(categoryService);
// categoryService.save(category);
}
}
else {
if(this.language==null || this.language.trim().equals("en")) {
category = Category.builder()
.id(this.id)
.title_en(this.title)
.description_en(this.description)
.en_available(true)
.image_url(this.image_url).build();
}
else {
category = Category.builder()
.id(this.id)
.title_np(this.title)
.description_np(this.description)
.np_available(true)
.image_url(this.image_url).build();
}
}
System.out.println(category);
return categoryService.save(category);
}
}
表单页面:
此页面将 categoryForm 对象发送到控制器中的 /save,它会调用 CategoryAddForm 中的保存功能
<form th:action="@{'/admin/category/save'}" method="post" th:object="${categoryForm}">
<div class="form-group my-1 p-2 row">
<label for="categoryTitle">Title</label>
<input type="title" class="form-control" name="title" id="categoryTitle" th:field="*{title}">
</div>
<div class="form-group my-1 p-2 row">
<label for="categoryDescription">Description</label>
<textarea rows="5" type="description" class="form-control" name="description" id="categoryDescription" th:field="*{description}">
</textarea>
</div>
<div class="form-check my-1 p-2 row">
<label for="languages">Select Language</label>
<div id="languages" class="row ml-2">
<div class="col-2" >
<input class="form-check-input" type="radio" value="en" th:field="*{language}" id="englishLanguage" th:selected="selected">
<label class="form-check-label" for="englishLanguage">
English
</label>
</div>
<div class="col-2" >
<input class="form-check-input" type="radio" value="ne" th:field="*{language}" id="nepaliLanguage">
<label class="form-check-label" for="nepaliLanguage">
Nepali
</label>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary m-2">Submit</button>
</form>
类别服务实施:
仅供参考和证明我没有做错什么。
@Service
public class CategoryServiceImpl implements CategoryService {
@Autowired
private CategoryRepository repository;
@Override
public Category save(Category document) {
return repository.save(document);
}
@Override
public Category getById(Long id) {
return repository.getOne(id);
}
@Override
public void delete(Category document) {
repository.delete(document);
}
@Override
public void deleteById(Long id) {
repository.deleteById(id);
}
@Override
public List<Category> getAllCategories() {
return repository.findAll();
}
}
在 CategoryAdminController 中保存路由:
此函数从表单中获取 categoryAddForm 并调用 categoryAddForm 中的 save() 函数。
@PostMapping(value = "/save")
public ModelAndView submitCategory(@ModelAttribute("categoryForm") CategoryAddForm categoryForm, BindingResult result){
categoryForm.save();
if(result.hasErrors()){
return new ModelAndView("redirect:/admin/category/add?error");
}
return new ModelAndView("redirect:/admin/category/add");
}
问题是您每次进行请求时都在创建一个新的 CategoryAddForm,并且您没有让 spring 管理您的 bean。如果您正在创建一个新的 CategorAddForm 实例,spring 将不知道他需要将您的服务注入其中。我想你可以尝试通过构造函数注入。
我正在使用 pojo 来处理表单请求,并且正在尝试使用 Autowired 访问服务。但无论我尝试更改什么,它都会返回 null。我会发布我所有的代码,请看一下。
类别添加表格:
在此页面中,我尝试使用 categoryService,但它似乎是空的(null),当我尝试使用该服务中的任何函数时,我得到 nullpointerexception。我没有尝试在任何地方初始化自动装配 class。
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Component
public class CategoryAddForm {
private Long id;
private String title;
private String description;
private String language;
private String image_url;
@Autowired
CategoryService categoryService;
public Category save() {
Category category;
if(this.id==null) {
if(this.language==null || this.language.trim().equals("en")) {
category = Category.builder()
.title_en(this.title)
.description_en(this.description)
.image_url(this.image_url)
.en_available(true)
.count(0l).build();
System.out.println("New for English: \n id: "+this.id+" \n title:"+this.title+"\n description: "+this.description+"\n language: "+this.language);
System.out.println(categoryService);
// categoryService.save(category);
}
else {
category = Category.builder()
.title_np(this.title)
.description_np(this.description)
.np_available(true)
.en_available(false)
.count(0l).build();
System.out.println("New for Nepali: \n id: "+this.id+" \n title:"+this.title+"\n description: "+this.description+"\n language: "+this.language);
System.out.println(categoryService);
// categoryService.save(category);
}
}
else {
if(this.language==null || this.language.trim().equals("en")) {
category = Category.builder()
.id(this.id)
.title_en(this.title)
.description_en(this.description)
.en_available(true)
.image_url(this.image_url).build();
}
else {
category = Category.builder()
.id(this.id)
.title_np(this.title)
.description_np(this.description)
.np_available(true)
.image_url(this.image_url).build();
}
}
System.out.println(category);
return categoryService.save(category);
}
}
表单页面:
此页面将 categoryForm 对象发送到控制器中的 /save,它会调用 CategoryAddForm 中的保存功能
<form th:action="@{'/admin/category/save'}" method="post" th:object="${categoryForm}">
<div class="form-group my-1 p-2 row">
<label for="categoryTitle">Title</label>
<input type="title" class="form-control" name="title" id="categoryTitle" th:field="*{title}">
</div>
<div class="form-group my-1 p-2 row">
<label for="categoryDescription">Description</label>
<textarea rows="5" type="description" class="form-control" name="description" id="categoryDescription" th:field="*{description}">
</textarea>
</div>
<div class="form-check my-1 p-2 row">
<label for="languages">Select Language</label>
<div id="languages" class="row ml-2">
<div class="col-2" >
<input class="form-check-input" type="radio" value="en" th:field="*{language}" id="englishLanguage" th:selected="selected">
<label class="form-check-label" for="englishLanguage">
English
</label>
</div>
<div class="col-2" >
<input class="form-check-input" type="radio" value="ne" th:field="*{language}" id="nepaliLanguage">
<label class="form-check-label" for="nepaliLanguage">
Nepali
</label>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary m-2">Submit</button>
</form>
类别服务实施: 仅供参考和证明我没有做错什么。
@Service
public class CategoryServiceImpl implements CategoryService {
@Autowired
private CategoryRepository repository;
@Override
public Category save(Category document) {
return repository.save(document);
}
@Override
public Category getById(Long id) {
return repository.getOne(id);
}
@Override
public void delete(Category document) {
repository.delete(document);
}
@Override
public void deleteById(Long id) {
repository.deleteById(id);
}
@Override
public List<Category> getAllCategories() {
return repository.findAll();
}
}
在 CategoryAdminController 中保存路由:
此函数从表单中获取 categoryAddForm 并调用 categoryAddForm 中的 save() 函数。
@PostMapping(value = "/save")
public ModelAndView submitCategory(@ModelAttribute("categoryForm") CategoryAddForm categoryForm, BindingResult result){
categoryForm.save();
if(result.hasErrors()){
return new ModelAndView("redirect:/admin/category/add?error");
}
return new ModelAndView("redirect:/admin/category/add");
}
问题是您每次进行请求时都在创建一个新的 CategoryAddForm,并且您没有让 spring 管理您的 bean。如果您正在创建一个新的 CategorAddForm 实例,spring 将不知道他需要将您的服务注入其中。我想你可以尝试通过构造函数注入。