## 一、概述
原来我们利用工厂来实现灵活的配置。现在利用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给我们带来了很大的便利这样让我们开发的时候对象与对象之间进行了解耦。让我们维护起来更加方便,当需求修改的时候我们只要修改一下配置文件,添加对象就可以了不用修改写好的代码。
- 前言
- 菜鸟学习Struts——配置Struts环境
- 菜鸟学习Struts——简易计算器
- 菜鸟学习Struts——bean标签库
- 菜鸟学习Struts——Scope属性
- 菜鸟学习Struts——国际化
- 菜鸟学习Struts——总结
- 菜鸟学习Hibernate——配置Hibernate环境
- 菜鸟学习Hibernate——持久层框架
- 菜鸟学习Hibernate——简单的一个例子
- 菜鸟学习Hibernate——简单的增、删、改、查操作
- 菜鸟学习Hibernate——一对多关系映射
- 菜鸟学习Hibernate——多对多关系映射
- 菜鸟学习Hibernate——缓存
- 菜鸟学习Spring——初识Spring
- 菜鸟学习Spring——第一个例子
- 菜鸟学习Spring——60s让你学会动态代理原理
- 菜鸟学习Spring——60s使用annotation实现简单AOP
- 菜鸟学习Spring——60s配置XML方法实现简单AOP
- 菜鸟学习Spring——60s利用JoinPoint获取参数的值和方法名称
- 菜鸟学习Spring——60s学会Spring与Hibernate的集成
- 菜鸟学习SSH——目录
- 菜鸟学习Spring——SpringMVC注解版前台向后台传值的两种方式
- 菜鸟学习Spring——SpringMVC注解版在服务器端获取Json字符串并解析
- 菜鸟学习Spring——SpringMVC注解版将URL中的参数转成实体
- 菜鸟学习Spring——SpringMVC注解版解析不同格式的JSON串
- 菜鸟学习Spring——SpringIoC容器基于三种配置的对比