Centos7已配置环境变量,javac命令执行无效
在centos7中以rpm包安装jdk无需配置环境变量, terminal中输入java -verison及java 命令也是没问题的,但是javac的话就会提示没有此命令 此时我们用yum来装原生的就行了: 使用 yum install java-devel (非管理员账号使用 sudo yum install java-devel命令) 下载安装完毕后,再次输入 javac ,ok了
在之前终于是把项目跑起来了,也是踩了不少的坑,把bug还原了一遍又一遍,希望能看的更清楚。现在我们跳过tomcat再来看看spring是如何启动的。
# 首先我们在web.xml的配置了这么一个lisenner
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
# 从源码中找到该类,类中的方法都是调用父类来实现的,如此明显的装饰者模式。
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
.....
}
# 再看看ContextLoader这个类
public class ContextLoader {
# 这个静态方法只为加载一个ContextLoader.properties文件,不出所料的这个文件跟类在同一个文件夹下。
# 文件中只有一句话 -> org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext
static {
try {
ClassPathResource resource = new ClassPathResource("ContextLoader.properties", ContextLoader.class);
defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
} catch (IOException var1) {
throw new IllegalStateException("Could not load 'ContextLoader.properties': " + var1.getMessage());
}
currentContextPerThread = new ConcurrentHashMap(1);
}
# 这个方法大致就是如果web.xml没有配置特殊的contextClass则会使用defaultStrategies中指定的类,也就是上面静态方法加载到的配置 XmlWebApplicationContext
protected Class<?> determineContextClass(ServletContext servletContext) {
String contextClassName = servletContext.getInitParameter("contextClass");
if (contextClassName != null) {
try {
return ClassUtils.forName(contextClassName, ClassUtils.getDefaultClassLoader());
} catch (ClassNotFoundException var4) {
throw new ApplicationContextException("Failed to load custom context class [" + contextClassName + "]", var4);
}
} else {
# 如果在web.xml中并未指定contextClass
# 简单写成: contextClassName = "org.springframework.web.context.support.XmlWebApplicationContext"
contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());
try {
return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());
} catch (ClassNotFoundException var5) {
throw new ApplicationContextException("Failed to load default context class [" + contextClassName + "]", var5);
}
}
}
protected WebApplicationContext createWebApplicationContext(ServletContext sc) {
# 将里面代码简单写成:
# return (ConfigurableWebApplicationContext)BeanUtils.instantiateClass(this.determineContextClass(sc));
}
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
....
} else {
.....
# 此时就要开始创建 WebApplicationContext 了
try {
if (this.context == null) {
# 我们假定 this.context 的类型就是 XmlWebApplicationContext
this.context = this.createWebApplicationContext(servletContext);
}
if (this.context instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext)this.context;
if (!cwac.isActive()) { # 刚跑起的项目context一般都没被激活
if (cwac.getParent() == null) {
# 很奇怪,loadParentContext返回的是 return null ??
ApplicationContext parent = this.loadParentContext(servletContext);
cwac.setParent(parent);
}
# 这个方法主要是设置contextId,若没有在web.xml中指定,则会像这个样子:
# org.springframework.web.context.WebApplicationContext:${contextPath}
this.configureAndRefreshWebApplicationContext(cwac, servletContext);
}
}
# 这个才是Lisener的关键,把创建的WebApplicationContext加入到servletContext中。
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
if (ccl == ContextLoader.class.getClassLoader()) {
currentContext = this.context;
} else if (ccl != null) {
currentContextPerThread.put(ccl, this.context);
}
return this.context;
.....
}
}
}
# 在XWAC中提供了默认的配置文件位置
public class XmlWebApplicationContext extends AbstractRefreshableWebApplicationContext {
public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";
public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/";
public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml";
.....
}
小结一下: tomcat启动时会给配置在web.xml中指定的lisener传递一个事件,让其初始化创建一个WebApplicationContext对象,并将其加入到ServletContext中。而在spring-web中ContextLoaderListener类为我们提供使用默认或自定义两种生成WebApplicationContext对象,需要使用自定义则需要在web.xml中指定contextClass属性,并且创建对应的类。创建好了context之后,下一步便是加载applicationContext中的bean了,暂时也还没有发现如何发起加载bean的过程,继续学习吧。。。
微信关注我们
转载内容版权归作者及来源网站所有!
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。
Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。
Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。
Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。