王勇 spring beginning

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配置文件到src下

 

3、拷贝log4j配置文件到src下

 

4、在UserManagerImpl中提供构造函数或setter方法,spring将实例化好的UserDao实现注入给我们

     UserDao.java

package com.bjsxt.spring.dao;

public interface UserDao {
	
	public void save(String username, String password);
}

实例化好的UserDao

UserDao4MySqlImpl.java

package com.bjsxt.spring.dao;

public class UserDao4MySqlImpl implements UserDao {

	public void save(String username, String password) {
		System.out.println("--------UserDao4MySqlImpl.save()-------");
	}
}

 

 

UserDao4OracleImpl.java

package com.bjsxt.spring.dao;

public class UserDao4OracleImpl implements UserDao {

	public void save(String username, String password) {
		System.out.println("--------UserDao4OracleImpl.save()-------");
	}
}

 

 

 

    

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;
	}
}

 

5、让spring管理我们的对象创建和依赖,必须在spring配置中进行定义

      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="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"/>
		 -->
		 <property name="userDao" ref="userDao4OracleImpl"/>
	</bean>
	
</beans>

6、编写客户端

Client.java

 

package com.bjsxt.spring.client;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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");
		
		BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		UserManager userManager = (UserManager)factory.getBean("userManager");
		userManager.save("张三", "123");
//以下是不使用spring的方式 说明spring是轻量级
		
//		UserManagerImpl userManager = new UserManagerImpl();
//		userManager.setUserDao(new UserDao4MySqlImpl());
//		userManager.save("张三", "123");
	}
}

 

 

目录结构

王勇 spring beginning_第1张图片

 


spring Ioc容器的关键点:
 * 必须将被管理的对象定义到spring配置文件中

 * 必须定义构造函数或setter方法,让spring将对象注入过来
 构造函数 <constructor-arg ref="userDao4OracleImpl"/>
 setter方法 <property name="userDao" ref="userDao4OracleImpl"/>

你可能感兴趣的:(spring,log4j,String,Class,setter,encoding)