技术标签: springcloud spring boot bean Initializing
当一个类实现了这个接口之后,这个类就可以方便的获得ApplicationContext对象(spring上下文),Spring发现某个Bean实现了ApplicationContextAware接口,Spring容器会在创建该Bean之后,自动调用该Bean的setApplicationContext(参数)方法,调用该方法时,会将容器本身ApplicationContext对象作为参数传递给该方法。
package com.lyj.demo.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* @author 凌兮
* @date 2020/5/18 15:39
* 全局上下文
*/
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
public static ApplicationContext getApplicationContext(){
return context;
}
/**
* 通过name获取 Bean
* @param name beanName
* @return Object
*/
public static Object getBean(String name){
return getApplicationContext().getBean(name);
}
public static <T> T getBean(Class<T> requiredType) throws BeansException{
return getApplicationContext().getBean(requiredType);
}
}
当一个类实现这个接口之后,Spring启动后,初始化Bean时,若该Bean实现InitialzingBean接口,会自动调用afterPropertiesSet()方法,完成一些用户自定义的初始化操作。
package com.lyj.demo.studySpringBoot.init;
import com.lyj.studySpringBoot.entity.Student;
import org.springframework.beans.factory.InitializingBean;
public class SpringBeanInit implements InitializingBean {
private Integer id;
private String name;
private Integer age;
private boolean sex;
private Student student;
/** 这里进行优先调用初始化一些参数
*/
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("this is bean init set student data");
Student student = new Student(id,name,age,sex);
this.student = student;
}
public void testInit(){
System.out.println("this is bean web.xml init-method invock");
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
}
同样配置Bean的时候使用init-method也可以实现类似的操作
<bean id = "springBeanInit02" class = "com.lyj.studySpringBoot.init.SpringBeanInit" init-method="testInit">
<property name="id" value="#{1111111}" />
<property name="name" value="${test.springEL}" />
<property name="age" value="#{10+8}" /> // SpringEL表达式
<property name="sex" value="false" />
</bean>
在spring初始化bean的时候,如果该bean是实现了InitializingBean接口,并且同时配置文件中指定了init-method,系统则是先调用afterPropertiesSet方法,然后在调用init-method中指定的方法。
Spring是通过反射来调用init-method指定方法,而实现InitializingBean接口是直接调用afterPropertiesSet方法,所以后者效率高,但使用init-method方式减少了对Spring的依赖
如果调用afterPropertiesSet方法时报错,则不会再调用init-method指定的方法
文章浏览阅读495次。设计,是一门庞大的学科它包罗万象,涉及生产、生活各个领域广义的设计分为:传统工艺品设计工业设计,视觉传达设计,环境艺术设计数字艺术设计,建筑设计,服装设计等等对于设计师来说,构思和创意固然重要提高工作效率,网站资源也是必不可少大概包括素材网站模板、灵感网站两大类资源一、素材网站模板:www.uppsd.com 优图网 电商美工必收藏的网站。www.sotu114.com 搜图114 专供PNG免扣素材的图片网站www.sucai63.com 素材路上 新媒_行业市场需求网站
文章浏览阅读1.6k次。我的版本 copy-webpack-plugin: ^6.0.3const CopyWebpackPlugin = require('copy-webpack-plugin');plugins: [ new CopyWebpackPlugin({ patterns: [{ from: path.join(__dirname,'../static'), to: 'static', globOptions: {_webpack4 使用copy-webpack-plugin哪个版本
文章浏览阅读4w次,点赞79次,收藏156次。WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘ProxyError(‘Cannot connect to proxy.’, OSError(0, ‘Error’))’:问题描述:pip 无论下载什么包的时候,就会出现4次:WARNING: Retrying (Retry(total=4, connect=None, r_warning: retrying (retry(total=4, connect=none, read=none, redirect=none, st
文章浏览阅读6k次,点赞4次,收藏32次。类图是描述类、接口以及它们之间的静态关系图;本文主要介绍如何使用PlantUML 绘制类图_plantuml画类图
文章浏览阅读982次。猫狗案例分析,实例及测试class Test05_Animal { public static void main(String[] args) { Cat c1 = new Cat("小蓝", 4); System.out.println(c1.getColor() + "..." + c1.getLeg()); c1.eat(); ..._定义猫类cat。属性:毛的颜色color,品种breed。行为:吃饭eat(),抓老鼠catchmouse()
文章浏览阅读860次。最近学习做android的游戏开发时候,发现一个关于android退出时不能彻底关闭的问题,比如:一个程序里new 出了N多个Thread,这样在退出程序的可能不能完全关闭,最后发现,只用finish()方法,有时候不能彻底退出,个人感觉还是要在适当的地方加上:System.exit(0);-=====-=-=-=-=-=======-----===== 1. finish_android+os怎么关闭
文章浏览阅读3.1k次,点赞2次,收藏8次。SLASLA:service level agreements服务等级协议cisco IOS SLA让用户可以监测两台思科路由器之间或思科路由器与一个远程IP设备之间的网络性能。Cisco公司提出来的一个用于测量网络质量的方法。IP SLA用途SLA监测网络性能监测网络服务评估端到端的可用性监测网络故障诊断MPLS网络监测VOIP网络监测IP SLA优点增强部署新应用的信..._ip sla
文章浏览阅读8.1k次。题目给定一个只包含正整数的非空数组。是否可以将这个数组分割成两个子集,使得两个子集的元素和相等。注意:每个数组中的元素不会超过 100数组的大小不会超过 200示例 1:输入: [1, 5, 11, 5]输出: true解释: 数组可以分割成 [1, 5, 5] 和 [11]. 示例 2:输入: [1, 2, 3, 5]输出: false解释: 数组不能分割成两个元素和相等的子集.代码class Solution { public boolean _分割等和子集 给定一个只包含正整数的非空数组。是否可以将这个数组分割成两个子
文章浏览阅读3.8k次。1.下载并安装WDK76002.打开开始菜单Windows Driver Kits\WDK 7600.16385.1\...3.(配置临时环境路径)新建setIncludeXP.bat编辑内容 其中paths值为wdk安装文件对应的一个是头文件,一个是库文件位置set paths=D:\WinDDK\7600.16385.1\increm set include=%inclu_windows怎么通过命令行编译驱动程序
文章浏览阅读8.4k次。有时候我们可以通过uboot的tftp服务下载内核镜像并运行,从而完成对内核驱动的调试;本文将介绍这种方式的环境搭建:一、在宿主机端配置tftp服务1、安装$ apt-get install tftp-hpa tftpd-hpa xinetd注:在此说明,tftp-hpa和tftpd-hpa为tftp的客户端和服务端的软件包,而这两个软件包在之前的ubuntu版本中是tftp tf_boot镜像文件下载
文章浏览阅读268次。MVCC多版本并发控制机制什么是MVCCMVCC的作用MVCC还涉及到RR、RC等相关问题什么是MVCCMVCC,全称Multi-Version Concurrency Control,即多版本并发控制。MVCC是一种并发控制的方法,一般在数据库管理系统中,实现对数据库的并发访问,在编程语言中实现事务内存。MVCC的作用Mysql在可重复读隔离级别下如何保证事务较高的隔离性,同样的sql查询语句在一个事务里多次执行查询结果相同,就算其它事务对数据有修改也不会影响当前事务sql语句的查询结果。这_mysql mvcc管理
文章浏览阅读3k次,点赞7次,收藏12次。出现的问题:一个最简单的办法:直接换源:可以换我推荐的源,阿里云的这个源步骤:1.sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak #备份2.把sources.list的内容全部删掉,把以下的内容复制进去sudo vim /etc/apt/sources.list #修改//阿里云源deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe _w: ignoring provides line with depcompareop for package libreoffice-l10n