'How to manage scope session in spring boot?
I created a spring boot app with a controller layer, a service layer and a persistance layer. And i used spring security with JWT to provide the login process and to manage http sessions. I injected the service in the controller as follows :
@RestController
@ComponentScan(basePackages ={"com.example.service"})
@RequestMapping("/releve")
@CrossOrigin(origins = "http://192.168.1.13:4200")
public class ReleveController {
@Autowired
ReleveService releveService;
@Autowired
@Qualifier("authenticationManagerBean")
AuthenticationManager authenticationManager;
@Autowired
PasswordEncoder encoder;
@PreAuthorize("hasRole('ADMIN')")
@GetMapping(value="/listNiveaux")
public List<Niveau> listNiveaux() {
return releveService.listNiveaux();
}
@PreAuthorize("hasRole('ADMIN')")
@PostMapping(value="/modifNiveaux",consumes = "application/json")
public void modifNiveaux(@RequestBody Niveau niveau) {
releveService.modifNiveaux(niveau);
}
@PreAuthorize("hasRole('ADMIN')")
@PostMapping(value="/delNiveaux",consumes = "application/json")
public void delNiveaux(@RequestBody Niveau niveau) {
releveService.delNiveaux(niveau);
}
@PreAuthorize("hasRole('ADMIN')")
@PostMapping(value="/sauvegardeNiveau")
public void sauvegardeNiveau() {
releveService.sauvegardeNiveau();
}
}
Here are the annotations that i used in my service :
@Service(value = "ReleveService")
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
@EnableJpaRepositories(basePackages="com.example.releve.repository", entityManagerFactoryRef="entityManagerFactory")
public class ReleveServiceImpl implements ReleveService {
...
}
The problem is that despite the fact that the scope is session, the service ( or the bean ) is reinstanciated in every REST request : it does not get destroyed however, i used @PostConstruct and @PreDestroy to verify that it does not have the same behavior as the request scope. How do i solve this ?
PS : i use Angular for the front-end app and there is another controller class for the login web service.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
