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

外贸网站建设 如何做有哪些网页设计公司

外贸网站建设 如何做,有哪些网页设计公司,做网站后台主要负责什么,做网站公司松江🍎道阻且长,行则将至。🍓 上一篇:Spring《一》快速入门 下一篇:Spring《三》DI依赖注入 目录一、bean实例化🍍1.构造方法 ***2.静态工厂 *使用工厂创建对象实例化bean3.实例工厂 ***使用示例工厂创建对象实…
🍎道阻且长,行则将至。🍓

上一篇:Spring《一》快速入门
下一篇:Spring《三》DI依赖注入


目录

  • 一、bean实例化🍍
    • 1.构造方法 ***
    • 2.静态工厂 *
      • 使用工厂创建对象
      • 实例化bean
    • 3.实例工厂 ***
      • 使用示例工厂创建对象
      • 实例工厂实例化bean
      • FactoryBean
  • 二、生命周期🍑
    • 1.生命周期设置
    • 2.在main方法使用close
    • 3.使用钩子关闭容器


一、bean实例化🍍

在上一篇Spring快速入门👉🏻中,我们使用IOC容器进行对象的创建,在IOC中的对象也称为bean,那么IOC容器是怎么创建bean的呢。

下面介绍bean的实例化方法:构造方法静态工厂实例工厂

1.构造方法 ***

  1. 我们先创建好一个maven项目,在maven配置文件pom中添加spring的依赖,再继续在resources文件夹下new个Spring的配置文件,applicationContext.xml。

  2. 准备好我们的接口和类:BookDaoBookDaoImpl:在接口添加一个save方法,到类中实现,输出一个语句示例。因为要使用构造方法,那我们就写一个无参构造器,并且在里面随便输出一个提示语句。

  3. 写好类之后,我们继续到Spring配置文件中将类配置到容器:<bean id="bookDao" name="book1" class="Demo1.dao.impl.BookDaoImpl"/>,我们可以发现在左侧出现spring的小标志了,表示配置正常;
    在这里插入图片描述

  4. 完成一个运行的程序,若最后可以打印构造器中的内容表示就是创建对象使用了构造函数。

public class AppForInstanceBook {public static void main(String[] args) {ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");BookDao bookDao = (BookDao) ctx.getBean("bookDao");bookDao.save();}
}

输出:
在这里插入图片描述
并且当构造器为私有的时候,仍然可以访问,说明了Spring的底层使用了反射。回顾参考👉🏻java反射机制
但是Spring只是使用无参构造器。

2.静态工厂 *

使用工厂创建对象

我们可以继续在前面的maven中类似地添加接口和类:OrderDaoOrderDaoImpl,然后在创建一个工厂类:

public class OrderDaoFactory {public static OrderDao getOrderDao(){return new OrderDaoImpl();}
}

完成一个运行的程序,使用工厂获取对象:

public class AppForInstanceOrder {public static void main(String[] args) {//通过静态工厂创建对象OrderDao orderDao = OrderDaoFactory.getOrderDao();orderDao.save();}
}

在这里插入图片描述

实例化bean

要使用静态工厂实例化bean只需要在Spring配置文件中添加<bean id="orderDao" name="Order1" class="Demo1.factory.OrderDaoFactory" factory-method="getOrderDao"/>,注意class是指向工厂类,添加的工厂方法属性指向工厂中创建对象的方法;静态工厂实例化配置成功之后再工厂类中也会有提示:
在这里插入图片描述 👉 在这里插入图片描述
然后在运行方法中从容器获取bean:

ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
OrderDao orderDao =(OrderDao) ctx.getBean("orderDao");
orderDao.save();

3.实例工厂 ***

使用示例工厂创建对象

我们还是可以继续在前面的maven中类似地添加接口和类:UserDaoUserDaoImpl,再同样的创建一个工厂类:OrderDaoFactory,注意和静态工厂的不一样:

//UserDao
package Demo1.dao;
public interface UserDao {public void save();
}
//UserDaoImpl
package Demo1.dao.impl;
import Demo1.dao.UserDao;
public class UserDaoImpl implements UserDao {@Overridepublic void save() {System.out.println("userDao save...");}
}
//OrderDaoFactory
package Demo1.factory;
import Demo1.dao.UserDao;
import Demo1.dao.impl.UserDaoImpl;
public class UserDaoFactory {public UserDao getUserDao(){return new UserDaoImpl();}
}

我们可以在运行程序中使用实例工厂获取对象:

public class AppForInstanceUser {public static void main(String[] args) {//创建实例工厂对象UserDaoFactory userDaoFactory = new UserDaoFactory();//通过实例工厂对象创建对象UserDao userDao = userDaoFactory.getUserDao();userDao.save();
}

我们想把这种实例工厂也交给Spring:

实例工厂实例化bean

在配置文件中添加:

<bean id="userFactory" class="Demo1.factory.UserDaoFactory"/>
<bean id="userDao" factory-method="getUserDao" factory-bean="userFactory"/>

在前面的静态工厂中我们只需要配置工厂类进来,使用静态方法就可以获得对象。而这需要先添加工厂,再添加Dao。
我们也可以修改验证一下为什么要这样,当你试图在工厂bean中加实例方法,直接给你报错:
在这里插入图片描述
其实想一想就知道了,这个不是静态方法啊,对象都没有你怎么使用这个方法。
然后在运行程序中添加:

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao) ctx.getBean("userDao");
userDao.save();

在这里插入图片描述

上面的Spring配置还是太麻烦,改!

FactoryBean

Spring为了简化这种配置方式就提供了FactoryBean来简化开发。

创建一个UserDaoFactoryBean的类,实现FactoryBean接口,重写接口的方法:

public class UserDaoFactoryBean implements FactoryBean<UserDao> {@Overridepublic UserDao getObject() throws Exception {return new UserDaoImpl();}@Overridepublic Class<?> getObjectType() {return UserDao.class;}
}

就只需要一个FactoryBean的配置:
<bean id="userDao" class="Demo1.factory.UserDaoFactoryBean"/>

FactoryBean的源代码也不长,我们可以打开看看:

public interface FactoryBean<T> {String OBJECT_TYPE_ATTRIBUTE = "factoryBeanObjectType";@NullableT getObject() throws Exception;@NullableClass<?> getObjectType();default boolean isSingleton() {return true;}
}

他提供了3个方法,我们在创建UserDaoFactoryBean的类的时候重写了两个接口的方法:
getObject(),在方法中进行对象的创建并返回;
getObjectType(),返回的是被创建类的Class对象;
isSingleton()就是设置对象是否为单例,默认true。


二、生命周期🍑

生命周期就是从创建到消亡的完整过程。bean的生命周期自然是指bean对象从创建到销毁的过程。

1.生命周期设置

我们想设置:

