spring3简单例子
2015-10-30 15:36
239 查看
环境:eclipse 4.6
spring版本:3.0.5
第一步,在lib中导入spring jar包,基础jar包如下:
第二步,配置spring配置文件,在src下新建spring.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-3.0.xsd">
<bean id="helloWorld" class="helloworld.HelloWorld"></bean></beans> 第三步,建一个helloworld包,在该包下创建HelloWorld类:
package helloworld;
public class HelloWorld {
public void sayHello() {
System.out.println("Hello world");
}
} 第四步,创建测试类testSpring,代码如下:
package helloworld;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testSpring {
private ApplicationContext context;
private HelloWorld helloWorld;
@Test
public void test(){
context=new ClassPathXmlApplicationContext("spring.xml");
helloWorld=(HelloWorld)context.getBean("helloWorld");
helloWorld.sayHello();
}}
spring版本:3.0.5
第一步,在lib中导入spring jar包,基础jar包如下:
第二步,配置spring配置文件,在src下新建spring.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-3.0.xsd">
<bean id="helloWorld" class="helloworld.HelloWorld"></bean></beans> 第三步,建一个helloworld包,在该包下创建HelloWorld类:
package helloworld;
public class HelloWorld {
public void sayHello() {
System.out.println("Hello world");
}
} 第四步,创建测试类testSpring,代码如下:
package helloworld;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testSpring {
private ApplicationContext context;
private HelloWorld helloWorld;
@Test
public void test(){
context=new ClassPathXmlApplicationContext("spring.xml");
helloWorld=(HelloWorld)context.getBean("helloWorld");
helloWorld.sayHello();
}}
相关文章推荐
- java实现二叉树
- java中char和byte的转换
- JavasScript基数排序
- Java注解(Annotation)详解(三)——解析注解
- java源码分析之ArrayList
- java 静态(static)方法的继承和重写
- spring基础包
- Java中的自动装箱与拆箱
- Java,数据库,,,优化问题
- java zip malformed解决
- Eclipse,MyEclipse 安装SVN插件
- Eclipse,MyEclipse 安装SVN插件
- Eclipse,MyEclipse 安装SVN插件
- Eclipse,MyEclipse 安装SVN插件
- MyEclipse 10.5 安装SVN插件
- java中的枚举介绍与简单应用
- 安装jdk出现问题:Error opening registry key'software\Javasoft\Java Runti
- 【java web】--html、xhtml、xml进化史
- Java异常机制
- 获取两个日期之间的相差月份