mardi 3 mars 2015

Creating unique instances in spring mvc 3 parent Controller

I'm developing a web app using the Spring 3 framework. So, I have the next hierachy level in my controllers package:



@Controller
public class BaseController {

@Autowired
@Qualifier("problemService")
protected ProblemService problemservice;

@Autowired
@Qualifier("codeService")
protected CodeService codeservice;

@Autowired
@Qualifier("userService")
protected UserService userservice;

@Autowired
@Qualifier("sessionRegistry")
protected SessionRegistry session;
.....
}


with children



@Controller
public class CodeController extends BaseController{
...
}

@Controller
public class ProblemController extends BaseController{
...
}


What i want is all these service of BaseController available to the children, but more like static resources, that is, all children share an unique instance of the father's services, and not a new instance of each as inheritance does. In that way, I tried this for each resource in BaseController:



@Autowired
@Qualifier("problemService")
protected static ProblemService problemservice;


but Spring throws a NullPointerException. So, is my idea at least correct? I mean, i do want to do what i said, but i dont know if it is a good approach at least.


Aucun commentaire:

Enregistrer un commentaire