Spring学习笔记(二):Spring在STS中的配置和入门例子

来源:互联网 发布:javascript 是什么 编辑:程序博客网 时间:2024/06/05 17:26

1、入门例子的步骤:

       (1)导入jar包:

           下载地址:http://pan.baidu.com/s/1jGKj53K(版本3.0)

        右键项目--build path--add extral archive file--找到下载的那些包,导入Spring下载的包即可,如果没有beans或者core会提示constructor错误;

   

(2)在src中创建一个applicationContext.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xmlns="http://www.springframework.org/schema/beans"      xsi:schemaLocation="http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean id="hello" class="hello.Hello"><property name="hello" value="hha"></property></bean></beans>


(3)在src中创建一个hello包,添加三个java,

hello.java

package hello;public class Hello implements HelloImpl{private String hello;public void setHello(String hello){this.hello=hello;}public void sayHello(){System.out.print(hello);}}
helloImpl.java接口类

package hello;public interface HelloImpl {public void sayHello();}

test.java测试类

package hello;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class test {public static void main(String[] args) {// TODO Auto-generated method stubApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");Hello hello=(Hello)app.getBean("hello");hello.sayHello();}}
完成:运行即可;


0 0
原创粉丝点击