首页 文章 精选 留言 我的

精选列表

搜索[语义化CSS],共10000篇文章
优秀的个人博客,低调大师

Spring的component-scan可配置化

本文是基于Spring4.3.5.RELEASE的版本进行的分析和实践 在Spring的web项目里面, 如果我们需要在类似 applicationContext.xml里面加上 <context:component-scan base-package="${component.scan.package}" /> 让base-package可配置, 那应该怎么做呢? 1. 加到Jvm的启动参数里面 比如 -Dcomponent.scan.package=com.example.yours 这里面有两个不方便的地方: 1.多个属性的话, 一个个加启动参数不方便, 2.修改了属性的值,也需要一个个修改对应的启动参数 2. 加到ServletContext里面 import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Map.Entry; import java.util.Properties; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.web.context.ContextLoaderListener; public class SingleWebContextLoaderListener extends ContextLoaderListener { private static final String DEFAULT_ENCODING = "UTF-8"; private static final String[] SPRING_PROPERTIES_FILE_NAME = new String[] {"classpath:property1.properties"}; @Override public void contextInitialized(ServletContextEvent event) { initPropertySources(event.getServletContext(), SPRING_PROPERTIES_FILE_NAME); super.contextInitialized(event); } private void initPropertySources(ServletContext servletContext, String... resourcesPaths) { ResourceLoader resourceLoader = new DefaultResourceLoader(); Properties props = new Properties(); for (String location : resourcesPaths) { Resource resource = resourceLoader.getResource(location); try (InputStream is = resource.getInputStream()){ props.load(new InputStreamReader(is, DEFAULT_ENCODING)); } catch (IOException ex) { ex.printStackTrace(); } } for(Entry<Object, Object> e : props.entrySet()) { servletContext.setInitParameter((String)e.getKey(), (String)e.getValue()); } } } 然后在web.xml把原来的ContextLoaderListener 替换成SingleWebContextLoaderListener <listener> <listener-class>xx.xx.SingleWebContextLoaderListener</listener-class> </listener> 当然如果你需要用到 ${}这种变量的话, 别忘了加 <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"/> 上面说明的两种方法, 都是比较方便的, 如果你对spring的源代码比较熟悉的话, 可以自己定一个ApplicationContext, 去实现 如果大家需要了解里面的原理的话, 可以留言, 我们再开一个帖子来解释一下

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码