Idea uses MAVEN to create a Web project, add Spring Framework support for a web app, create a bean to get

Source: Internet
Author: User
Tags tomcat server

1 Environment Release Notes

jdk:1.8

maven:3.5

Idea: Professional Edition 2017.2

2 Environment Readiness 2.1 MAVEN installation and its configuration 2.2 tomcat installation and its configuration

3 Step 3.1 Create a MAVEN project based on a template

WebApp, MAVEN, project, file--New

Tip 01: When creating a Web project from a template, select Maven-archetype-webapp

3.2 Directory Structure adjustment

The directory structure after the project was successfully created is as follows:

Jump Pit 01: The newly created project does not have a Java folder for the source files, there is no test folder for testing files, and there is no Resources folder to store the resource files.

Jump Pit 01: In the main directory of new Java, resources two folders, respectively, for storing source files and resource files; In the sibling directory of Main, create a new test directory to hold the testing folder

Tip 01: Although we have created the relevant folder, idea does not know that the Java folder is used to store the source files, test is used to store the testing files, resources are used to store the resource files; we have to manually configure:

FILE--Project structure-modules

After Setup, the entire directory structure is as follows:

3.3 Configuring Tomcat 3.3.1 to open the Startup configuration page

3.3.2 Adding a Tomcat boot entry

3.3.3 Configuring Tomcat Basic information

3.3.4 Adding a Web module

Tip # 01: Add a Web module to your project, file--project structure, module

Jump Pit 01: When you create a project with idea, a WebApp folder is created in the main directory, where the content is the content that needs to be deployed to the Tomcat container, but when we add a Web module to the project, a Web folder is automatically generated at the root of the project " It is recommended to delete this Web folder ", the role of this folder is the same as the WebApp folder under the main directory, And when you add a Web module, you are automatically looking for the Web. xml file under the newly created website, change the Web. XML to the webapp below, and change the source file folder for WebApp to the following:

3.3.5 adding artifacts

Tip # 01: Add a Web App that comes from modules "is actually from the Web project we created"

3.3.6 Configuring the Publishing page

Configure artifacts as a web app to add to the deployment in the Tomcat configuration

3.3.7 Configuration Development Hot Deployment

is to modify the front and back of the code without restarting Tomcat,idea will automatically "modify the background automatically restart the Tomcat server, modify the foreground when the browser will be able to refresh"

3.3.8 Start test

You can start Tomcat directly from idea.

Tip 01: When the app starts successfully, it automatically accesses the index.jsp page inside the WebApp

4 Adding framework Support

We create a shelf of web application knowledge, but idea supports the automatic addition of frames, so you don't need to manually add dependent configurations for the relevant framework in Pom.xml

Right-click Project, add Framework Stupport

Tip 01: This blog post mainly adds support for the spring framework

Tip 02: Click OK to automatically download the Spring Framework's dependency package to the project "PS: Download dependencies directly into the project Lib directory", the whole process takes a little time

Jump Pit 01: If the download depends on the network because of failure, this time will need to re-add the framework; but there are no spring-related options.

Pits 01: This is required to go into the modules configuration in the project structure, remove the spring-related modules, and re-add the framework

Tip 03: After adding Spring framework support, the relevant configuration files are automatically generated under the WebApp folder, and the configuration files are monitored in the web. XML in WebApp

5 Bean related

For more information on beans, please refer to "Proficient in spring4.x enterprise application development"

5.1 Configuring bean 5.1.1 Preparation

Introducing Lombok dependencies in the Pom.xml file

Creating student classes and teacher classes

 Packagedomain;ImportLombok. Data;/** * @authorWang Yangji * @create 2018-08-10 20:43 * @desc Students*/@Data Public classStudent {PrivateString ID; PrivateString name; PrivateInteger age; PrivateString address;  Public voidinfo () {System.out.println ("I am a student, and I am learning to control the inversion of knowledge. "); }}
