2Spring xml配置IOC——注入对象

上一讲介绍了如何使用xml注入值类型,这里我们要注入的是对象。
这里我们需要用到2个类,分别是UserDao和UserService,UserDao是直接与数据库打交道的类,而UserService是面向业务给业务提供调取方法的类。

UserService是需要调取UserDao用于实现自己的业务逻辑的。

继续使用上一个例子的代码

[color=red]1、新建UserDao类,包com.spring.dao[/color]
这里我们并没有真正的操作数据库,打印一行文字来代替

package com.spring.dao;

public class UserDao {

public void add()
{
System.out.println("执行UserDao.add()方法");
}
}

[color=red]2、新建UserService类 包com.spring.service[/color]

package com.spring.service;

import com.spring.dao.UserDao;

public class UserService {

private UserDao dao;
public UserDao getDao() {
return dao;
}
public void setDao(UserDao dao) {
this.dao = dao;
}
public void add()
{
dao.add();
}

}

[color=red]3、xml文件springbeans.xml[/color]
我们将要使用xml把dao这个属性通过setDao方法注入到UserService中。
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 = "dao" class="com.spring.dao.UserDao"></bean>
<bean id="userService" class ="com.spring.service.UserService">
<property name="dao" ref="dao"></property>
</bean>
</beans>

这段xml中配置了2个bean,分别对应UserDao类和UserService类的实例。这两个bean经过spring的解析之后会生成2个对象。而ref = "dao"这里的dao对应了id="dao"这句。把UserDao的对象实例注入到了UserService的dao属性中。
[color=red]4新建测试类UserServiceTest包com.spring.service.test[/color]

package com.spring.service.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.service.UserService;
public class UserServiceTest {

public static void main(String[] args) {
// TODO Auto-generated method stub

ApplicationContext ctx = new ClassPathXmlApplicationContext("springbeans.xml");
UserService service = (UserService) ctx.getBean("userService");
service.add();
}

}

如果没有spring,这段代码会报null的异常,因为UserService中的dao属性并没有被设置,是null,这样调用service.add()方法时候会调用到dao.add()方法。会报null的exception。而spring帮我们根据名字自动注入了dao属性。这就是spring做的工作。
现在测试一下。

2014-8-26 19:45:15 [color=red]org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@61b383e9: display name [org.springframework.context.support.ClassPathXmlApplicationContext@61b383e9]; startup date [Tue Aug 26 19:45:15 HKT 2014]; root of context hierarchy
2014-8-26 19:45:15 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [springbeans.xml]
2014-8-26 19:45:15 org.springframework.context.support.ClassPathXmlApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@61b383e9]: org.springframework.beans.factory.support.DefaultListableBeanFactory@4839e5b5
2014-8-26 19:45:15 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4839e5b5: defining beans [dao,userService]; root of factory hierarchy[/color]
执行UserDao.add()方法


[color=red]正确执行

下面是错误的调用方法[/color]倘若测试代码部分改为

package com.spring.service.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.service.UserService;
public class UserServiceTest {

public static void main(String[] args) {
// TODO Auto-generated method stub
UserService service = new UserService();
service.add();//因为service对象中的dao属性是null,所以在这里会报错
}

}

会报空指针异常,结果如下

Exception in thread "main" java.lang.NullPointerException
at com.spring.service.UserService.add(UserService.java:16)
at com.spring.service.test.UserServiceTest.main(UserServiceTest.java:12)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值