background
When making a tool class, the NullPointerException exception is reported, and after debug it is found that the injected service is null. The code is as follows:
Package com.gwall.utils;
public class Stringreplaceutil {
@Resource
private static systrepository systrepository;
public static string replace (String oldstring) {
Sqlbuilder builder = new Wherebuilder ();
list<syst> systlist = Systrepository.findallbywhere (builder);
Return "";
}
}
resolution Process
Preliminary analysis (static variables)
Because the injected property is static, the initial assertion is that the bean is not allowed to be injected into a static variable. Try:
Scenario One: Setter Injection (recommended)
Package com.gwall.utils;
public class Stringreplaceutil {
private static systrepository systrepository;
Public Systrepository getsystrepository () {
return systrepository;
}
Spring injection cannot be injected into the static property, so it is injected with a setter. Add service annotations to the service to properly inject
@Resource public
void Setsystrepository (Systrepository systrepository) {
Stringreplaceutil.systrepository = systrepository;
}
public static string replace (String oldstring) {
Sqlbuilder builder = new Wherebuilder ();
list<syst> systlist = Systrepository.findallbywhere (builder);
Return "";
}
}
Scenario Two: Define a variable injection
Find the solution online, you can refer to public
class Test {
@Resource
private systrepository systrepository;
private static systrepository staticsystrepository;
@PostConstruct
void init () {
staticsystrepository = systrepository;
}
}
Results:
Still error second step analysis (Spring scan)
Spring is the management of beans through component scanning and automated assembly. Because the scan scope needs to be configured for component scanning, determine whether the class is within the scan range. Try:
Transfer classes to service scan locations based on configuration
Package Com.gwall.service.impl;
public class Stringreplaceutil {
private static systrepository systrepository;
Public Systrepository getsystrepository () {
return systrepository;
}
Spring injection cannot be injected into the static property, so it is injected with a setter. Add service annotations to the service to properly inject
@Resource public
void Setsystrepository (Systrepository systrepository) {
stringreplaceutil.systrepository = systrepository;
}
public static string replace (String oldstring) {
Sqlbuilder builder = new Wherebuilder ();
list<syst> systlist = Systrepository.findallbywhere (builder);
Return "";
}
}
Results:
Still error Third step analysis (Spring component Declaration)
Class is to be discovered and managed as a bean by spring and should first be declared as a component class
Package Com.gwall.service.impl;
@Service public
class Stringreplaceutil {
private static systrepository systrepository;
Public Systrepository getsystrepository () {
return systrepository;
}
Spring injection cannot be injected into the static property, so it is injected with a setter. Add service annotations to the service to properly inject
@Resource public
void Setsystrepository (Systrepository systrepository) {
stringreplaceutil.systrepository = systrepository;
}
public static string replace (String oldstring) {
Sqlbuilder builder = new Wherebuilder ();
list<syst> systlist = Systrepository.findallbywhere (builder);
Return "";
}
}
Try:
Add the corresponding component declaration annotations (@Component @controller@service@repository) results:
Success Summary whether add Component declaration annotations (@Component @controller@service@repository) are injected into the static variable within the scan range