当前位置: 首页 > news >正文

欧美只做les 网站网络营销有哪些就业岗位

欧美只做les 网站,网络营销有哪些就业岗位,北京 网站建设,一键安装 wordpress这里写目录标题 1. 在类中提供生命周期控制方法,并在配置文件中配置init-method&destroy-method(配置)关闭容器操作1:ctx.close()关闭容器操作2:关闭钩子:ctx.registerShutdownHook() 2. 实现接口来做和…

在这里插入图片描述
在这里插入图片描述

这里写目录标题

  • 1. 在类中提供生命周期控制方法,并在配置文件中配置init-method&destroy-method(配置)
    • 关闭容器操作1:ctx.close()
    • 关闭容器操作2:关闭钩子:ctx.registerShutdownHook()
  • 2. 实现接口来做和init和destroy(接口)
  • 3. bean的生命周期
  • 4. bean的销毁时机

1. 在类中提供生命周期控制方法,并在配置文件中配置init-method&destroy-method(配置)

定义实现类如下:

package com.example.demo231116.dao.impl;import com.example.demo231116.dao.BookDao;public class BookDaoImpl implements BookDao {public void save(){System.out.println("book dao save...");}public void init(){System.out.println("book dao init...");}public void destroy(){System.out.println("book dao destroy...");}
}

配置方法如下:

<bean id="bookDaoCycle" class="com.example.demo231116.dao.impl.BookDaoImpl" init-method="init" destroy-method="destroy" />

最终调用跟平时一样:

// IoC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");BookDao bookDao5 = (BookDao) ctx.getBean("bookDaoCycle");
System.out.println(bookDao5);

输出结果为:

book dao init...
com.example.demo231116.dao.impl.BookDaoImpl@5dd6264

关闭容器操作1:ctx.close()

并没有自动调用destroy方法,因为在程序执行结束后,java虚拟机关闭,程序不会自动调用destroy方法,如果需要调用,可以在程序的末尾加上:ctx.close(),即在java虚拟机关闭之前执行destroy方法
但是事实上,ApplicationContext 并没有close方法, ApplicationContext 下的一个接口才有定义close方法,所以这里想要使用close方法,可以修改IoC容器定义:ClassPathXmlApplicationContextctx = new ClassPathXmlApplicationContext("applicationContext.xml");
然后再末尾调用ctx.close():

// IoC容器
ClassPathXmlApplicationContextctx = new ClassPathXmlApplicationContext("applicationContext.xml");BookDao bookDao5 = (BookDao) ctx.getBean("bookDaoCycle");
System.out.println(bookDao5);ctx.close()

输出结果为:

book dao init...
com.example.demo231116.dao.impl.BookDaoImpl@5dd6264
book dao destroy...

但是如果是这样的话,ctx.close()只能在程序的末尾写,因为在开头定义结束就写的话,这个IoC容器就被销毁了,下面也不可能执行一些getBean的操作

关闭容器操作2:关闭钩子:ctx.registerShutdownHook()

我们可以注册一个关闭钩子,在不用强行关闭IoC容器的情况下,设置在java虚拟机关闭之前让程序执行销毁的方法:

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
ctx.registerShutdownHook();BookDao bookDao5 = (BookDao) ctx.getBean("bookDaoCycle");
System.out.println(bookDao5);

这样就不再需要强硬地执行ctx.close()方法了
:我在写这些代码的过程中发现一个老师没有提及的点,也是我之前一直忽略的,这些init方法的执行,是在初始化IoC容器时候就执行了,我的完整代码如下:

