基于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,弹出如下页面:

spring 配置mysql 配置时区设置 myeclipse配置spring_xml

  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.运行代码控制台打印如下:

spring 配置mysql 配置时区设置 myeclipse配置spring_spring_02

 这里log4j警告,解决方法参考:

在src下面新建file名为log4j.properties内容如下:

# Configure logging for testing: optionally with log file
log4j.rootLogger=WARN, stdout
# log4j.rootLogger=WARN, stdout, logfilelog4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%nlog4j.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开发环境搭建完毕!!!