一、spring概述
2、在使用spring前
我们已经运用了面向接口的编程,服务层、dao层、表示层的分离
详细参见下例
1)dao层
package com.bjsxt.spring.dao; public interface UserDao { public void save(String username, String password); }
package com.bjsxt.spring.dao; public class UserDao4MySqlImpl implements UserDao { public void save(String username, String password) { System.out.println("--------UserDao4MySqlImpl.save()-------"); } }
package com.bjsxt.spring.dao; public class UserDao4OracleImpl implements UserDao { public void save(String username, String password) { System.out.println("--------UserDao4OracleImpl.save()-------"); } }
2)、服务层
package com.bjsxt.spring.manager; public interface UserManager { public void save(String username, String password); }
package com.bjsxt.spring.manager; import com.bjsxt.spring.dao.UserDao; public class UserManagerImpl implements UserManager { private UserDao userDao; public UserManagerImpl(UserDao userDao) { this.userDao = userDao; } public void save(String username, String password) { this.userDao.save(username, password); } }
public void setUserDao(UserDao userDao) { this.userDao = userDao; }
3、客户端
package com.bjsxt.spring.client; import com.bjsxt.spring.dao.UserDao4MySqlImpl; import com.bjsxt.spring.dao.UserDao4OracleImpl; import com.bjsxt.spring.manager.UserManager; import com.bjsxt.spring.manager.UserManagerImpl; public class Client { public static void main(String[] args) { //UserManager userManager = new UserManagerImpl(new UserDao4OracleImpl()); UserManager userManager = new UserManagerImpl(new UserDao4MySqlImpl()); userManager.save("张三", "123"); } }
3、使用spring后
1)、dao层、服务层可以不变
2)、1、spring依赖库
* SPRING_HOME/dist/spring.jar
* SPRING_HOME/lib/jakarta-commons/commons-logging.jar
* SPRING_HOME/lib/log4j/log4j-1.2.14.jar
可以将以上三个jar设计成一个User Liberies(方法如下:Elipse/windows/perfence/java/buildpath/user liberies)然后增加下面三个包。
2、拷贝spring配置文件applicationContext.xml拷贝到src下,进行修改。
主要修改如下
<bean id="userDao4MySqlImpl" class="com.bjsxt.spring.dao.UserDao4MySqlImpl"/> <bean id="userDao4OracleImpl" class="com.bjsxt.spring.dao.UserDao4OracleImpl"/> <bean id="userManager" class="com.bjsxt.spring.manager.UserManagerImpl"> <constructor-arg ref="userDao4OracleImpl"/> <!-- 也可以更换为非构造方法注入(set方法来注入) <property name="userDao" ref="userDao4OracleImpl"/>--> </bean>
备注:修改时若没有提示信息帮助,处理如下(Eclipse/Windows/perfermance/MyEclipse/files and Editor/xml/xml catalog/user specified Engtries/add/跳出一个有三个文本域的对话框,中间的先选中url,然后对最上面的文本框采用浏览文件系统的方式,选中spring-framework-2.0/dist/resources/spring-beans.2.o.xsd)
此时这三个文本域都有内容了。
修改文本域内容:中间的改选为“location”,最下面的在原来的基础上,在末尾加上\spring-beans.2.o.xsd
即加上你上面选定(选中spring-framework-2.0/dist/resources/spring-beans.2.o.xsd)的schemel的名字。
3、拷贝log4j配置文件到src下
4、在UserManagerImpl中提供构造函数或setter方法,spring将实例化好的UserDao实现注入给我们
5、让spring管理我们的对象创建和依赖,必须在spring配置中进行定义
6、编写客户端
import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bjsxt.spring.manager.UserManager; public class Client { public static void main(String[] args) { BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml"); UserManager userManager = (UserManager)factory.getBean("userManager"); userManager.save("张三", "123"); } }
spring Ioc容器的关键点:
* 必须将被管理的对象定义到spring配置文件中
* 必须定义构造函数或setter方法,让spring将对象注入过来