• spring整合


    spring整合hibernate,整合什么?

    1. Spring 整合 Hibernate 整合什么 ?
    
    1). 有 IOC 容器来管理 Hibernate 的 SessionFactory
    2). 让 Hibernate 使用上 Spring 的声明式事务
    
    2. 整合步骤:
    
    1). 加入 hibernate
    ①. jar 包
    ②. 添加 hibernate 的配置文件: hibernate.cfg.xml
    ③. 编写了持久化类对应的 .hbm.xml 文件。 
    
    2). 加入 Spring
    ①. jar 包
    ②. 加入 Spring 的配置文件
    
    3). 整合.
    
    3. 编写代码

    加入hibernate4的jar包,lib equired下有8个,全加入。

    加入mysql驱动

    加入c3p0

    加入spring4的jar包,required下15个

    整合的时候,我该怎么用hibernate呢?用hibernate,需要他的sessionFactory,可以从当前线程中获取与线程绑定的 sessionFactory ,他是线程安全的

    这样用:

    在daoimpl里:

    @Autowired
    private SessionFactory sessionFactory;
    
    //不推荐使用 HibernateTemplate 和 HibernateDaoSupport
    //因为这样会导致 Dao 和 Spring 的 API 进行耦合
    //可以移植性变差
    //    private HibernateTemplate hibernateTemplate;
    
    //获取和当前线程绑定的 Session. 
    private Session getSession(){
        return sessionFactory.getCurrentSession();
    }

     spring整合web应用:

    1. Spring 如何在 WEB 应用中使用 ?
    
    1). 需要额外加入的 jar 包:
    
    spring-web-4.0.0.RELEASE.jar
    spring-webmvc-4.0.0.RELEASE.jar
    
    2). Spring 的配置文件, 没有什么不同
    
    3). 如何创建 IOC 容器 ? 
    
    ①. 非 WEB 应用在 main 方法中直接创建
    ②. 应该在 WEB 应用被服务器加载时就创建 IOC 容器: 
    
    在 ServletContextListener#contextInitialized(ServletContextEvent sce) 方法中创建 IOC 容器.
    
    ③. 在 WEB 应用的其他组件中如何来访问 IOC 容器呢 ?
    
    在 ServletContextListener#contextInitialized(ServletContextEvent sce) 方法中创建 IOC 容器后, 可以把其放在
    ServletContext(即 application 域)的一个属性中. 
    
    ④. 实际上, Spring 配置文件的名字和位置应该也是可配置的! 将其配置到当前 WEB 应用的初始化参数中较为合适. 
    
    2. Spring 如何整合 Struts2 ?

    创建一个listener:

    为了spring的配置文件的可配置,在web.xml文件中,初始化参数:

    <context-param>
    <param-name>configLocation</param-name>
    <param-value>applicationContext.xml</param-value>
    </context-param>

    然后在listener中获取参数并初始化ioc容器,并放到servletContext中:

    /**
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent arg0) {
        //1. 获取 Spring 配置文件的名称. 
        ServletContext servletContext = arg0.getServletContext();
        String config = servletContext.getInitParameter("configLocation");
        
        //1. 创建 IOC 容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        
        //2. 把 IOC 容器放在 ServletContext 的一个属性中. 
        servletContext.setAttribute("ApplicationContext", ctx);
    }

    之后就可以开始servlet开发了 :

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 从 application 域对象中得到 IOC 容器的引用
        ServletContext servletContext = getServletContext();
        ApplicationContext ctx = (ApplicationContext) servletContext.getAttribute("ApplicationContext");
        
        //2. 从 IOC 容器中得到需要的 bean
        Person person = ctx.getBean(Person.class);
        person.hello();
    }

    其实spring有给提供listener,直接用即可

    4). 在 WEB 环境下使用 Spring
    
    ①. 需要额外加入的 jar 包:
    
    spring-web-4.0.0.RELEASE.jar
    spring-webmvc-4.0.0.RELEASE.jar
    
    ②. Spring 的配置文件, 和非 WEB 环境没有什么不同
    
    ③. 需要在 web.xml 文件中加入如下配置:
    
    <!-- 配置 Spring 配置文件的名称和位置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    
    <!-- 启动 IOC 容器的 ServletContextListener -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    spring整合struts2整合什么?: 使IOC容器来管理struts2的action!

    1. Spring 如何在 WEB 应用中使用 ?
    
    1). 需要额外加入的 jar 包:
    
    spring-web-4.0.0.RELEASE.jar
    spring-webmvc-4.0.0.RELEASE.jar
    
    2). Spring 的配置文件, 没有什么不同
    
    3). 如何创建 IOC 容器 ? 
    
    ①. 非 WEB 应用在 main 方法中直接创建
    ②. 应该在 WEB 应用被服务器加载时就创建 IOC 容器: 
    
    在 ServletContextListener#contextInitialized(ServletContextEvent sce) 方法中创建 IOC 容器.
    
    ③. 在 WEB 应用的其他组件中如何来访问 IOC 容器呢 ?
    
    在 ServletContextListener#contextInitialized(ServletContextEvent sce) 方法中创建 IOC 容器后, 可以把其放在
    ServletContext(即 application 域)的一个属性中. 
    
    ④. 实际上, Spring 配置文件的名字和位置应该也是可配置的! 将其配置到当前 WEB 应用的初始化参数中较为合适. 
    
    4). 在 WEB 环境下使用 Spring
    
    ①. 需要额外加入的 jar 包:
    
    spring-web-4.0.0.RELEASE.jar
    spring-webmvc-4.0.0.RELEASE.jar
    
    ②. Spring 的配置文件, 和非 WEB 环境没有什么不同
    
    ③. 需要在 web.xml 文件中加入如下配置:
    
    <!-- 配置 Spring 配置文件的名称和位置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    
    <!-- 启动 IOC 容器的 ServletContextListener -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    2. Spring 如何整合 Struts2 ?
    
    1). 整合目标 ? 使 IOC 容器来管理 Struts2 的 Action!
    
    2). 如何进行整合 ? 
    
    ①. 正常加入 Struts2
    
    ②. 在 Spring 的 IOC 容器中配置 Struts2 的 Action
    注意: 在 IOC 容器中配置 Struts2 的 Action 时, 需要配置 scope 属性, 其值必须为 prototype
    
    <bean id="personAction" 
        class="com.atguigu.spring.struts2.actions.PersonAction"
        scope="prototype">
        <property name="personService" ref="personService"></property>    
    </bean>
    
    ③. 配置 Struts2 的配置文件: action 节点的 class 属性需要指向 IOC 容器中该 bean 的 id
    
    <action name="person-save" class="personAction">
        <result>/success.jsp</result>
    </action> 
    
    ④. 加入 struts2-spring-plugin-2.3.15.3.jar
    
    3). 整合原理: 通过添加 struts2-spring-plugin-2.3.15.3.jar 以后, Struts2 会先从 IOC 容器中
    获取 Action 的实例.
    
    if (appContext.containsBean(beanName)) {
        o = appContext.getBean(beanName);
    } else {
        Class beanClazz = getClassInstance(beanName);
        o = buildBean(beanClazz, extraContext);
    }
    这个博客主要是javaEE相关或者不相关的记录, hadoop与spark的相关文章我写在下面地址的博客啦~ http://www.cnblogs.com/sorco
  • 相关阅读:
    父级display:none获取子元素宽的问题
    获取url参数
    借一例固定菜单栏!!!
    笨蛋!!!
    调用腾讯地图api,在手机端获取用户地理位置。
    遇到的小tip
    复制事件
    跑马灯!!!!的汉子你威武雄壮!!!
    web页面有哪三层构成?分别是什么?
    CentOS 8 手动部署LNMP环境
  • 原文地址:https://www.cnblogs.com/orco/p/6297616.html
Copyright © 2020-2023  润新知