I'm trying to access a variable from another class via autowiring in Spring but I get a null pointer. The only way I can is by having it newly instantiated as static. Is there a way I can access it (the variable) in a non-static context - maybe something like autowiring?
Via static:
@Component
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "prototype")
public class ClassWhereVariableIsNeeded {
public static void setViews(Views aViews) {
views = aViews;
}
private static Views views;
.
.
.
private void method() {
views.thisVariable.....
}
I'd like it somewhat like this:
@Component
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "prototype")
public class ClassWhereVariableIsNeeded {
@Autowired
Views views;
.
.
.
private void method() {
views.thisVariable.....
}
Where the variable resides:
@Component
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "prototype")
public class Views extends ClassWhereVariableIsNeeded {
ThisVariable thisVariable;
.
.
.
To access any variable and/or method you must set this variable/method access modifier higher than private. private modifier restricts access to within the defining (or inner) class and nowhere else. You can provide public/default/package (access) method for other classes to use/access your private variable
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.