Annotation-based approach , no web.xml
Using web3.0
Environment build :
1. Create a new maven project , the default is web3.0, there is no web.xml file
Since there is no web.xml file , the pom.xml file will report an error .
2. Configure the maven plugin and ignore errors .
Directly in the pom.xml file alt + / will prompt :
Then configure it in maven-war-plugin :
No web.xml can be ignored .
You need to update maven-update at this point
Set the runtime environment for the project
At this point, you can also manually add the maven compilation plugin in pom.xml :
It should be noted that the jdk version obtained in the runtime environment must be greater than the jdk version at compile time , otherwise there will be problems .
Based on annotation development , a knowledge point that needs to be known :
When you put the project into the Tomcat container and start running , tomcat will look for a core class in the dependencies :
After Tomcat finds this file , it will read the contents of this file :
Then Tomcat will create this object through reflection :
onStartUp method
The onStartUp method of this class will add all subclasses of WebApplicationInitializer to the set collection of this class . It is used to hand over to spring to create objects ,
3. Create a JtWebAppInitializer class
inherit from an abstract class ;
We also need a class to proxy spring 's core configuration file for package scanning and to enable mvc annotations :
A class that implements this interface can be used as the core configuration file for spring :
For convenience , you can make your own class inherit a subclass of this interface ;
The configuration class needs to be added to the front-end controller class , which is equivalent to adding the configuration file to the front-end controller DispatherServlet ;
getServletConfigClasses for this class :
This method is responsible for loading Controllers, ViewResovlers, HandlerMapping
For the AppServletConfig configuration class , you can also add interceptors , and you can limit only @Controller to scan, not @Service and @Reporosity
Re-understand :
The role of the JtWebAppInitializer we wrote is equivalent to web.xml. This class is actually a container for initialization. Note that it is not DispatherServlet. This class knowledge initializes DispatherServlet .
Look at the innermost interface :
This interface is from spring .
See the explanation of this interface :
Translation : This interface is implemented in the environment of Servlet3.0+ , and the purpose is to configure in a programmatic way , which is exactly opposite to the implementation of traditional web.xml ( or combined with web.xml) .
The implementation classes of this interface will be automatically detected by SpringServletContainerInitializer , and these implementation classes will be automatically created by any Servlet3.0+ container .
Servlet 3.0 is designed to support code-based configuration of servlet containers using Spring 's SPI ,
This class will be loaded and initialized , and its methods will be implemented by any Servlet 3.0 - container at container startup .
getServletConfigClass() and getRootConfigclasses () of JtWebAppInitializer
==
Now you also need an AppRootConfig class , which is used for back-end configuration , creating DruidDataSOurce, etc. to connect resources , and scan the service annotations of com.jt
Configure the AppRootConfig class :
>>1. Configure DruidDataSource:
Note that under the web project , the classpath must be added to load the properties file
>>1.2 Test whether DruidDataSource is configured ;
Querying the data means that the DruidDataSource is configured ;
>>2.1 Configure SqlSessionFactory
2.2 Testing
>>3.1 Configure packet scanning of dao interface :
>>4.2 Write service layer : by interface
>>4.2 to test the service:
In this case, an error will be reported ;
The reason for this error is : AppRootConfig defaults to scan all annotations of all classes under com.jt , and creates objects based on this annotation .
At this time , @Controller also needs to be scanned , but this @Controller is scanned by mvc and needs to be created in the tomcat environment , but now we are testing under Junit , so an error will be reported , so when AppRootConfig is scanned , it should be Do not scan for @Controller annotations :
But if it is only configured like this , the @Service annotation will not be scanned , so the @Service annotation has to be added ;
4. Configure the Controller
The configuration of Produces is used to solve the problem of garbled responses