创建一个Java工程,导入Spring的jar包。
一个简单的Javabean
package com.haifeiWu.testSpring;
public class HelloWorld {
private String name;
private String type;
public void setName(String name) {
this.name = name;
}
public void printHello() {
System.out.println("Spring4 : Hello ! " + name+" 类型:"+this.type);
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
3.Spring的配置文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<bean id="helloBean" class="com.haifeiWu.testSpring.HelloWorld">
<property name="name" value="yiibai" />
<property name="type" value="txt" />
</bean>
</beans>
4.执行主函数
package com.haifeiWu.core;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.haifeiWu.testSpring.HelloWorld;
/**
* Spring的执行代码
* @author wuhaifei
* @d2016年10月25日
*/
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloBean");
obj.printHello();
}
}
5.输出结果
Spring4 : Hello ! yiibai 类型:txt