1. 程式人生 > >IDEA中利用Maven創建Web項目、為Web應用添加Spring框架支持、bean的創建於獲取

IDEA中利用Maven創建Web項目、為Web應用添加Spring框架支持、bean的創建於獲取

println pack 下載源 環境 source 點擊下載 3.6 信息 自動添加

1 環境版本說明

  Jdk : 1.8

  Maven : 3.5

  IDEA : 專業版 2017.2

技術分享圖片

技術分享圖片

2 環境準備

  2.1 Maven安裝及其配置

  2.2 Tomcat安裝及其配置

3 詳細步驟

  3.1 根據模板創建maven工程

    file -> new -> project -> maven -> webapp

    技巧01:根據模板創建web工程時,請選擇 maven-archetype-webapp

技術分享圖片

  3.2 目錄結構調整

    項目創建成功後的目錄結構如下:

技術分享圖片

    跳坑01:新創建的項目中沒有存放源文件的java文件夾,也沒有存放測試文件的test文件夾,同樣沒有存放資源文件的resources文件夾

    跳坑01:在main目錄下新建java、resources兩個文件夾,分別用來存放源文件和資源文件;在main的同級目錄中新建test目錄用來存放測試文件夾

    技巧01:雖然我們創建了相關的文件夾,但是IDEA並不知道java文件夾是用來存放源文件,test用來存放測試文件,resources用來存放資源文件的;我們必須進行手動配置:

      file -> project structure -> modules

技術分享圖片

    設置完後整個目錄結構如下:

技術分享圖片

  3.3 配置tomcat

    3.3.1 打開啟動配置頁面

技術分享圖片

    3.3.2 添加tomcat啟動項

技術分享圖片

    3.3.3 配置tomcat基本信息

技術分享圖片

    3.3.4 添加web模塊

      技巧01:為項目添加一個web模塊,file -> project structure -> module

技術分享圖片

技術分享圖片

      跳坑01:利用IDEA創建項目時會在main目錄下創建一個webapp文件夾,該文件夾裏面的內容就是需要被部署到tomcat容器的內容,但是我們為項目添加了web模塊後會自動在項目的根目錄下生成一個web文件夾【建議將這個web文件夾刪掉】,這個文件夾的作用和main目錄下的webapp文件夾的作用相同,而且添加web模塊時自動尋找的是新創建的web文件夾下面的web.xml文件;將web.xml改成webapp下面的web.xml,並將web的源文件文件夾改成webapp,修改後的效果如下:

技術分享圖片

    3.3.5 添加artifacts

      技巧01:添加一個web應用,這個web引用來源於modules【其實就是來源於我們創建的web工程】

技術分享圖片

技術分享圖片

    3.3.6 配置發布頁面

      將 artifacts 中配置為web應用添加到tomcat配置中的deployment

技術分享圖片

    3.3.7 配置開發熱部署

      就是修改前後臺代碼後不用重啟tomcat,IDEA會自動進行【修改後臺時自動重啟tomcat服務器,修改前臺時刷新瀏覽器就可以啦】

技術分享圖片

    3.3.8 啟動測試

      直接通過IDEA啟動tomcat就可以啦

      技巧01:應用啟動成功後,會自動訪問webapp裏面的index.jsp頁面

技術分享圖片

技術分享圖片

4 添加框架支持

  我們創建的Web應用知識一個架子,不過IDEA支持自動添加框架;這樣就不需要手動在pom.xml中添加相關框架的依賴配置了

  右鍵項目 -> add framework stupport

技術分享圖片

  技巧01:本博文主要添加spring框架的支持

技術分享圖片

  技巧02:點擊確認後會自動將spring框架的依賴包下載到項目中去【PS: 是直接將依賴下載到項目中的lib目錄下】,整個過程有點花時間

  跳坑01:如果下載依賴期間由於網絡原因失敗,這時候就需要重新添加框架;但是這時候發現已經沒有spring相關的選項了

  填坑01:這是後就需要進入到項目結構中的modules配置中,將spring相關的模塊刪除,在重新進行框架添加

技術分享圖片

  技巧03:添加完spring框架支持後會在webapp文件夾下自動生成相關的配置文件,並在webapp中的web.xml中對這些配置文件記性監聽配置

技術分享圖片

