Spring review--Spring框架搭建

ZeroWM發表於2016-08-04


一、Spring依賴包配置


框架成品目錄結構如下:



本次搭建採用的spring-framework-2.0,搭框架需要的核心包在解壓包裡面的位置:




如何載入包呢?

window—preference—User Libraries—Add External JARS



這樣就建立User Libraries,我們還需要把這個新增入專案中:




二、提供spring配置檔案applicationContext.xml


spring-framework-2.0中找到ApplicationContext.xml檔案,複製到框架中,並刪除註釋。如果敲<符號的時候,沒有出現相應提示,執行以下操作,就會有提示出現了:






效果如下:




三、提供log4j.properties 配置檔案


從\spring-framework-2.0\samples\jpetstore\war\WEB-INF  路徑下面找到log4j.properties並複製到專案中



四、在UserManager中提供函式,讓Spring將UserDao實現注入(DI)


UserManagerImpl程式碼:

package com.bjpowernode.spring.manager;

import com.bjpowernode.spring.dao.UserDao;
import com.bjpowernode.spring.dao.UserDao4MySqlImpl;
import com.bjpowernode.spring.dao.UserDao4OracleImpl;

public class UserManagerImpl implements UserManager {

	private UserDao userDao;//定義成員變數
	//通過構造方法賦值
	public UserManagerImpl(UserDao userDao) {
		super();
		this.userDao = userDao;
	}

	@Override
	public void addUser(String username, String password) {

		//(服務/物件)定位程式碼,主動查詢
		//UserDao userDao=new UserDao4OracleImpl();
		userDao.addUser(username, password);
	}
	/*public void setUserDao(UserDao userDao) {
	this.userDao = userDao;
	}*/
}



五、讓Spring管理我們物件的建立和依賴,必須將依賴關係配置到Spring的核心配置檔案中


applicationContext.xml,配置介面和實現的關係。constructor-arg:通過建構函式注入。property:通過setxx方法注入。

<?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: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="userDao4Oracle" class="com.bjpowernode.spring.dao.UserDao4OracleImpl"/>
	<bean id="userManager" class="com.bjpowernode.spring.manager.UserManagerImpl">
		<constructor-arg ref="userDao4Mysql"/><!-- <property name="userDao" ref="userDao4Mysql"/> -->
	</bean>

</beans>
	



六、編寫客戶端

Client程式碼:

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 {

	public static void main(String[] args) {
		//UserManager userManager=new UserManagerImpl(new UserDao4MySqlImpl());
		//userManager.addUser("張三", "123");

		//通過BeanFactory解決Bean之間依賴,達到鬆耦合的效果;
		BeanFactory factory =new ClassPathXmlApplicationContext("applicationContext.xml");
		UserManager userManager=(UserManager ) factory.getBean("userManager");
		userManager.addUser("張三", "123");
	}

}





相關文章