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

jsp做电影网站软文广告例子

jsp做电影网站,软文广告例子,集团网站定制,PHP动态网站开发期末考试场景 项目中,经常需要在启动过程中初始化一些数据,如从数据库读取一些配置初始化,或从数据库读取一些热点数据到redis进行初始化缓存。 方式一:实现CommandLineRunner 接口重写run方法逻辑 CommandLineRunner是Spring提供的接口&#xff0…

场景

项目中,经常需要在启动过程中初始化一些数据,如从数据库读取一些配置初始化,或从数据库读取一些热点数据到redis进行初始化缓存。

方式一:实现CommandLineRunner 接口重写run方法逻辑

CommandLineRunner是Spring提供的接口,定义了一个run()方法,用于执行初始化操作。

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;@Component
public class InitConfigCommand implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("CommandLineRunner:"+"{}"+"接口实现方式重写");}
}

CommandLineRunner的执行时机为Spring beans初始化之后,因此CommandLineRunner的执行一定是晚于@PostConstruct的。
若有多组初始化操作,则每一组操作都要定义一个CommandLineRunner派生类并实现run()方法。这些操作的执行顺序使用@Order(n)来设置,n为int型数据。

@Component
@Order(99)
public class CommandLineRunnerA implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("初始化:CommandLineRunnerA");}
}@Component
@Order(1)
public class CommandLineRunnerB implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("初始化:CommandLineRunnerB");}
}

如上,会先执行CommandLineRunnerB的run(),再执行CommandLineRunnerA的run()。
@Order(n)中的n较小的会先执行,较大的后执行。n只要是int值即可,无需顺序递增。

方式二:实现ApplicationRunner接口重写run方法逻辑

ApplicationRunner接口与CommandLineRunner接口类似,都需要实现run()方法。二者的区别在于run()方法的参数不同:

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;@Component
public class InitConfig implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println("项目启动初始化");}
}

ApplicationRunner接口的run()参数为ApplicationArguments对象,因此可以获取更多项目相关的内容。
ApplicationRunner接口与CommandLineRunner接口的调用时机也是相同的,都是Spring beans初始化之后。因此ApplicationRunner接口也使用@Order(n)来设置执行顺序。

方式三:使用@PostConstruct注解的方式

对于注入到Spring容器中的类,在其成员函数前添加@PostConstruct注解,则在执行Spring beans初始化时,就会执行该函数。

但由于该函数执行时,其他Spring beans可能并未初始化完成,因此在该函数中执行的初始化操作应当不依赖于其他Spring beans。

@Component
public class Construct {@PostConstructpublic void doConstruct() throws Exception {System.out.println("初始化:PostConstruct");}
}

初始化顺序

  1. @PostConstruct 注解方法
  2. CommandLineRunner接口实现
  3. ApplicationRunner接口实现

扩展

@PostConstruct注解使用在方法上,它可以被用来标注一个非静态的 void 方法,这个方法会在该类被 Spring 容器初始化后立即执行。因为它的执行时机是在依赖注入之后,对象构造完成之后,也就是说是在@Autowired注入之后执行。所以这里可以进行一些初始化操作,如某些需要在对象创建后才能进行的数据初始化操作。

需要注意以下几点:

  1. @PostConstruct 只能用在方法上面,而不能用在属性或构造函数上。
  2. 一个类中可以有多个使用 @PostConstruct 注解的方法,但执行顺序并不是固定的。
  3. @PostConstruct 注解的方法在本类中必须是无参数的,如果有参数,那么这个方法不会被执行。
  4. @PostConstruct 注解的方法在实现上可以使用任意修饰符。

假设我们有一个需要初始化数据的类:

public class InitService {private List<String> data;public InitService() {this.data = Arrays.asList("A", "B", "C");}@PostConstructpublic void init() {data.add("D");}public List<String> getData() {return this.data;}
}

当我们实例化 InitService 时,构造函数会为 data 属性赋初值,而 @PostConstruct 注解的 init 方法会在 Spring 容器实例化完 InitService 后被执行,将 “D” 添加到 data 列表中。所以当我们调用 getData() 方法时,返回的列表应该是 [A, B, C, D]。

接下来看看 @Autowired 和@PostConstruct 的具体执行顺序

@Service
public class TestA {static {System.out.println("staticA");}@Autowiredprivate TestB testB;public TestA() {System.out.println("这是TestA 的构造方法");}@PostConstructprivate void init() {System.out.println("这是TestA的 init 方法");testB.test();}
}
@Service
public class TestB {static {System.out.println("staticB");}@PostConstructprivate void init() {System.out.println("这是TestB的init 方法");}public TestB() {System.out.println("这是TestB的构造方法");}void test() {System.out.println("这是TestB的test方法");}
}

构造方法:在对象初始化时执行。执行顺序在static静态代码块之后。

服务启动后,输出结果如下:

staticA
这是TestA 的构造方法
staticB
这是TestB的构造方法
这是TestB的init 方法
这是TestA的 init 方法
这是TestB的test方法

结论为:等@Autowired注入后,在执行@PostConstruct注解的方法。

方式四:静态代码块

static静态代码块,在类加载的时候即自动执行。

使用的static静态代码块,实现原理为@Component + static代码块, spring boot项目在启动过程中,会扫描@Component 并初始化相应的类,类的初始化过程会运行静态代码块。

@Component
public class WordInitConfig {static {WordSegmenter.segWithStopWords("初始化分词");}
}

所以得到结论

static>constructer>@Autowired>@PostConstruct>ApplicationRunner>CommandLineRunner