5 Bean相關

  Bean相關的詳細內容請參見《精通spring4.x企業應用開發實戰》

  5.1 配置bean

    5.1.1 準備

      在pom.xml文件中引入lombok依賴

      創建Student類和Teacher類

技術分享圖片
package domain;

import lombok.Data;

/**
 * @author 王楊帥
 * @create 2018-08-10 20:43
 * @desc 學生實體類
 **/
@Data
public class Student {

    private String id;
    private String name;
    private Integer age;
    private String address;

    public void info() {
        System.out.println("我是學生,我在學習控制反轉相關知識點。");
    }

}
Student.java 技術分享圖片
package domain;

/**
 * @author 王楊帥
 * @create 2018-08-10 20:45
 * @desc 老師實體類
 **/
public class Teacher {

    private String id;
    private String name;
    private Integer age;
    private String address;

    public void info() {
        System.out.println("我是老師,我在教授IOC相關知識點。");
    }

}
Teacher.java

    5.1.2 利用xml配置

      技巧01:需要將配置文件放到resources文件夾下【之前通過添加spring框架支持時產生的配置文件位於webapp下面,移動後需要更改web.xml配置文件???待修改】

技術分享圖片
<?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">

    <bean id="student" class="domain.Student"></bean>

</beans>
View Code

    5.1.3 利用類註解配置

技術分享圖片
package core.config;

import domain.Teacher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 王楊帥
 * @create 2018-08-10 20:59
 * @desc bean配置類
 **/
@Configuration
public class Beans {

    @Bean(value = "teacher")
    public Teacher buildTeacher() {
        return new Teacher();
    }

}
Beans.java

  5.2 獲取bean

    5.2.1 利用BeanFactory獲取

技術分享圖片
    /**
     * 利用BeanFactory獲取bean
     * @throws IOException
     */
    @Test
    public void test01() throws IOException {
        System.out.println("Hello Boy");
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource resource = resolver.getResource("classpath:applicationContext.xml");

        System.out.println(resource.getURL());

        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
        reader.loadBeanDefinitions(resource);

        System.out.println("初始化BeanFactory完畢");

        Student student = factory.getBean("student", Student.class);
        System.out.println("student bean 獲取成功");

        student.info();

    }
View Code

    5.2.2. 利用ApplicationContext獲取

      技巧01:利用ApplicationContext獲取xml配置的bean和配置類配置的bean需要用不同的實現類

技術分享圖片
    /**
     * 利用ApplicationContext獲取bean
     */
    @Test
    public void test02() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Student student = applicationContext.getBean("student", Student.class);

        student.info();
    }

    /**
     * 利用ApplicationContext獲取配置類配置的bean
     */
    @Test
    public void test03() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Beans.class);
        Teacher teacher = applicationContext.getBean("teacher", Teacher.class);

        teacher.info();

    }
View Code

    5.2.3 獲取bean代碼匯總

技術分享圖片
package domain;

import core.config.Beans;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.IOException;

/**
 * @author 王楊帥
 * @create 2018-08-10 20:47
 * @desc 測試類
 **/
public class TestDemo {

    @Before
    public void init() {
        System.out.println("初始化方法");
    }

    /**
     * 利用BeanFactory獲取bean
     * @throws IOException
     */
    @Test
    public void test01() throws IOException {
        System.out.println("Hello Boy");
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource resource = resolver.getResource("classpath:applicationContext.xml");

        System.out.println(resource.getURL());

        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
        reader.loadBeanDefinitions(resource);

        System.out.println("初始化BeanFactory完畢");

        Student student = factory.getBean("student", Student.class);
        System.out.println("student bean 獲取成功");

        student.info();

    }

    /**
     * 利用ApplicationContext獲取bean
     */
    @Test
    public void test02() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Student student = applicationContext.getBean("student", Student.class);

        student.info();
    }

    /**
     * 利用ApplicationContext獲取配置類配置的bean
     */
    @Test
    public void test03() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Beans.class);
        Teacher teacher = applicationContext.getBean("teacher", Teacher.class);

        teacher.info();

    }

}
View Code

    

6 本博文參考源代碼

  點擊下載源代碼

7 參考博文

  項目創建

  添加框架依賴

技術分享圖片

      

    

IDEA中利用Maven創建Web項目、為Web應用添加Spring框架支持、bean的創建於獲取