spring 框架是 java 应用最广的框架,它的成功来源于理念,而不是技术本身,它的理念包括 ioc (inversion of control,控制反转) 和 aop(aspect oriented programming,面向切面编程)。
尽管 j2ee 能够赶上 spring 的步伐,但 spring 并没有停止前进, spring 继续在其他领域发展,而 j2ee 则刚刚开始涉及这些领域,或者还没有完全开始在这些领域的创新。移动开发、社交 api 集成、nosql 数据库、云计算以及大数据都是 spring 正在涉足和创新的领域。spring 的前景依然会很美好。
注意:bean 的各种名称——虽然 spring 用 bean 或者 javabean 来表示应用组件,但并不意味着 spring 组件必须遵循 javabean 规范,一个 spring 组件可以是任意形式的 pojo。
①.spring 能帮我们根据配置文件创建及组装对象之间的依赖关系。
②.spring 面向切面编程能帮助我们无耦合的实现日志记录,性能统计,安全控制。
③.spring 能非常简单的帮我们管理数据库事务。
④.spring 还提供了与第三方数据访问框架(如hibernate、jpa)无缝集成,而且自己也提供了一套jdbc访问模板来方便数据库访问。
⑤.spring 还提供与第三方web(如struts1/2、jsf)框架无缝集成,而且自己也提供了一套spring mvc框架,来方便web层搭建。
⑥.spring 能方便的与java ee(如java mail、任务调度)整合,与更多技术整合(比如缓存框架)。
控制反转显然是一个抽象的概念,我们举一个鲜明的例子来说明。
在现实生活中,人们要用到一样东西的时候,第一反应就是去找到这件东西,比如想喝新鲜橙汁,在没有饮品店的日子里,最直观的做法就是:买果汁机、买橙子,然后准备开水。值得注意的是:这些都是你自己“主动”创造的过程,也就是说一杯橙汁需要你自己创造。
然而到了今时今日,由于饮品店的盛行,当我们想喝橙汁时,第一想法就转换成了找到饮品店的联系方式,通过电话等渠道描述你的需要、地址、联系方式等,下订单等待,过一会儿就会有人送来橙汁了。
请注意你并没有“主动”去创造橙汁,橙汁是由饮品店创造的,而不是你,然而也完全达到了你的要求,甚至比你创造的要好上那么一些。
package pojo; public class source { private string fruit; // 类型 private string sugar; // 糖分描述 private string size; // 大小杯 /* setter and getter */ }
<?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 name="source" class="pojo.source"> <property name="fruit" value="橙子"/> <property name="sugar" value="多糖"/> <property name="size" value="超大杯"/> </bean> </beans>
package test; import org.junit.test; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import pojo.source; public class testspring { @test public void test(){ applicationcontext context = new classpathxmlapplicationcontext( new string[]{"applicationcontext.xml"} ); source source = (source) context.getbean("source"); system.out.println(source.getfruit()); system.out.println(source.getsugar()); system.out.println(source.getsize()); } }
参考地址:
package pojo; public class juicemaker { // 唯一关联了一个 source 对象 private source source = null; /* setter and getter */ public string makejuice(){ string juice = "xxx用户点了一杯" + source.getfruit() + source.getsugar() + source.getsize(); return juice; } }
<?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 name="source" class="pojo.source"> <property name="fruit" value="橙子"/> <property name="sugar" value="多糖"/> <property name="size" value="超大杯"/> </bean> <bean name="juickmaker" class="pojo.juicemaker"> <property name="source" ref="source" /> </bean> </beans>
package test; import org.junit.test; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import pojo.juicemaker; import pojo.source; public class testspring { @test public void test(){ applicationcontext context = new classpathxmlapplicationcontext( new string[]{"applicationcontext.xml"} ); source source = (source) context.getbean("source"); system.out.println(source.getfruit()); system.out.println(source.getsugar()); system.out.println(source.getsize()); juicemaker juicemaker = (juicemaker) context.getbean("juickmaker"); system.out.println(juicemaker.makejuice()); } }
总结:ioc 和 di 其实是同一个概念的不同角度描述,di 相对 ioc 而言,明确描述了“被注入对象依赖 ioc 容器配置依赖对象”
最后我们简单说说ioc是如何实现的。想象一下如果我们自己来实现这个依赖注入的功能,我们怎么来做? 无外乎:
我们发现其实自己来实现也不是很难,spring实际也就是这么做的。这么看的话其实ioc就是一个工厂模式的升级版!当然要做一个成熟的ioc框架,还是非常多细致的工作要做,spring不仅提供了一个已经成为业界标准的java ioc框架,还提供了更多强大的功能,所以大家就别去造轮子啦!希望了解ioc更多实现细节不妨通过学习spring的源码来加深理解!
引用地址:
如果说 ioc 是 spring 的核心,那么面向切面编程就是 spring 最为重要的功能之一了,在数据库事务中切面编程被广泛使用。
首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能。
周边功能在 spring 的面向切面编程aop思想里,即被定义为切面
在面向切面编程aop的思想里面,核心业务功能和切面功能分别独立进行开发,然后把切面功能和核心业务功能 "编织" 在一起,这就叫aop
aop能够将那些与业务无关,却为业务模块所共同调用的逻辑或责任(例如事务处理、日志管理、权限控制等)封装起来,便于减少系统的重复代码,降低模块间的耦合度,并有利于未来的可拓展性和可维护性。
package service; public class productservice { public void dosomeservice(){ system.out.println("dosomeservice"); } }
<bean name="productservice" class="service.productservice" />
package aspect; import org.aspectj.lang.proceedingjoinpoint; public class loggeraspect { public object log(proceedingjoinpoint joinpoint) throws throwable { system.out.println("start log:" + joinpoint.getsignature().getname()); object object = joinpoint.proceed(); system.out.println("end log:" + joinpoint.getsignature().getname()); return object; } }
<?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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean name="productservice" class="service.productservice" /> <bean id="loggeraspect" class="aspect.loggeraspect"/> <!-- 配置aop --> <aop:config> <!-- where:在哪些地方(包.类.方法)做增加 --> <aop:pointcut id="loggercutpoint" expression="execution(* service.productservice.*(..)) "/> <!-- what:做什么增强 --> <aop:aspect id="logaspect" ref="loggeraspect"> <!-- when:在什么时机(方法前/后/前后) --> <aop:around pointcut-ref="loggercutpoint" method="log"/> </aop:aspect> </aop:config> </beans>
欢迎转载,转载请注明出处!
简书id:
github:
欢迎关注公众微信号:
分享自己的文娱和互联网编程知识
如对本文有疑问, 点击进行留言回复!!
Java面向对象中:方法重载和方法重写以及区别、 this关键字和super关键字以及区别
【开源】使用Angular9和TypeScript开发RPG游戏(补充了Buffer技能)
网友评论