天天看點

初識 Spring(01)---(淺談 Spring 架構)淺談 Spring 架構

淺談 Spring 架構

Spring架構是分子產品存在,除了最核心的Spring Core Container(即Spring容器)是必要子產品之外,其他子產品都是可選,視需要而定。大約有20多個子產品。

Spring的核心是控制反轉(IoC)和面向切面(AOP)。

簡單來說,Spring是一個分層的JavaSE/EE full-stack(一站式) 輕量級開源架構。

初識 Spring(01)---(淺談 Spring 架構)淺談 Spring 架構

一般來說,Spring主要分為7個子產品:

初識 Spring(01)---(淺談 Spring 架構)淺談 Spring 架構
初識 Spring(01)---(淺談 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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<bean id="hello" class="com.neuedu.hello.HelloWorld"></bean>
	<bean id="userDaoOracle" class="com.neuedu.dao.impl.UserDaoOracleImpl"></bean>
</beans>
           

Test.java

package com.neuedu.test;
import com.neuedu.hello.HelloWorld;
import com.neuedu.service.UserService;
import com.neuedu.dao.impl.UserDaoOracleImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {

	public static void main(String[] args) {
		/*HelloWorld hw = new HelloWorld();
		hw.say();*/
		/*
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		HelloWorld hw = (HelloWorld)ac.getBean("hello");
		hw.say();

		HelloWorld hw2 = (HelloWorld)ac.getBean("hello");
		System.out.println(hw == hw2);*/
		
		/*UserService us = new UserService();
		us.save();*/
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserDaoOracleImpl ud = (UserDaoOracleImpl)ac.getBean("userDaoOracle");
		ud.save();
		
	}
 
}
           

ClassPathXmlApplicationContext 建立了一個 spring 容器,在容器中加載了一個配置檔案(applicationContext.xml)

 通過配置檔案可以讀取到所有的bean标簽(id/class)

 通過 class 可以 new 一個對象,class.forName("com.neuedu.hello.HelloWorld").newInstance();

UserDao.java

package com.neuedu.dao;

public interface UserDao {
	 public void save();
}
           

UserService.java

package com.neuedu.service;
import com.neuedu.dao.UserDao;
import com.neuedu.dao.impl.UserDaoOracleImpl;
import com.neuedu.dao.impl.UserDaoMysqlImpl;
public class UserService {
	//UserDaoOracleImpl dao = new UserDaoOracleImpl();
	//UserDaoMysqlImpl udml = new UserDaoMysqlImpl();
	UserDao dao = new UserDaoMysqlImpl();//new UserDaoOracleImpl();
	
	public void save() {
		dao.save();
		
		//dao.insert();
		
	}
}
            

UserDaoMysqlImpl.java

package com.neuedu.dao.impl;

import com.neuedu.dao.UserDao;

public class UserDaoMysqlImpl implements UserDao{
	public void save() {
		System.out.println("通過mysql資料庫将使用者資訊儲存到資料庫中");
	}
}
           

UserDaoOracleImpl.java

package com.neuedu.dao.impl;
import com.neuedu.dao.UserDao;
public class UserDaoOracleImpl implements UserDao{
	public void save() {
		System.out.println("通過oracle資料庫将使用者資訊儲存到資料庫中");
	}
	
	/*public void insert() {
		System.out.println("通過mysql資料庫将使用者資訊儲存到資料庫中");
	}*/
}
           

輸出:

改進:控制反轉(IOC)

上面的方法,UserDao 注入是通過 UserService 自身 new 一個 dao 實作類  下面方法 将控制權交給 sping 容器,因為Service 和

Dao 都在 spring中,故當 Service 需要 Dao 時,會自動将 dao 的實作類的對象放入 Service 中 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<bean id="hello" class="com.neuedu.hello.HelloWorld"></bean>
	<bean id="userDaoOracle" class="com.neuedu.dao.impl.UserDaoOracleImpl"></bean>
	<bean id="userService" class="com.neuedu.service.UserService">
	<!-- SQL注入 -->
		<property name="dao" ref="userDaoOracle"></property>
	</bean>
</beans>
            
package com.neuedu.test;
import com.neuedu.hello.HelloWorld;
import com.neuedu.service.UserService;
import com.neuedu.dao.impl.UserDaoOracleImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {

	public static void main(String[] args) {
	
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		/*UserDaoOracleImpl ud = (UserDaoOracleImpl)ac.getBean("userDaoOracle");
		ud.save();*/
		
		UserService us = (UserService)ac.getBean("userService");
		us.save();
	}
 
}
           
package com.neuedu.service;
import com.neuedu.dao.UserDao;
import com.neuedu.dao.impl.UserDaoOracleImpl;
import com.neuedu.dao.impl.UserDaoMysqlImpl;
public class UserService {
	//UserDaoOracleImpl dao = new UserDaoOracleImpl();
	//UserDaoMysqlImpl udml = new UserDaoMysqlImpl();
	//UserDao dao = new UserDaoMysqlImpl();//new UserDaoOracleImpl();
	
	private UserDao dao;
	
	public void save() {
		dao.save();
		
		//dao.insert();
		
	}

	public UserDao getDao() {
		return dao;
	}

	public void setDao(UserDao dao) {
		this.dao = dao;
	}
}
            

好處:Service 和 Dao 徹底解耦,隻需更改配置檔案: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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<bean id="hello" class="com.neuedu.hello.HelloWorld"></bean>
	<bean id="userDaoMysql" class="com.neuedu.dao.impl.UserDaoMysqlImpl"></bean>
	<bean id="userDaoOracle" class="com.neuedu.dao.impl.UserDaoOracleImpl"></bean>
	<bean id="userService" class="com.neuedu.service.UserService">
	<!-- SQL注入 -->
		<property name="dao" ref="userDaoMysql"></property>
	</bean>
</beans>