Hello spring

步骤:导入相关jar包(记得选中jar包,右键build path)   

      commons-logging-1.2.jar
      spring-aop-4.1.6.RELEASE.jar
      spring-aspects-4.1.6.RELEASE.jar
      spring-beans-4.1.6.RELEASE.jar
      spring-context-4.1.6.RELEASE.jar
      spring-context-support-4.1.6.RELEASE.jar
      spring-core-4.1.6.RELEASE.jar
      spring-expression-4.1.6.RELEASE.jar
      spring-instrument-4.1.6.RELEASE.jar
      spring-jdbc-4.1.6.RELEASE.jar
      spring-orm-4.1.6.RELEASE.jar
      spring-test-4.1.6.RELEASE.jar
      spring-tx-4.1.6.RELEASE.jar
      spring-web-4.1.6.RELEASE.jar
      spring-webmvc-4.1.6.RELEASE.jar

         编写spring配置文件(名称可以自定义)

(废话少说-----:)

Hello.java

package com.yikuan.cn;

public class Hello {
private String name;
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("hello,"+name);
}
}

beans.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">
<!-- bean就是java对象,由spring容器来创建和管理 -->
<bean name="hello" class="com.yikuan.cn.Hello">
<property name="name" value="张三"/>
</bean>
</beans>

Test1.java

package com.yikuan.cn.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yikuan.cn.Hello;

public class Test1 {
public static void main(String[] args) {
//解析beans.xml文件,生成管理相应的bean对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Hello hello = (Hello) context.getBean("hello");
hello.show();
}
}