  • bean创建之后,想要添加内容,比如用来初始化需要用到资源
  • bean销毁之前,想要添加内容,比如用来释放用到的资源
    继续在前面的案例中,我们在实例工厂类添加:
public void init(){System.out.println("init userDaofac...");
}
public void destory(){System.out.println("destory userDaofac...");
}

在bean中追加:init-method="init" destroy-method="destory"
在这里插入图片描述
只有初始化被执行了。问:为什么
Spring的IOC容器是运行在JVM中,运行main方法后,JVM启动,Spring加载配置文件生成IOC容器,从容器获取bean对象,然后调方法执行。然而main方法执行完后,JVM就退出了,这个时候IOC容器中的bean也就结束了,根本没有调用destroy方法。
所以这个时候我们可以在main方法使用close使用钩子关闭容器

2.在main方法使用close

ApplicationContext 中没有提供close方法,我们需要使用ClassPathXmlApplicationContext
修改

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

就可以使用:ctx.close();了。
在这里插入图片描述

3.使用钩子关闭容器

就是在容器未关闭之前,提前设置好回调函数,让JVM可以在退出之前回调此函数来关闭容器。
就是把前面的ctx.close();换为ctx.registerShutdownHook();

需要修改配置文件还是太麻烦,再改!

前面的方法还是需要在Spring配置文件中添加bean的init-methoddestroy-method,Spring对此进行简化,提供了两个接口来完成生命周期的控制,而不用再进行配置InitializingBeanDisposableBean
在类里面继承接口并重写:

public class UserDaoFactoryBean implements FactoryBean<UserDao> , InitializingBean, DisposableBean {@Overridepublic UserDao getObject() throws Exception {return new UserDaoImpl();}@Overridepublic Class<?> getObjectType() {return UserDao.class;}
//************************@Overridepublic void destroy() throws Exception {System.out.println("destory...");}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("ini...");}
}

在运行程序仍然使用ctx.registerShutdownHook();
在这里插入图片描述


回到第一页☝

☕物有本末,事有终始,知所先后。🍭

🍎☝☝☝☝☝我的CSDN☝☝☝☝☝☝🍓


文章转载自:
http://cauldron.bpcf.cn
http://pulaski.bpcf.cn
http://munition.bpcf.cn
http://acquaintanceship.bpcf.cn
http://unrealistic.bpcf.cn
http://product.bpcf.cn
http://mavournin.bpcf.cn
http://humanization.bpcf.cn
http://micrometeor.bpcf.cn
http://dire.bpcf.cn
http://trengganu.bpcf.cn
http://circumlocution.bpcf.cn
http://cansure.bpcf.cn
http://automatically.bpcf.cn
http://arthurian.bpcf.cn
http://curability.bpcf.cn
http://jactance.bpcf.cn
http://petalon.bpcf.cn
http://excreta.bpcf.cn
http://infatuated.bpcf.cn
http://niftic.bpcf.cn
http://inconvenience.bpcf.cn
http://warragal.bpcf.cn
http://hyperbatic.bpcf.cn
http://unionism.bpcf.cn
http://hydropical.bpcf.cn
http://nethermore.bpcf.cn
http://unimpassioned.bpcf.cn
http://thy.bpcf.cn
http://defame.bpcf.cn
http://deglutinate.bpcf.cn
http://deictic.bpcf.cn
http://platypus.bpcf.cn
http://guarani.bpcf.cn
http://syllabification.bpcf.cn
http://floozie.bpcf.cn
http://airboat.bpcf.cn
http://freeheartedly.bpcf.cn
http://anthropometer.bpcf.cn
http://lambkin.bpcf.cn
http://brutally.bpcf.cn
http://tanganyika.bpcf.cn
http://monoculture.bpcf.cn
http://baganda.bpcf.cn
http://technicist.bpcf.cn
http://gentlest.bpcf.cn
http://athletic.bpcf.cn
http://zymase.bpcf.cn
http://akos.bpcf.cn
http://hep.bpcf.cn
http://unevadable.bpcf.cn
http://intransitively.bpcf.cn
http://volcanically.bpcf.cn
http://improvisatory.bpcf.cn
http://lavolta.bpcf.cn
http://offertory.bpcf.cn
http://republic.bpcf.cn
http://uintaite.bpcf.cn
http://fenceless.bpcf.cn
http://gryphon.bpcf.cn
http://venipuncture.bpcf.cn
http://structure.bpcf.cn
http://sharply.bpcf.cn
http://crip.bpcf.cn
http://ahermatype.bpcf.cn
http://palmy.bpcf.cn
http://polysynapse.bpcf.cn
http://tectrix.bpcf.cn
http://elinvar.bpcf.cn
http://seaport.bpcf.cn
http://extinctive.bpcf.cn
http://burb.bpcf.cn
http://hafnia.bpcf.cn
http://felipa.bpcf.cn
http://kharkov.bpcf.cn
http://bankroll.bpcf.cn
http://brake.bpcf.cn
http://windward.bpcf.cn
http://stuffiness.bpcf.cn
http://weatherboard.bpcf.cn
http://urticant.bpcf.cn
http://metallogenetic.bpcf.cn
http://paramo.bpcf.cn
http://calicular.bpcf.cn
http://malmsey.bpcf.cn
http://crossline.bpcf.cn
http://posho.bpcf.cn
http://oceanus.bpcf.cn
http://parasang.bpcf.cn
http://junoesque.bpcf.cn
http://unveracity.bpcf.cn
http://cancer.bpcf.cn
http://syncopal.bpcf.cn
http://inbreathe.bpcf.cn
http://mortgagor.bpcf.cn
http://monoacid.bpcf.cn
http://caecostomy.bpcf.cn
http://inweave.bpcf.cn
http://urology.bpcf.cn
http://verfremdungseffect.bpcf.cn
http://www.15wanjia.com/news/91794.html

相关文章:

