spring(2)
什么是spring?
答:spring是一个轻量级的J2EE框架,它贯穿程序中每一个层次,通过DI(依赖注入)可以降低组件之间的耦合度,它的AOP(面向方面编程)可以让程序员专注于写业务逻辑
DI: 依赖注入
IOC: 控制反转
AOP:面向方面编程 (面向切面编程)
在spring的应用程序中,所有对象均是由spring的工厂(Spring容器)产生
ApplicationCotnext -----spring容器 它是继承于BeanFactory
(它负责产生所有的对象实例)
spring容器要产生的实例,必须首先在spring的核心文件applicationContext.xml文件中注册才可以
applicationContext.xml用于注册对象,维护对象与对象之间的关系,连接数据库.......
-----------------------------------------------------------------------------------------------
产生容器工厂
ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");
对象 obj =(对象) cxt.getBean("id名称");
IDao----接口
Dao------接口实现类
-------------------------------------------------------------
Dao-----接口
DaoImpl-------------接口实现类
spring与Hibernate整合:
整合的顺序本身没有要求,只不过,先导入spring,再导入hibernate会比较轻松
1、先导入spring
2、导入Hibernate
3、映射实体类
4、编写Dao类,并且让Dao类继承于HibernateDaoSupport,这样就可以使用一组Hibernate工具类提供的方法,简化操作
***********注意:所有继承于HibernateDaoSupport的子类,必须给父类注入 sessionFactory
5、在applicationContext.xml文件中注册Dao类,并且,给父类注入sessionFactory
6、创建service接口与实现类,并且在applicaitonContext.xml中注册(要注入一个Dao)
7、测试类
spring与Hibernate集成之后的依赖关系
Service > Dao > sessionFactory > dataSource
spring + struts2
步骤:
1、导入struts2
2、导入spring
注意:spring配置文件建议放到WEB-INF
3、添加index.jsp,show.jsp,以及LoginAction
4、在struts.xml文件中配置如下代码
<constant name="struts.objectFactory" value="spring"/>
<package name="struts2" extends="struts-default">
<action name="shw" class="Action在spring配置文件中的id名称">
<result name="shw">/show.jsp</result>
</action>
</package>
5、导入包struts2-spring-plugin.jar
6、在web.xml文件中,配置监听器加载spring配置文件
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
spring第二章
什么是spring?
答:spring是一个轻量级的J2EE框架,它贯穿程序中每一个层次,通过DI(依赖注入)可以降低组件之间的耦合度,它的AOP(面向方面编程)可以让程序员专注于写业务逻辑
DI: 依赖注入
IOC: 控制反转
AOP:面向方面编程 (面向切面编程)
在spring的应用程序中,所有对象均是由spring的工厂(Spring容器)产生
ApplicationCotnext -----spring容器 它是继承于BeanFactory
(它负责产生所有的对象实例)
spring容器要产生的实例,必须首先在spring的核心文件applicationContext.xml文件中注册才可以
applicationContext.xml用于注册对象,维护对象与对象之间的关系,连接数据库.......
-----------------------------------------------------------------------------------------------
产生容器工厂
ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");
对象 obj =(对象) cxt.getBean("id名称");
IDao----接口
Dao------接口实现类
-------------------------------------------------------------
Dao-----接口
DaoImpl-------------接口实现类
spring与Hibernate整合:
整合的顺序本身没有要求,只不过,先导入spring,再导入hibernate会比较轻松
1、先导入spring
2、导入Hibernate
3、映射实体类
4、编写Dao类,并且让Dao类继承于HibernateDaoSupport,这样就可以使用一组Hibernate工具类提供的方法,简化操作
***********注意:所有继承于HibernateDaoSupport的子类,必须给父类注入 sessionFactory
5、在applicationContext.xml文件中注册Dao类,并且,给父类注入sessionFactory
6、创建service接口与实现类,并且在applicaitonContext.xml中注册(要注入一个Dao)
7、测试类
spring与Hibernate集成之后的依赖关系
Service > Dao > sessionFactory > dataSource
spring + struts2
步骤:
1、导入struts2
2、导入spring
注意:spring配置文件建议放到WEB-INF
3、添加index.jsp,show.jsp,以及LoginAction
4、在struts.xml文件中配置如下代码
<constant name="struts.objectFactory" value="spring"/>
<package name="struts2" extends="struts-default">
<action name="shw" class="Action在spring配置文件中的id名称">
<result name="shw">/show.jsp</result>
</action>
</package>
5、导入包struts2-spring-plugin.jar
6、在web.xml文件中,配置监听器加载spring配置文件
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>