环境在java项目下新建一个lib目录,引入jar包:
注意:Spring依赖一个日志包: commons-logging-1.1.1.jar
**commons-logging-1.1.1.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar**
Helloword的类:
package zhang.spring.helloword;
public class HelloWord {
private String name;
private int age;
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setName(String name) {
System.out.println("set name:"+name);
this.name = name;
}
public void say(){
System.out.println("hello:"+this.name);
}
public HelloWord() {
System.out.println("hello的无参构造器");
}
}
在appliactionContext.xml文件中配置bean,oic容器利用反射创建bean的对象,并且根据set
方法给bean的属性赋值
<?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 id="helloWord" class="zhang.spring.helloword.HelloWord">
<property name="name" value="Spring"></property>
</bean>
<!-- 配置bean -->
<bean id="customer" class="zhang.spring.helloword.Customer">
<property name="name" value="zhangyukang"></property>
</bean>
<!-- 使用构造器来初始化 -->
<bean id="car" class="zhang.spring.helloword.Car">
<constructor-arg value="baoma"></constructor-arg>
<constructor-arg value="100"></constructor-arg>
<constructor-arg value="20000"></constructor-arg>
</bean>
</beans>
**测试方法:**
package zhang.spring.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import zhang.spring.helloword.Car;
import zhang.spring.helloword.Customer;
import zhang.spring.helloword.HelloWord;
public class TestSpring1 {
/**
* 使用构造器类个属性赋值
*/
@Test
public void testcar(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
Car bean = (Car) applicationContext.getBean("car");
System.out.println(bean);
}
@Test
public void testcustomer(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
Customer bean = applicationContext.getBean(Customer.class);
System.out.println(bean);
bean.say();
}
/**
* IOC容器的做的事情:
* 创建在applicationContext.xml中配置的bean 的对象
* 给bean配置好的属性进行赋值操作
*/
@Test
public void test(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWord bean = applicationContext.getBean(HelloWord.class);
bean.say();
}
@Test
public void testhelloword(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWord bean = applicationContext.getBean(HelloWord.class);
System.out.println(bean);
}
@Test
public void testhelleword(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
Bean bean = (Bean) applicationContext.getBean("helloWord");
System.out.println(bean);
}
}