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包后的图

[img]http://dl2.iteye.com/upload/attachment/0100/5509/9dc3916e-cb9b-3e78-9eb4-2a1fbdc2b7fe.png[/img]

执行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
张三
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值