新建一个最简单的spring应用

确保安装了eclipse,另外有web server如tomcat或jetty等

1.新建一个web项目,名字随意取

2.导入所需jar包


另外还需  commons-logging 这个jar包,若用日志的话导入log4j(若使用需要log4j.properties这文件)

3.创建applicationcontext.xml文件  位置随意放 我放在src目录下

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="hello" class="com.liu.pojo.Hello" >
<property name="message" value=" world"></property>     <!--  这个是通过set方法注入的 确保有setter方法以及一个空的构造函数 -->
</bean>
</beans>

4.创建一个pojo类 也就是普通的javabean类 

public class Hello implements HelloImpl{
private String message;

public Hello(){

}
public Hello(String message){
this.message=message;
}
    public String getMessage() {
        return message;
    }


    public void setMessage(String message) {
        this.message = message;
    }
    public String excute(){
        return "Hello"+getMessage();
    }
}

5.创建一个测试类  

public class test {
public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext.xml");
HelloImpl hello = (Hello) ctx.getBean("hello");
System.out.println(hello.excute());

}
}

6.输出hello world即成功

当然这只是个超级简单的入门例子 仅仅体现了通过xml注入简单属性这个特性  

继续学习!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章