How to use tomcat with spring without web.xml?

I have a JAX-RS application deployed on Tomcat 7. I am deploying the application on Tomcat without the web.xml .

I want to use spring to input some dependencies in my project. I do not use an XML file to configure beans, instead I created a class and annotated it using the @Configuration annotation, which contains all the bean definitions. Now I'm trying to use this bean by annotating a field in a separate class using @Autowired , but this field is null .

How do I make spring work with this setting?

EDIT: I was able to initialize the spring application context by creating web.xml in WEB-INF / with the following contents.

 <web-app> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app> 

I checked the tomcat logs and verified that the bean was created by spring. But the problem is that when I try to use this bean, the field in the class is null.

+5
source share
4 answers

You can use Spring Boot , which helps you develop a web application as easy as developing a standalone application that you can โ€œjust runโ€.

According to the official Spring Boot website, you can create

  • Creating Standalone Spring Applications
  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
  • Provide self-confident start-up POMs to simplify Maven configuration.
  • Automatically configure Spring whenever possible
  • Provide out-of-the-box features like metrics, health checks, and external configuration.
  • Absolutely do not generate code or require XML configuration

You can find out about it in just 1 hour. I found this YouTube video very interesting. Getting Started with Spring Download .

+2
source

web.xml is used as a starting point when the tomcat container starts. If you want to get around this, you can write a simple Java program, and the main () function will simply call Spring to load the application context and specify the path of your application applicationContext.

More details: https://www.tutorialspoint.com/spring/spring_java_based_configuration.htm

0
source

If you want to use @Autowired in a class, it must first be a Bean. You can do this by annotating the class with @Component or using @Bean Annotation in Configuration.

 @Bean Yourclass yourclass(){ return new YourClass(); } 

Secondly, you will need to add the @ComponentScan annotation to your configuration.

 @ComponentScan("your.package") @Configuration 

Third, you must create an instance of ApplicationContext. For example, for example

 JavaConfigApplicationContext context = new JavaConfigApplicationContext(YourConfig.class); 

Since this is a Web application, it would probably be better to take a look at the WebApplicationInitializer , implement your own and initialize the context there.

0
source

What you are missing is a way to initialize the Spring Dispatcher Servlet servlet. You need to implement the WebApplicationInitializer interface, and in this class you pass all the Java configurations necessary for your application.

You must define AnnotationConfigWebApplicationContext where you can register your configuration class:

 context.register(MyConfig.class); 

or you can define base packages containing all of your configuration classes:

 context.setConfigLocation("com.example.app.config"); 

Full example:

  public class MyWebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation("com.example.app.config"); container.addListener(new ContextLoaderListener(context)); ServletRegistration.Dynamic dispatcher = container .addServlet("dispatcher", new DispatcherServlet(context)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } } 

Last, if you are using maven, remember to set the failOnmissingwebxml property to false in pom.xml .

You can find more information here.

0
source

Source: https://habr.com/ru/post/1273432/


All Articles