I have a Spring controller defined like this with 2 request mappings, one using localDAO and the other using dependencyDAO. LocalDAO classes exist in my project and DependencyDAO classes are imported via maven dependency:
@RestController
@PreAuthorize("hasRole('USER')")
public class MyController
@Autowired
private localDAO LocalDAO; // dao classes exist in my project
@Autowired
private DependencyDAO dependencyDAO; // dao classes imported via maven dependency
...
@RequestMapping("/getUsingLocalDAO")
private String getUsingLocalDAO(
@JsonProperty("param") String param) {
localDAO.get(param) ... // <== this never null
}
@RequestMapping("/getUsingDependencyDAO")
private String getUsingDependencyDAO(
@JsonProperty("param") String param) {
dependencyDAO.get(param) ... // <== this always null
}
...
My dao beans are defined in another class:
@Configuration
public class DaoBeans {
@Bean
public LocalDAO localDAO() throws Exception {
return new LocalDAOImpl();
}
@Bean
public DependencyDAO dependencyDAO () throws Exception {
return new DependencyDAOImpl();
}
...
I am doing an $http.post from Angular like this:
$http.post('getUsingLocalDAO', $.param($scope.parameters), {
headers : {
"content-type" : "application/x-www-form-urlencoded"
}
}).success(function(data) {
...
}).error(function(data) {
...
$http.post('getUsingDependencyDAO', $.param($scope.parameters), {
headers : {
"content-type" : "application/x-www-form-urlencoded"
}
}).success(function(data) {
...
}).error(function(data) {
...
Both posts are identical except for the method they execute.
When stepping through the debugger I can see all the dao beans being created.
When I call getUsingLocalDAO everything works as expected. But, when I call getUsingDependencyDAO every @Autowired object is null.
I believe I am getting different instances of MyController. One managed by Spring and one not; or at least not instantiated properly.
I make these calls in succession. It doesn't matter what order they are in.
I tried injecting the servlet context via @Autowired to get the bean manually but it is always null in getUsingDependencyDAO as well.
I tried using application context aware and although I see the context setter being set in the debugger the context is always null in getUsingDependencyDAO.
If I wrap the two calls in a third request mapping like so everything works well (no null objects).
@RequestMapping("/getUsingBothDAO")
private String getUsingBothDAO(
@JsonProperty("param") String param) {
getLocalDAO(param);
getDependencyDAO(param);
...
}
I am using Spring-Boot 4.1.5. My project is 100% annotation driven and has no .xml configurations. The only difference between the two request mappings is that one uses a bean from a dependency and one does not.
I have been searching for an answer to this problem for 3 days and have not found anything close to what I am experiencing.
Can anyone shed some light as to what I am doing wrong? Any help would be greatly appreciated.
Thanks.
Aucun commentaire:
Enregistrer un commentaire