英文:
Spring @Autowired service is strangely null inspite of using required fields all over
问题
I am using a pojo for handling form request and I am trying to access service using Autowired. But it keeps returning null inspite of whatever I try changing. I will publish all my code, please take a look into it.
CategoryAddForm:
In this page, I am trying to use categoryService but it seems be empty(null) and when I try using any function inside the service, I get nullpointerexception. I have not tried to initialize the autowired class anywhere.
@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);
}
}
Form page:
This page sends categoryForm object to /save in controller which invokes the save function that is inside 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>
Category Service Implementation:
This is just for the reference and proof that I have not done anything wrong.
@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();
}
}
save route in CategoryAdminController:
This function is taking categoryAddForm from the form and calls save() function in categoryAddForm.
@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");
}
英文:
I am using a pojo for handling form request and I am trying to access service using Autowired. But it keeps returning null inspite of whatever I try changing. I will publish all my code, please take a look into it.
CategoryAddForm:
In this page, I am trying to use categoryService but it seems be empty(null) and when I try using any function inside the service, I get nullpointerexception. I have not tried to initialize the autowired class anywhere.
@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);
}
}
Form page:
This page sends categoryForm object to /save in controller which invokes the save function that is inside 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>
Category Service Implementation:
This is just for the reference and proof that I have not done anything wrong.
@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();
}
}
save route in CategoryAdminController:
This function is taking categoryAddForm from the form and calls save() function in categoryAddForm.
@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");
}
答案1
得分: 1
问题是每次进行请求时都在创建一个新的CategoryAddForm,而且你没有让Spring来管理你的Bean。如果你每次都创建一个新的CategoryAddForm实例,Spring就不会知道它需要将你的服务注入进去。我认为你可以尝试通过构造函数来进行注入。
英文:
The problem is that you are creating a new CategoryAddForm every time you make a reuqest and you are not letting spring manage your beans. If you are creating a new instance of CategorAddForm, spring won't know that he needs to inject your service into it. I think you can try to inject by constructor.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论