Spring---->环境 及 为什么使用spring
一、为什么使用Spring(举例说明)
UserDao.java
package com.ncepu.dao; public interface UserDao { public void save(String name,String psssword); }
UserdaoForMysqlImpl.java
package com.ncepu.dao; public class UserdaoForMysqlImpl implements UserDao { public void save(String name, String psssword) { System.out.print("-----UserdaoForMysqlImpl---实现"); } }
UserdaoForOracleImpl.java
package com.ncepu.dao; public class UserdaoForOracleImpl implements UserDao { public void save(String name, String psssword) { System.out.print("-----UserdaoForOracleImpl---实现"); } }
UserManager.java
package com.ncepu.manager; public interface UserManager { public void save(String username, String password); }
UserManagerImpl.java
package com.ncepu.manager; import com.ncepu.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); } }
Client.java
package com.ncepu.client; import com.ncepu.dao.UserdaoForOracleImpl; import com.ncepu.manager.UserManager; import com.ncepu.manager.UserManagerImpl; public class Client { public static void main(String[] args) { // UserManager userManager = new UserManagerImpl(new UserdaoForMysqlImpl()); // userManager.save("张三", "123"); UserManager userManager = new UserManagerImpl(new UserdaoForOracleImpl()); userManager.save("张三", "123"); } }
在Client中,如果需求改变,如果不用依赖注入,我们需要给出两套实现。
二.控制反转(Inversion of Control)与依赖注入(Dependency Injection)
三、Spring实现,使一切变得简单
1、spring依赖库
* SPRING_HOME/dist/spring.jar
* SPRING_HOME/lib/jakarta-commons/commons-logging.jar
* SPRING_HOME/lib/log4j/log4j-1.2.14.jar
2、拷贝spring配置文件applicationContext.xml到src下[spring-framework-2.0\samples\jpetstore\war\WEB-INF里面有]
3、拷贝log4j配置文件log4j.properties到src下 [spring-framework-2.0\samples\jpetstore\war\WEB-INF里面有]
4、在UserManagerImpl中提供构造函数或setter方法,spring将实例化好的UserDao实现注入给我们
5、让spring管理我们的对象创建和依赖,必须在spring配置中进行定义
构造函数
<bean id="UserdaoForMysqlImpl" class="com.ncepu.dao.UserdaoForMysqlImpl"> </bean> <bean id="UserdaoForOracleImpl" class="com.ncepu.dao.UserdaoForOracleImpl"></bean> <bean id="UserManagerImpl" class="com.ncepu.manager.UserManagerImpl"> <constructor-arg ref="UserdaoForMysqlImpl"></constructor-arg> </bean>
或者setter方法
<bean id="UserdaoForMysqlImpl" class="com.ncepu.dao.UserdaoForMysqlImpl"> </bean> <bean id="UserdaoForOracleImpl" class="com.ncepu.dao.UserdaoForOracleImpl"></bean> <bean id="UserManagerImpl" class="com.ncepu.manager.UserManagerImpl"> <property name="userDao" ref="UserdaoForOracleImpl"></property> </bean>
6、编写客户端Client.java
package com.ncepu.client; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ncepu.manager.UserManager; public class Client { public static void main(String[] args) { BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml"); UserManager userManager = (UserManager)factory.getBean("UserManagerImpl"); userManager.save("张三", "123"); } }
使用了依赖注入,需求改变时,客户端我们不需要改,只需要改配置文件就可以。
四、Spring最常用的特性
利用Spring构建业务逻辑层
管理依赖关系
适应需求变更
利用Spring创建数据访问对象(DAO)
利用Spring进行事务处理