*SPRING_HOME/dist/spring.jar
*SPRING_HOME/lib/log4j/log4j-1.2.14.jar
*SPRING_HOME/lib/jakarta-commons/commons-logging.jar
2、提供spring配置文件applicationContext.xml
[html]
< xml version="1.0" encoding="UTF-8" >
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">
3、提供log4j.properties配置文件,放在classpath能够找到的地方.
4、在UserManager中提供构造函数,让spring将UserDao实现注入(DI)过来
[java]
package com.tgb.spring.manger;
import com.tgb.spring.dao.UserDao;
import com.tgb.spring.dao.UserDaoMysqlImpl;
/**
*
* @title UserManager 通过Spring注入实现
* @project_name spring_why_spring
* @author jnqqls
* @group TGB
* @version 1.0
* @comments
*/
public class UserManagerImpl implements UserManager {
// 因为UserDao为没有状态的类,所以可以定义为成员变量
private UserDao userDao;
/**
* 可以通过构造方法赋值
*
* @param userDao
*/
public UserManagerImpl(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void addUser(String username, String password) {
userDao.addUser(username, password);
}
}
5、让spring管理我们对象的创建和依赖,必须将依赖关系配置到spring的核心配置文件中
[html]
< xml version="1.0" encoding="UTF-8" >
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns="http://www.springframework.org/schema/beans"
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" >
id="userDao4Mysql"
class="com.tgb.spring.dao.UserDaoMysqlImpl" />
id="userDao4Oracle"
class="com.tgb.spring.dao.UserDaoOracleImpl" />
id="userManager"
class="com.tgb.spring.manger.UserManager" >
6、编写客户端
[java]
package com.tgb.spring.client;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tgb.spring.dao.UserDaoMysqlImpl;
import com.tgb.spring.manger.UserManager;
import com.tgb.spring.manger.UserManagerImpl;
/**
*
* @title Spring实例客户端
* @project_name spring_why_spring
* @author jnqqls
* @group TGB
* @version 1.0