How Spring 3.1 Java Configuration Works

Just a general question when you define a Java-based configuration web application. Those. you have a class for: ApplicationContext and the WebApplicationInitializer class.

How Spring knows that it has to load beans, since there are no xml configuration files. How tomcat knows anything about webapp without web.xml

This is a newbie question. I appreciate it.:)

+4
source share
2 answers

See this blog post from the SpringSource blog , in the important part about web.xml there is an example, basically you point to JavaConfigWebApplicationContext instead of the standard XmlWebApplicationContext in the DispatcherServlet <init-param> :

 <web-app> <!-- Configure ContextLoaderListener to use JavaConfigWebApplicationContext instead of the default XmlWebApplicationContext --> <context-param> <param-name>contextClass</param-name> <param-value>org.springframework.config.java.context.JavaConfigWebApplicationContext</param-value> </context-param> <!-- Configuration locations must consist of one or more comma- or space-delimited fully-qualified @Configuration classes --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>example.RootApplicationConfig</param-value> </context-param> <!-- Bootstrap the root application context as usual using ContextLoaderListener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Declare a Spring MVC DispatcherServlet as usual --> <servlet> <servlet-name>dispatcher-servlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- Configure DispatcherServlet to use JavaConfigWebApplicationContext instead of the default XmlWebApplicationContext --> <init-param> <param-name>contextClass</param-name> <param-value>org.springframework.config.java.context.JavaConfigWebApplicationContext</param-value> </init-param> <!-- Again, config locations must consist of one or more comma- or space-delimited and fully-qualified @Configuration classes --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>example.web.WebBeansConfig</param-value> </init-param> </servlet> </web-app> 
+3
source

I have a VERY GOOD WAY to help you find out Spring MVC if you have Maven up and running.

IF SO: go to your command line (Cygwin), I use ...

  • mvn archetype: generate
  • He will ask for an "archetype number." For you ... type 16
  • Enter the group ID, which is the main package.
  • Enter the ID of the artifact that is your project name.
  • SNAP-SHOT --- just press enter the same with the version.
  • A package is the same as the name of your group. EX: com.spring
  • Confirm this by typing the letter 'y' and press enter.

Follow all the steps above after they are in your workspace directory. Thus, it is created there. You can do "mvn eclipse: eclipse" to load it into Eclipse or you can just import it. I prefer the old-fashioned import of an existing project.

Everything will be โ€œalreadyโ€ configured for you in terms of the entire configuration (based on Java) that suits you. It will also have all the Maven dependencies you need and in your pom.xml. You can add or take it if you want.

The fact is that you will already have a running project, and you can play with it from there. First I create all my projects like this, and erase what I donโ€™t need, and add what I do, and then from there.

Good luck !!!

Anywho ... add this to your web.xml. This will help you in your answer. Check it out below:

 <context-param> <param-name>contextClass</param-name> <param-value> org.springframework.web.context.support.AnnotationConfigWebApplicationContext </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 
+1
source

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


All Articles