Spring的hello,引用IOC容器给bean赋值,以及bean的配置

环境在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);
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值