新新新新新新到不行的新手手向:Spring在Eclipse中的配置方法
2016-03-21 03:42
363 查看
|
三、配置Eclipse:
打开Eclipse-window-Preference
找到java-Build Path-User Libraries
填入自己的库名:
添加上spring离线包里libs内的jar包以及下载的commons-logging jar包,完成后我们就可以测试我们的项目了。
四、测试项目
我们需要几个类来完成测试
在测试之前我们需要把测试代码的类和配置文件建好:
首先新建java project
然后在src文件夹中新建几个类:
(这里的Person上面的小S是我添加完bean Xml之后出现的,)
然后在src文件夹下新建bean.xml
完成后是这个样子的:
然后需要配置项目类库:
右键项目properties-java Build Path选择Add Library
选择userlibrary:
然后选定我们刚才添加的类库就好了
Person类代码
<span style="font-size:18px;">package com.test.bean; public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void info(){ System.out.println("一起来吃麻辣烫!"); System.out.println("name:"+getName()+" age:"+getAge()); } }</span>
test类 代码
<span style="font-size:18px;">package testSpring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.test.bean.Person; public class test { public static void main(String[] args){ ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml"); //读取bean.xml中的内容 Person p = ctx.getBean("person",Person.class); //创建bean的引用对象 p.info(); } } </span>
然后是bean.xml 中的代码
<span style="font-size:18px;"><?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="person" class="com.test.bean.Person"> <property name="name" value="xingoo"/> <property name="age" value="12"/> </bean> </beans> </span>
然后跑起来的输出结果是这样的:
相关文章推荐
- JAVA+MYSQL+JDBC
- Java学习笔记之多态
- java中File基础
- Java学习笔记之引用类型
- Java学习笔记之垃圾回收机制
- spring学习笔记(9)AOP基本概念
- springmvc mybatis 声明式事务管理回滚失效,(checked回滚)捕捉异常,传输错误信息
- 16 API-集合(List的子类(ArrayList_Vector_LinkedList,集合嵌套遍历),JDK5新特性(泛型,增强for循环,静态导入,可变参数)
- Java Lambda表达式入门
- 18. 4Sum | Java最短代码实现
- Spring中配置和读取多个Properties文件--转
- Spring MVC 与ExtJS完美集成
- 【未完】【java算法】迷宫求解
- Java 实现多线程的三种方式
- Java DB
- spring 处理clob,blog例子
- Double的精度问题
- Java异常处理机制(经典)
- Spring Boot定时任务的使用方法
- SpringBoot 定时任务遇到的坑