Student.java
 Packagedomain;/** * @authorWang Yangji * @create 2018-08-10 20:45 * @desc Teacher Reality **/ Public classTeacher {PrivateString ID; PrivateString name; PrivateInteger age; PrivateString address;  Public voidinfo () {System.out.println ("I am a teacher, I am teaching the IOC relevant knowledge points." "); }}
Teacher.java5.1.2 Using XML configuration

Tip 01: You need to put the configuration files under the Resources folder "the configuration files generated by adding Spring Framework support are located under WebApp, and you need to change the Web. XML configuration file after the move.?? Pending modification "

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"        xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"       xsi:schemalocation= "http// Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">     Class= "domain." Student "></bean></beans>
View Code5.1.3 Using class annotations configuration
 PackageCore.config;Importdomain. Teacher;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;/** * @authorWang Yangji * @create 2018-08-10 20:59 * @desc Bean Configuration class **/@Configuration Public classBeans {@Bean (value= "Teacher")     PublicTeacher Buildteacher () {return NewTeacher (); }}
Beans.java5.2 Get Bean 5.2.1 using beanfactory get
    /*** using beanfactory to get beans *@throwsIOException*/@Test Public voidTEST01 ()throwsIOException {System.out.println ("Hello boy."); Resourcepatternresolver Resolver=NewPathmatchingresourcepatternresolver (); Resource Resource= Resolver.getresource ("Classpath:applicationContext.xml");        System.out.println (Resource.geturl ()); Defaultlistablebeanfactory Factory=Newdefaultlistablebeanfactory (); Xmlbeandefinitionreader Reader=NewXmlbeandefinitionreader (Factory);        Reader.loadbeandefinitions (Resource); System.out.println ("Initialization beanfactory Complete"); Student Student= Factory.getbean ("Student", student.class); System.out.println ("Student Bean gets Success");    Student.info (); }
View Code5.2.2. Using ApplicationContext to get

Tip 01: Using ApplicationContext to get XML configuration beans and configuration class beans requires different implementation classes

    /*** Using ApplicationContext to get beans*/@Test Public voidtest02 () {ApplicationContext ApplicationContext=NewClasspathxmlapplicationcontext ("Classpath:applicationContext.xml"); Student Student= Applicationcontext.getbean ("Student", student.class);    Student.info (); }    /*** Using ApplicationContext to get the Bean configuration class configuration*/@Test Public voidtest03 () {ApplicationContext ApplicationContext=NewAnnotationconfigapplicationcontext (Beans.class); Teacher Teacher= Applicationcontext.getbean ("Teacher", teacher.class);    Teacher.info (); }
View Code5.2.3 Get Bean Code Rollup
 Packagedomain;ImportCore.config.Beans;ImportOrg.junit.Before;Importorg.junit.Test;Importorg.springframework.beans.factory.support.DefaultListableBeanFactory;ImportOrg.springframework.beans.factory.xml.XmlBeanDefinitionReader;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.annotation.AnnotationConfigApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportOrg.springframework.core.io.Resource;ImportOrg.springframework.core.io.support.PathMatchingResourcePatternResolver;ImportOrg.springframework.core.io.support.ResourcePatternResolver;Importjava.io.IOException;/** * @authorWang Yangji * @create 2018-08-10 20:47 * @desc Test class **/ Public classTestdemo {@Before Public voidinit () {System.out.println ("Initialize Method"); }    /*** using beanfactory to get beans *@throwsIOException*/@Test Public voidTEST01 ()throwsIOException {System.out.println ("Hello boy."); Resourcepatternresolver Resolver=NewPathmatchingresourcepatternresolver (); Resource Resource= Resolver.getresource ("Classpath:applicationContext.xml");        System.out.println (Resource.geturl ()); Defaultlistablebeanfactory Factory=Newdefaultlistablebeanfactory (); Xmlbeandefinitionreader Reader=NewXmlbeandefinitionreader (Factory);        Reader.loadbeandefinitions (Resource); System.out.println ("Initialization beanfactory Complete"); Student Student= Factory.getbean ("Student", student.class); System.out.println ("Student Bean gets Success");    Student.info (); }    /*** Using ApplicationContext to get beans*/@Test Public voidtest02 () {ApplicationContext ApplicationContext=NewClasspathxmlapplicationcontext ("Classpath:applicationContext.xml"); Student Student= Applicationcontext.getbean ("Student", student.class);    Student.info (); }    /*** Using ApplicationContext to get the Bean configuration class configuration*/@Test Public voidtest03 () {ApplicationContext ApplicationContext=NewAnnotationconfigapplicationcontext (Beans.class); Teacher Teacher= Applicationcontext.getbean ("Teacher", teacher.class);    Teacher.info (); }}
View Code

    

6 This blog post reference source code

Click to download source code

7 Reference Blog

Project creation

Add Frame Dependencies

      

    

Idea uses MAVEN to create a Web project, add Spring Framework support for a web app, create a bean to get

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.