如何实现Spring?

23 篇文章 1 订阅
18 篇文章 0 订阅

            通过上篇文章的介绍我们已经了解了一部分原因关于为什么要使用Spring,接下来我们将通过一个例子来了解Spring是如何运行的.


1、spring的依赖包配置

*SPRING_HOME/dist/spring.jar

*SPRING_HOME/lib/log4j/log4j-1.2.14.jar

*SPRING_HOME/lib/jakarta-commons/commons-logging.jar


计算机生成了可选文字:画prefeFenCeSUSeFLibF己FieSJaVaBUi!dp日thUSerLibrarieSUSeFlibFarieSCanbeaddedpathwhenlaunched.Defineduser!ibraries:‘峨springtoaJavaBuildpathandbundleanumberofeXternalarchives.Systemlibrarieswillbeaddedtothebootclass.子springjar一D:\软件安装包丫工程软件气开发资源包卜pring\spring一framework一2.0一wit卜dependencies\spring一frame叭函1og4)一1.2.14jar一D:\软件安装包气工程软件\开发资源包spring\spring一framework一2.0一wit卜dependencies\spring一fr最commons一logging.jar一。:\软件安装包\工程软丫电开发资源包\spring\spring一framework一2.0一wit卜dependencieS\sFAddJARS~


2、提供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">
			
			
			</beans>


 

3、提供log4j.properties配置文件,放在classpath能够找到的地方.

 

4、在UserManager中提供构造函数,让spring将UserDao实现注入(DI)过来

		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的核心配置文件中

		<?xml version="1.0" encoding="UTF-8"?>
		<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"
		    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" >
		
		    <bean
		        id="userDao4Mysql"
		        class="com.tgb.spring.dao.UserDaoMysqlImpl" />
		
		    <bean
		        id="userDao4Oracle"
		        class="com.tgb.spring.dao.UserDaoOracleImpl" />
		
		    <bean
		        id="userManager"
		        class="com.tgb.spring.manger.UserManager" >
		        
		        <!-- 描述依赖关系  UserManager 依赖MySql的实现,会先找到UserDaoMysqlImpl,然后New好对象,随后将对象注入到构造函数中.-->
		        <!-- 你找女朋友和女朋友找你是不一样的 -->
		        <constructor-arg ref="userDao4Mysql"></constructor-arg>
		    </bean>
		
		</beans>


 

 

6、编写客户端

			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
			 * @comments
			 */
			public class Client {
			
				/**
				 * @param args
				 */
				public static void main(String[] args) {
			
					// 请求IOC容器,读取配置文件,根据配置文件创建UserManager对象
					BeanFactory beanFactory = new ClassPathXmlApplicationContext(
							"applicationContext.xml");
					// 获取UserManager的对象,不用去管UserDao因为在Spring中已经对关系进行管理
					UserManager userManager = (UserManager) beanFactory
							.getBean("userManager");
			
					userManager.addUser("jnqqls", "123456");
			
				}
			
			}

   

    以上便是Spring的实例,核心内容是第4步,第5步,通过依赖注入和Spring的配置文件来达到不需要主动查找对象,对象的查找,定位和创建全部由容器管理.

      Spring 小结:

     鼓励我们面向接口编程,能够大量减少FactorySingleton的数量.这样代码的层次会更清楚,只是业务对象之间的关系,程序员可以更加注重对业务的精力.主要原因是我们不再查找,定位,创建和管理对象之间的关系,都交给IOC来管理.减少了代码中的耦合(解耦合),将耦合推迟到了配置文件中,发生了变化也更容易控制.

    


   

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值