Eclipse创建简单spring框架

  Eclipse创建简单spring框架

一:下载spring Framework

  https://repo.spring.io/release/org/springframework/spring/5.2.6.RELEASE/

  往下拉,选择稳定且最新的版本,下载并解压

 

二:导入包:

  1. 四个包,前两个十分重要,后面是描述功能的,分别在工程里面导入下面四个包,这也是ico基本包:

 

  1. 还需要导入一个外包,也就是日志包commons-logging-1.1.1.jar

  网站地址,请自行下载:https://www.jb51.net/softs/577391.html

  (可以把这几个包方法哦Eclipse的JRE System包中,这样更加方便)

 

三:Eclipse安装Spring IDE插件

  1)打开Eclipse,点击Help->Install New Software

  2)选择Add,添加Name(可以随便取)和Location:http://dist.springsource.com/release/TOOLS/update/e4.10/

  3)勾选中下边四个和Spring相关的文件。Core/Spring IDE、Extensions/Spring IDE、Integrations/Spring IDE、Resources/Spring IDE四项

 

四:创建两个class,一个就是最简单的java代码,另外一个需要配置spring的容器和引入spring的配置文件xml

  包1: ApplicationContext :

    ApplicationContext的中文意bai思是“应用前后关系”,它继承自duBeanFactory接口,除了包含BeanFactory的所有功能之zhi外,在国际化dao支持、资源访问(如URL和文件)、事件传播等方面进行了良好的支持,可应用在Java APP与Java Web中

 

五:实例:

  一:创建Dynamic web project

    注意在创建的过程中要把web.xml

  注意:这里会出现类似这样子的问题加上,至于怎么加,请参考下面:https://blog.csdn.net/pseudonym_/article/details/77412812

 

  二:在Java Resources中的source加入下面两个包,并创建之前说的实体类和测试类:

  并且按照上面图片创建一个application.xml,好了,代码附上

Hello.java:

package com.spring_test;
​
public class Hello {
    public void sleep() {
        System.out.println("I want sleep");
    }
    
    public void study() {
        System.out.println("I want study");
    }
}

Test_hou_1.java:

package test;
​
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring_test.Hello;
​
public class Test_hou_1 {
    public static void main(String[] args) {
        //1.加载spring配置文件
        ApplicationContext app=new ClassPathXmlApplicationContext("application.xml");
        //获取配置对象,返回对象的需要强制转换成Hello类的
        Hello hello=(Hello)app.getBean("hello");
        hello.sleep();
        hello.study();
    }
}

application.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"><!--配置文件的内容,id是在引用的名字,class是对应想要执行的类(这个必须要包含包路径)  -->
<bean id="hello" class="com.spring_test.Hello"></bean>
 
</beans>

三:导入jar包:

  右键项目->Build Path->Configure Build Path->点击Classpath->点击右侧Add External JARs->导入上面说的4个包,缺一个也不行哦

 

四:输出:

 

六:出现问题:

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path res

这个时候需要导入aop包来解决问题,当然版本要与其他spring包的版本要一致

原文地址:https://www.cnblogs.com/instead-everyone/p/13858981.html