package com.example.demo231116;import com.example.demo231116.dao.BookDao;
import com.example.demo231116.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Demo231116Application2 {public static void main(String[] args) {// 3. 获取IoC容器ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");ctx.registerShutdownHook();// 4. 获取bean
//        BookDao bookDao = (BookDao) ctx.getBean("bookDao");
//        bookDao.save();BookService bookService = (BookService) ctx.getBean("bookService");bookService.save();BookDao bookDao = (BookDao) ctx.getBean("dao");BookDao bookDao1 = (BookDao) ctx.getBean("dao");System.out.println(bookDao);System.out.println(bookDao1);BookDao bookDao2 = (BookDao) ctx.getBean("bookDaoFactory");System.out.println(bookDao2);BookDao bookDao3 = (BookDao) ctx.getBean("bd");System.out.println(bookDao3);BookDao bookDao4 = (BookDao) ctx.getBean("bookDaoFactoryMethod");System.out.println(bookDao4);BookDao bookDao5 = (BookDao) ctx.getBean("bookDaoCycle");System.out.println(bookDao5);//        ctx.close();}
}

得到的结果是这样的:

Factory method....
实例工厂方法...
book dao init...
book service save...
book dao save...
com.example.demo231116.dao.impl.BookDaoImpl@6193932a
com.example.demo231116.dao.impl.BookDaoImpl@6193932a
com.example.demo231116.dao.impl.BookDaoImpl@647fd8ce
com.example.demo231116.dao.impl.BookDaoImpl@159f197
com.example.demo231116.dao.impl.BookDaoImpl@78aab498
com.example.demo231116.dao.impl.BookDaoImpl@5dd6264
book dao destroy...

事实上,init方法是在IoC容器初始化的时候执行了,而不是在我具体调用getBean()的时候才运行的,默认bean是单例模式,一开始就把init()给执行掉了
如果我不使用单例而是定义多例的scope:

<bean id="bookDaoCycle" class="com.example.demo231116.dao.impl.BookDaoImpl" init-method="init" destroy-method="destroy" scope="prototype" />

主代码如下:

package com.example.demo231116;import com.example.demo231116.dao.BookDao;
import com.example.demo231116.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Demo231116Application2 {public static void main(String[] args) {// 3. 获取IoC容器ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");ctx.registerShutdownHook();// 4. 获取bean
//        BookDao bookDao = (BookDao) ctx.getBean("bookDao");
//        bookDao.save();BookService bookService = (BookService) ctx.getBean("bookService");bookService.save();BookDao bookDao = (BookDao) ctx.getBean("dao");BookDao bookDao1 = (BookDao) ctx.getBean("dao");System.out.println(bookDao);System.out.println(bookDao1);BookDao bookDao2 = (BookDao) ctx.getBean("bookDaoFactory");System.out.println(bookDao2);BookDao bookDao3 = (BookDao) ctx.getBean("bd");System.out.println(bookDao3);BookDao bookDao4 = (BookDao) ctx.getBean("bookDaoFactoryMethod");System.out.println(bookDao4);BookDao bookDao5 = (BookDao) ctx.getBean("bookDaoCycle");BookDao bookDao6 = (BookDao) ctx.getBean("bookDaoCycle");System.out.println(bookDao5);System.out.println(bookDao6);//        ctx.close();}
}

这样运行的结果是:

Factory method....
实例工厂方法...
book service save...
book dao save...
com.example.demo231116.dao.impl.BookDaoImpl@d4342c2
com.example.demo231116.dao.impl.BookDaoImpl@d4342c2
com.example.demo231116.dao.impl.BookDaoImpl@2bbf180e
com.example.demo231116.dao.impl.BookDaoImpl@163e4e87
com.example.demo231116.dao.impl.BookDaoImpl@56de5251
book dao init...
book dao init...
com.example.demo231116.dao.impl.BookDaoImpl@78aab498
com.example.demo231116.dao.impl.BookDaoImpl@5dd6264

是在具体定义实例的时候才执行的init方法,所以scope不同,init方法执行的先后顺序是不一样的

2. 实现接口来做和init和destroy(接口)

只需要在bean类下多实现这两个接口:
并继承必要的方法:
在这里插入图片描述
定义代码如下:

package com.example.demo231116.dao.impl;import com.example.demo231116.dao.BookDao;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;public class BookDaoImpl implements BookDao, InitializingBean, DisposableBean {public void save(){System.out.println("book dao save...");}//    public void init(){
//        System.out.println("book dao init...");
//    }@Overridepublic void destroy() throws Exception {System.out.println("接口destroy");}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("接口init");}
}

Spring Config配置如下:

<bean id="bookDaoCycle" class="com.example.demo231116.dao.impl.BookDaoImpl" />

这个afterPropertiesSet的init方法,是在先执行属性设置后再执行init方法

3. bean的生命周期

在这里插入图片描述

4. bean的销毁时机

在这里插入图片描述

http://www.yayakq.cn/news/626759/

相关文章:

  • 百度网站验证是二次开发有没有前途
  • 温州建站模板搭建专门找人做软件的网站
  • 哈尔滨做网站找哪家好网络行业做什么挣钱
  • 怎么做国外的网站网站建设实训结论与分析总结
  • 宁波网站制作网站开发网站私活
  • 门户网站设计特点好用WordPress产品展示主题
  • 网站建设开发费用预算安卓小项目源码免费网站
  • 手机网站费用酒店网站建设便宜
  • 绿色网站设计广州公司注册代理公司注册服务
  • python做网站源码长安区建设局官网站站
  • 企业网站做静态网站还是自己做企业网站用哪个软件
  • 网站优化排名易下拉霸屏小型电子商务网站开发
  • 番禺网络公司wordpress seo 优化
  • 清远专业网站建设服务新浪云应用 wordpress
  • 帮别人做网站被抓对单位网站建设的建议
  • ps 做网站切图网站的备案流程
  • 做网站都不赚钱了吗潍坊seo关键词排名
  • 许昌小学网站建设天津网站建设价格多少
  • 有没有做链接的网站dede 如何做视频网站
  • 低价网站建设资讯wordpress图标字体不显示
  • 如何提高网站在百度的排名wordpress模板开发套用
  • 杭州开发区网站建设新北做网站
  • 绍兴专业做网站城厢区住房和城乡建设局网站
  • 哪些网站做外链京东网站推广方式
  • 企业网站建设需要提供什么材料在线房产网
  • 网站制作 视频福州商城网站开发公司
  • 南京建站公司模板成都做网络推广的公司有哪些
  • 1688货源网官方网站wordpress博客文章栏目
  • 小当网 绵阳网站建设互联网技术对人们工作生活的影响
  • 建网站 就能开店做一款网站注意啥