多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 一、概述 原来我们利用工厂来实现灵活的配置。现在利用Spring以后把这个交给了IoC容器管理。我们只要在XML文件上配上就可以了这样的话就节省了很多开发时间我们不需要知道后面的有多少只要动态的配上类就可以了下面就给大家做一个例子 ## 二、代码展示。 文件目录如下图: ![](https://box.kancloud.cn/2016-02-22_56caddfba8121.jpg) 需要引用的jar包: spring.jar Lib/log4j/log4j.jar Lib/jakarta-commons/commons-logging.jar 需要引用的文件 Log4j.properties 下面是代码: UserDao.java ~~~ package com.bjpowernode.spring.dao; public interface UserDao { public void addUser(String userName,String password); } ~~~ UserDao4MySqlImpl.java ~~~ package com.bjpowernode.spring.dao; public class UserDao4MySqlImpl implements UserDao { public void addUser(String userName, String password) { // TODO Auto-generated method stub System.out.println("UserDao4MySqlImpl.addUser()"); } } ~~~ UserDao4OrcleImpl.java ~~~ package com.bjpowernode.spring.dao; public class UserDao4OrcleImpl implements UserDao { public void addUser(String userName, String password) { // TODO Auto-generated method stub System.out.println("UserDao4OrcleImpl.addUser()"); } } ~~~ UserManager.java ~~~ package com.bjpowernode.spring.manager; public interface UserManager { public void addUser(String userName,String password); } ~~~ UserManagerImpl.java ~~~ package com.bjpowernode.spring.manager; import com.bjpowernode.spring.dao.UserDao; public class UserManagerImpl implements UserManager{ private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } // public UserManagerImpl(UserDao userDao) { // // this.userDao = userDao; // } public void addUser(String userName, String password) { // TODO Auto-generated method stub // //由我们的应用程序负责服务定位 // UserDao userDao=new UserDao4MySqlImpl(); userDao.addUser(userName, password); } } ~~~ Client.java ~~~ package com.bjpowernode.spring.client; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bjpowernode.spring.dao.UserDao4MySqlImpl; import com.bjpowernode.spring.manager.UserManager; import com.bjpowernode.spring.manager.UserManagerImpl; public class Client { /** * @param args */ public static void main(String[] args) { BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml"); UserManager userManager=(UserManager) factory.getBean("userManager"); userManager.addUser("张三", "123"); // // TODO Auto-generated method stub // UserManager userManager=new UserManagerImpl(new UserDao4MySqlImpl()); // userManager.addUser("张三", "1231"); } } ~~~ applicationContext.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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="userDao4MySql" class="com.bjpowernode.spring.dao.UserDao4MySqlImpl"/> <bean id="userDao4Orcle" class="com.bjpowernode.spring.dao.UserDao4OrcleImpl"/> <bean id="userManager" class="com.bjpowernode.spring.manager.UserManagerImpl"> <!-- <constructor-arg ref="userDao4MySql" /> --> <property name="userDao" ref="userDao4MySql"/> </bean> </beans> ~~~ 运行结果: ![](https://box.kancloud.cn/2016-02-22_56caddfbbbb6d.jpg) ## 三、比较 与原来用工厂写的区别就在于UserManagerImpl.java和Client.java这两个类的写法不一样原来这两个类这样写 UserManagerImpl.java ~~~ package com.bjpowernode.spring.manager; import com.bjpowernode.spring.dao.UserDao; import com.bjpowernode.spring.dao.UserDao4MySqlImpl; public class UserManagerImpl implements UserManager{ public void addUser(String userName, String password) { // TODO Auto-generated method stub //由我们的应用程序负责服务定位 UserDao userDao=new UserDao4MySqlImpl(); userDao.addUser(userName, password); } } ~~~ Client.java ~~~ package com.bjpowernode.spring.client; import com.bjpowernode.spring.manager.UserManager; import com.bjpowernode.spring.manager.UserManagerImpl; public class Client { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub UserManager userManager=new UserManagerImpl(); userManager.addUser("张三", "1231"); } } ~~~ ## 四、总结 Spring给我们带来了很大的便利这样让我们开发的时候对象与对象之间进行了解耦。让我们维护起来更加方便,当需求修改的时候我们只要修改一下配置文件,添加对象就可以了不用修改写好的代码。