1Spring xml配置IOC——注入值属性

1属性注入。其实就是把对象里属性的值从xml读取出来,然后通过set方法设置到对象中。
开始
1、新建项目,spring_ioc
2、新建一个类User,包是com.spring.model
代码如下
package com.spring.model;

public class User {
	private int userId;
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	private String userName;
}

这是一个model类,作用就不说了,现在要从xml中把值注入进去
3 新建xml文件名springbeans.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-2.5.xsd">  
            
            <bean id="user" class="com.spring.model.User">
            	<property name="userId" value="1"></property>
            	<property name="userName" value="张三"></property>
            </bean>   
       
</beans>

4新建一个测试类,为了简单就不适用junit了,包名com.spring.model.test类名UserTest
代码如下
package com.spring.model.test;

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

import com.spring.model.User;

public class UserTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ApplicationContext ctx = new ClassPathXmlApplicationContext("springbeans.xml");
	 	User user = (User) ctx.getBean("user");
	 	System.out.println(user.getUserId());
	 	System.out.println(user.getUserName());
	}

}

这里注意,必须用ctx.getbean这种方式拿到对象,如果直接User user=new User();是无法注入属性的值的。
在这里会发现代码处错误,是因为还没有引入jar包
5引入jar包
引入spring.jar包
运行还是不行,因为还需要common-logging的jar包,再引入这个jar,这里我用的是1.2版本
引入jar包后的图

1Spring xml配置IOC——注入值属性

执行UserTest.java ok了
运行结果如下
2014-8-25 23:05:25 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@61b383e9: display name [org.springframework.context.support.ClassPathXmlApplicationContext@61b383e9]; startup date [Mon Aug 25 23:05:25 HKT 2014]; root of context hierarchy
2014-8-25 23:05:25 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [springbeans.xml]
2014-8-25 23:05:26 org.springframework.context.support.ClassPathXmlApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@61b383e9]: org.springframework.beans.factory.support.DefaultListableBeanFactory@3d3e58d4
2014-8-25 23:05:26 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3d3e58d4: defining beans [user]; root of factory hierarchy
1
张三

你可能感兴趣的:(spring,IOC,属性注入)