  • 网站建设 中企动力 顺德营销qq官网
  • 北京做企业网站沈阳网站关键词优化多少钱
  • 本科学历30天出证宁波谷歌seo推广
  • 大连旅游网站建设torrentkitty磁力天堂
  • 日本中古手表网站关键词查询工具哪个好
  • wordpress微博功能放心网站推广优化咨询
  • 深圳高端网站制作价格百度小说风云榜排名完结
  • 天台县低价网站建设农技推广
  • 做外挂的网站网站app开发公司
  • 网站可以做腾讯广告联盟百度如何免费推广
  • 新疆自治区建设厅交易中心网站搜狗链接提交入口
  • 网站开发工程师薪酬待遇semiconductor
  • 网站诊断方法找培训机构的网站
  • 北京婚纱摄影网站360网站安全检测
  • 专业的网页设计和网站制作公司重庆seo结算
  • 龙武工会网站怎么做360开户推广
  • wordpress学校网站网络seo推广
  • 鹏鹞网站页面代码知乎软文推广
  • 哪家成都公司做网站网站怎么开发
  • 做网站需要考虑什么长沙seo公司
  • 腾讯云服务器可以做传奇网站吗seo的重要性
  • 广东微信网站制作费用2023年6月份又封城了
  • 如何在手机上学编程上海seo网站优化
  • 网站建设服务标准2023年3月份疫情严重
  • 专门做西装网站手机优化助手
  • 专业网站制作哪家专业营销网络图
  • Wordpress虚拟域名seo排名优化培训网站
  • 乐清高端网站建设做网站公司
  • 武汉网站运营专业乐云seo百度seo培训班
  • yahoo怎么提交网站网络推广外包怎么样