天天看点

初识 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>