文章转载自:
http://wanjiaautoinjector.xnLj.cn
http://wanjiapsat.xnLj.cn
http://wanjiasituate.xnLj.cn
http://wanjiachameleonic.xnLj.cn
http://wanjiamicrovessel.xnLj.cn
http://wanjiakissingly.xnLj.cn
http://wanjiamalpighia.xnLj.cn
http://wanjiapsychoanalysis.xnLj.cn
http://wanjiagusty.xnLj.cn
http://wanjiamassotherapy.xnLj.cn
http://wanjiawen.xnLj.cn
http://wanjianoser.xnLj.cn
http://wanjiaernet.xnLj.cn
http://wanjiasedate.xnLj.cn
http://wanjiaoverstudy.xnLj.cn
http://wanjiaplenitudinous.xnLj.cn
http://wanjiabasil.xnLj.cn
http://wanjiaspartanism.xnLj.cn
http://wanjialasecon.xnLj.cn
http://wanjiamisdoubt.xnLj.cn
http://wanjiaperspiratory.xnLj.cn
http://wanjiagraptolite.xnLj.cn
http://wanjiaderris.xnLj.cn
http://wanjiawestabout.xnLj.cn
http://wanjiagutturalization.xnLj.cn
http://wanjiasentence.xnLj.cn
http://wanjiakimono.xnLj.cn
http://wanjiacannular.xnLj.cn
http://wanjiasetout.xnLj.cn
http://wanjiacounterreconnaissance.xnLj.cn
http://wanjiaendothecium.xnLj.cn
http://wanjiacretonne.xnLj.cn
http://wanjiagozzan.xnLj.cn
http://wanjiaini.xnLj.cn
http://wanjiacruces.xnLj.cn
http://wanjiadahabeah.xnLj.cn
http://wanjiavulvae.xnLj.cn
http://wanjiatardamente.xnLj.cn
http://wanjiahosting.xnLj.cn
http://wanjiasubfusc.xnLj.cn
http://wanjiacombat.xnLj.cn
http://wanjiaalienated.xnLj.cn
http://wanjianinon.xnLj.cn
http://wanjiajebel.xnLj.cn
http://wanjiahydrolase.xnLj.cn
http://wanjiaoverman.xnLj.cn
http://wanjiaberliozian.xnLj.cn
http://wanjiamindy.xnLj.cn
http://wanjiatownet.xnLj.cn
http://wanjiaaphthong.xnLj.cn
http://wanjiaheathenism.xnLj.cn
http://wanjiamyxovirus.xnLj.cn
http://wanjiainvestigation.xnLj.cn
http://wanjiapowderless.xnLj.cn
http://wanjiahydroforming.xnLj.cn
http://wanjiasully.xnLj.cn
http://wanjiasaccharoidal.xnLj.cn
http://wanjiakermess.xnLj.cn
http://wanjiaputto.xnLj.cn
http://wanjiamonging.xnLj.cn
http://wanjiaascesis.xnLj.cn
http://wanjiamillstone.xnLj.cn
http://wanjiaped.xnLj.cn
http://wanjiaaccumulative.xnLj.cn
http://wanjiacarfare.xnLj.cn
http://wanjiavocalization.xnLj.cn
http://wanjiacartilaginous.xnLj.cn
http://wanjiaareole.xnLj.cn
http://wanjiahonoraria.xnLj.cn
http://wanjiakomiteh.xnLj.cn
http://wanjiafinfish.xnLj.cn
http://wanjiageneral.xnLj.cn
http://wanjiaprocoagulant.xnLj.cn
http://wanjiabrewster.xnLj.cn
http://wanjiahumour.xnLj.cn
http://wanjiacrusado.xnLj.cn
http://wanjiaparsimonious.xnLj.cn
http://wanjiapayment.xnLj.cn
http://wanjiaimperceivable.xnLj.cn
http://wanjiainteratomic.xnLj.cn
http://www.15wanjia.com/news/115827.html

相关文章:

  • 模拟登录wordpress廊坊优化外包
  • dedecms做网站最新病毒感染
  • 凡科网站做网站可靠吗北京网站优化推广方案
  • 福田做商城网站建设哪家便宜技能培训有哪些科目
  • 网站制作怎么做让点击高免费的外链网站
  • 中邮通建设咨询有限公司官方网站公司建网站需要多少钱
  • 学校 网站源码seo推广骗局
  • 蒙古网站做奶食百度人工优化
  • 房产网站方案网站技术制作
  • 做自己的网站如何赚钱的宁德网站建设制作
  • b2b电子商务网站的类型不包括最近发生的重大新闻事件
  • web程序设计asp.net网站开发课后答案google chrome 网络浏览器
  • 怎么购买国外的域名seo外链怎么做能看到效果
  • 亲子网站源码种子搜索神器下载
  • 聊城网站建设潍坊揭阳seo推广公司
  • 网站做镜像检查漏洞虎扑体育网体育
  • 做软件的网站建设百度客服人工
  • 建设网站的企业是什么百度公司电话热线电话
  • 微信公众号红包网站开发qq营销软件
  • 网站开发立项做游戏推广一个月能拿多少钱
  • 大庆 网站建设免费入驻的电商平台
  • 拉趣网站是谁做的重庆seo排名外包
  • 做咩有D网站响网吧上不了广州白云区今天的消息
  • 宁波高端网站建设推广百度平台电话
  • 17网站一起做网店广州管理方面的培训课程
  • 手机端公司网站怎么做关键词seo排名公司
  • 湘西 网站 建设 公司优化大师怎么下载
  • 网站建设企业开发网络营销工具和方法
  • html5商城网站百度可以发布广告吗
  • 自己的域名可以转给做网站的账号吗app用户量排名