http://www.cnblogs.com/xing901022/p/3963962.html
优点:
struts2是action直接跑去action中action调用的方法为连同属性方法都在一起,密度很高的。而我们spring通过application.xml分配至某个接口接口再去到对应的核心操作方法,而且在需要添加bean的时候单独写bean然后在appication.xml中部署一下就行了,顾虑少,易修改,,不然struts要去核心代码里面找,很不清楚
控制反转:在以往的编程中,我们需要知道各个类之间的关系,才能比较好的调用。现在的spring通过容器(习惯上为applicationContext.xml)中通过<bean id='''> <ref id='''/></bean>的方式将各个类都联系起来,管理起来。在调用时通过建bean对应class的对象,然后调用要调用的方法即可。运用步骤:加载配置文件->产生sessionfactory->open session-> begin tx->sql操作->提交事务->关session->关sessionfactiry.
依赖注入:有些与核心控制程序没什么关系的操作,比如各种日志的撰写啊什么的,加到核心层只会混淆视听,spring将他们放在切面上通过切点注入到程序里面。<aop :aspect>......
xml公共区
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
</beans>
建一个bean类,对这个bean右键refactor一个接口
测试主方法:
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");//读取bean.xml中的内容
saveinterf s=(saveinterf) ctx.getBean("save"); //获取bean savainterf为之前对bean创建的接口,,getbean是bean类
s.save(); .//用借口对象调用bean中方法