idea構建第一個Spring框架的應用

目錄

1.點擊 New Project ,選擇Maven,然後一直正常配置就OK了,不多說(前提是你的Maven倉庫啥的都配置好了)

2. 接下來就是配置pom.xml文件了,來使用maven描述依賴包,(版本啥的要注意)

3.然後我們還是以入門必備的hello world例子來構建項目

4.log4j的配置(log4j--log for java(java的日誌))

5.關於一個idea的小警告


首先廢話不多說:擺出Spring的官方框架圖:(現在看不懂沒關係,我暫時也沒懂,先入手在走着瞧😅)

 我選擇使用Maven來管理我的項目

1.點擊 New Project ,選擇Maven,然後一直正常配置就OK了,不多說(前提是你的Maven倉庫啥的都配置好了)

2. 接下來就是配置pom.xml文件了,來使用maven描述依賴包,(版本啥的要注意)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>MavenSpring02</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.26.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>

</project>

3.然後我們還是以入門必備的hello world例子來構建項目

在src/main/java下新建一個hello包,在裏面建立三個類,內容分別如下

這裏要說說明一下Spring的兩種實現方法:

第一種:註解實現,其中三個文件的代碼如下

MessageService.java

package hello;

import org.springframework.stereotype.Component;

@Component
public class MessageService {
    public MessageService() {
        System.out.println("MessageService.....");
    }
    public String getMessage() {
        return "hello world";
    }
}

MessagePrinter.java

package hello;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MessagePrinter {
    public MessagePrinter() {
        System.out.println("MessagePrinter....");
    }

    private MessageService messageService;
    @Autowired
    public void setMessageService(MessageService messageService) {
        this.messageService = messageService;
    }

    public void printMessage() {
        System.out.println(this.messageService.getMessage());
    }
}

MainSpring.java

package hello;


import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
public class MainSpring {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new AnnotationConfigApplicationContext(MainSpring.class);
        MessagePrinter messagePrinter = applicationContext.getBean(MessagePrinter.class);
        messagePrinter.printMessage();
    }
}

(說明:註解@Component表示的類表明此類是服從Spring容器管理,即對象的創建是由容器完成的,不需程序員使用new關鍵字來自行創建,而是用@Autowried修飾的方法,表示由容器自動構建對象的依賴關係,也不需要我們自己管理對象之間的關係)

第二種:使用 applicationContext.xml (約定俗成的命名,創建方法如下圖)文件配置

然後再applicationContext.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元素:描述當前對象需要由Spring管理
    id屬性:標識對象,未來可以在應用程序中根據id訪問對象
    class屬性:被管理對象的全類名
    -->
    <bean id="service" class="hello.MessageService"></bean>
    <bean id="printer" class="hello.MessagePrinter">
        <!--
        property元素:設置當前類的屬性
        name屬性:設置屬性名
        ref屬性:設置屬性類
        -->
        <property name="messageService" ref="service"></property>
    </bean>
</beans>

使用此方法構建Spring項目可以省去類裏面的註解,然後將MainSpring.java修改成如下代碼:

package hello;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@ComponentScan
public class MainSpring {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        MessagePrinter messagePrinter = applicationContext.getBean(MessagePrinter.class);
        messagePrinter.printMessage();
    }
}

4.log4j的配置(log4j--log for java(java的日誌))

根據名字也就知道配置它有啥作用了

首先pom.xml中配置log4j依賴的包,然後在官網查得配置內容如下:

網址:https://docs.spring.io/spring/docs/4.3.26.RELEASE/spring-framework-reference/htmlsingle/

不難看出,我之前的pom.xml配置就是依據這個配置,然後我們子啊resources文件夾下新建一個log4j.properties文件,複製粘貼官網的簡單例子:

log4j.rootCategory=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n
log4j.category.org.springframework.beans.factory=DEBUG

之後,再次運行程序,便可觀察到如下執行過程了

5.關於一個idea的小警告

在創建applicationContext.xml後,使用idea會出現黃色部分的小警告

解決方法:

END--------覺得有用就賞個讚唄! good luck!

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章