1.创建接口
2.创建接口实现类
3.在beanfactory的xml文件中配置创建的接口实现类。
4.通过工厂得到类的实例化,下面是例子
package com.service.test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
public class bftest {
public static void main(String[] args) {
DefaultListableBeanFactory beanFactory=new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader=new XmlBeanDefinitionReader(beanFactory);
reader.loadBeanDefinitions("beans.xml");
Object userService=beanFactory.getBean("userServiceimpl");
//System.out.println(userService);
Object userDao=beanFactory.getBean("userDaoimpl");
//System.out.println(userDao);
}
}
5.关于依赖注入即设置xml中bean的property元素来实现依赖注入,下面是例子
<?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="userServiceimpl" class="com.service.impl.UserServiceimpl">
<property name="userDao" ref="userDaoimpl" ></property>
</bean>
<bean id="userDaoimpl" class="com.service.Dao"></bean>
</beans>