[JAVA] I want to use @Autowired in Servlet

I think that in principle, it should be a "single language / single framework" when building a web application, but in reality there are cases where that is not allowed. In particular, I hear that if you are involved in the development of business systems, you may be forced to develop something like patchwork (bloody tears).

As an extension of that, there was a case where a class created by Spring was used in Servlet, that is, @Autowired had to be done in __Servlet. I thought it would be easy, but I was quite addicted to it, so I will show you a sample as a memo.

@WebServlet("/hoge")
public class HogeServlet extends HttpServlet {
	
    @Autowired private HogeService hogeService;
    
	@Override
	protected void init(ServletConfig config) throws ServletException {
		super.init(config);
		SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
	}
    
    @Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
		hogeService.doSomething();
	}
}

The principle of Spring is to "leave the creation and disappearance of various beans to Spring", and @Autowired can be said to be a typical example. Conversely, in order to use @Autowried in a Servlet outside __Spring, it is necessary to incorporate that Servlet into the Spring life cycle, specifically the overridden ʻinit` method. The setting is done in. __

Recommended Posts

I want to use @Autowired in Servlet
I want to use Combine in UIKit as well.
I want to use Clojure's convenient functions in Kotlin
I want to use fish shell in Laradock too! !!
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
I want to use a little icon in Rails
I want to use FormObject well
I want to target static fields to @Autowired
I want to send an email in Java.
I want to use java8 forEach with index
I want to pass APP_HOME to logback in Gradle
rsync4j --I want to touch rsync in Java.
[Xcode] I want to manage images in folders
I want to be eventually even in kotlin
I want to get the value in Ruby
[Java Spring MVC] I want to use DI in my own class
I want to do something like "cls" in Java
I want to use NetBeans on Mac → I can use it!
I want to embed any TraceId in the log
I want to convert characters ...
I want to define a function in Rails Console
[Android Studio] I want to use Maven library on Android
I want to stop snake case in table definition
I want to click a GoogleMap pin in RSpec
I want to use PowerMock in a class that combines parameterized tests and ordinary tests
[Beginner] I want to modify the migration file-How to use rollback-
I want to find a relative path in a situation using Path
I want to set the conditions to be displayed in collection_check_boxes
I want to perform high-speed prime factorization in Ruby (ABC177E)
I want to use the Java 8 DateTime API slowly (now)
I want to use the sanitize method other than View.
Reasons to use Servlet and JSP separately in Java development
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to create a Parquet file even in Ruby
I want to transition to the same screen in the saved state
I want to use FireBase to display a timeline like Twitter
I want to simplify the conditional if-else statement in Java
Swift: I want to chain arrays
How to use Lombok in Spring
How to use scope (JSP & Servlet)
I want to convert InputStream to String
I want to docker-compose up Next.js!
How to use InjectorHolder in OpenAM
How to use classes in Java?
I want to use swipeback on a screen that uses XLPagerTabStrip
[Ruby] I want to put an array in a variable. I want to convert to an array
I tried setting Java beginners to use shortcut keys in eclipse
I want to display the images under assets/images in the production environment
I want to add devise in Rails, but I can't bundle install
I want to remove the top margin in Grouped UITableView (swift)
[Java] I want to perform distinct with the key in the object
I want to change the value of Attribute in Selenium of Ruby
[Android] I want to get the listener from the button in ListView
I want to develop a web application!
Multilingual Locale in Java How to use Locale
I want to write a nice build.gradle
I want to eliminate duplicate error messages
How to use custom helpers in rails
How to use named volume in docker-compose.yml
I want to make an ios.android app
I want to display background-ground-image on heroku.