Spring框架入門之開發環境搭建(MyEclipse2017平台)


基於MyEclipse2017平台搭建Spring開發環境

這里MyEclipse已將Spring集成好了,我們只需要做一簡單配置即可

一、環境配置

  • OS:Windows7 64位
  • IDE工具:MyEclipse2017
  • Java版本:Java8
  • Spring版本:4.1.0

二、開始前的准備

  為了便於大家理解,以及做相應的對比,這里我們先創建一個web工程,寫一個簡單的HelloWorld

  如下:

package me.spring.beans; public class HelloWorld { private String name; public void setName(String name) { this.name = name; } public void hello() { System.out.println("hello:" + name); } } package me.spring.beans; public class Main { public static void main(String[] args) { HelloWorld helloWorld = new HelloWorld(); helloWorld.setName("Spring"); helloWorld.hello(); } }

  顯然,運行這段代碼,控制台打印出hello: Spring

三、Spring環境搭建

  1.在剛才建的web工程項目名稱上右鍵單擊——configure Facets——install Spring Facet,彈出如下頁面:

  3.選擇合適的Spring版本,以及服務運行環境(建議選擇自帶的Target runtime)

  4.保持默認,一路next,然后finish,至此Spring開發環境搭建完畢

四、環境驗證

  1,經過第三步,此時在項目名稱——src下面自動生成了一個spring的xml主配置文件,打開該文件並添加如下代碼:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans  3     xmlns="http://www.springframework.org/schema/beans"
 4  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5  xmlns:p="http://www.springframework.org/schema/p"
 6  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
 7     
 8     <bean id="helloWorld" class="me.spring.beans.HelloWorld">
 9         <property name="name" value="Spring"></property>
10     </bean>
11 
12 </beans>

  2.在Main類中創建Spring的IOC容器,並獲取bean實例,調用hello方法如下:

package me.spring.beans; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { // HelloWorld helloWorld = new HelloWorld(); // helloWorld.setName("Spring"); // helloWorld.hello(); //1.創建Spring的IOC容器對象
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //2.從IOC容器中獲取bean實例
        HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld"); //3.調用hello方法
 helloWorld.hello(); } }

3.運行代碼控制台打印如下:

 這里log4j警告,解決方法參考https://www.cnblogs.com/jbelial/archive/2012/06/05/2536814.html:

在src下面新建file名為log4j.properties內容如下:
# Configure logging for testing: optionally with log file
log4j.rootLogger=WARN, stdout
# log4j.rootLogger=WARN, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

===============================
重新發布,OK,沒有提示了。加入了這個配置文件后,再次運行程序上面的警告就會消失。尤其在進行Web 層開發的時候,只有加入了這個文件后才能看到Spring 后台完整的出錯信息。在開發Spring 整合應用時,經常有人遇到出現404 錯誤但是卻看不到任何出錯信息的情況,這時你就需要檢查一下這個文件是不是存在。

在Eclipse中開發相關項目時,在控制台經常看到如下信息:
log4j:WARN No appenders could be found for logger
log4j:WARN Please initialize the log4j system properly.

此處輸出信息並不是錯誤信息而僅只是警告信息,因為log4j無法輸出日志,log4j是一個日志輸入軟件包。可以將Struts或Hibernate等壓縮包解壓,內有log4j.properties文件,將它復制到項目src文件夾或將log4j.properties放到 \WEB-INF\classes文件夾中即可。

===================================
WARN No appenders could be found for logger的解決辦法

這幾天做一個SSH項目,tomcat啟動時出現以下問題:
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.

在網上查了一下,多是說把ContextLoaderListener改為SpringContextServlet,但我這樣改了沒用。后來在一個英文網站上看到一個遇到同樣問題的帖子,他是這樣改的:

<context-param>
   <param-name>log4jConfigLocation</param-name>
   <param-value>/WEB-INF/config/log4j.properties</param-value>
</context-param>

······

<!-- 定義LOG4J監聽器 -->
<listener>
   <listener-class>
org.springframework.web.util.Log4jConfigListener
   </listener-class>
</listener>

這樣改了問題就解決了,不用再修改ContextLoaderListener。

 

至此Spring開發環境搭建完畢!!!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM