May 2015
Intermediate to advanced
234 pages
4h 18m
English
It's possible to get a bean directly from Spring instead of using dependency injection by making Spring's ApplicationContext, which contains all the beans, a dependency of your class. In this recipe, we'll inject an existing bean into a controller class.
We will use the code from the Defining a bean implicitly with @Component recipe, where we defined a UserService bean.
Here are the steps to get and use a bean directly:
ApplicationContext field annotated with @Autowired:@Autowired private ApplicationContext applicationContext;
ApplicationContext object and its getBean() method to retrieve the UserService bean:UserService userService = (UserService)applicationContext. ...
Read now
Unlock full access