java动态更新工程配置文件|java如何动态更新一个class文件一个jar文件

|

A. java应用(非web应用)中log4j.properties/xml动态修改配置文件,无需重启,就能立即生效,如何实现

import org.apache.log4j.Logger;import org.apache.log4j.PropertyConfigurator;public class BeTestClass {static String log4jPath;static Logger log = Logger.getLogger(BeTestClass.class);static { PropertyConfigurator.configureAndWatch("log4j.properties", 1000); }public static void main(String[] args) {for (int i = 0; i < 1000; i++) {//PropertyConfigurator.configure(log4jPath);log.info("———-info");log.debug("———-debug");log.error("———-error");System.out.println("***********************");try {Thread.sleep(2000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}

B. spring如何动态加载配置文件,就是配置文件修改了,application.xml如何能读取到

项目,需要访问多个数据库,而且需要在服务器运行不重新启动的情况下,动态的修改spring中配置的数据源datasource,在网上找了很多资料,最后找到了适合我的方法,下面总结一下。 spring的配置文件是在容器启动的时候就加载到内存中的,如果手动改了application.xml,我们必须要重新启动服务器配置文件才会生效。而在spring中提供了一个类WebApplicationContext,这个类可以让你获得一些bean,可以修改内存中的信息,我就是通过这个类来实现的。下面是我具体的代码。 package com.southdigital.hospital; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.mchange.v2.c3p0.ComboPooledDataSource; public class ChangeSpringConfig extends HttpServlet { private String ipAddress = "127.0.0.1"; /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //先取得servleContext对象,提供给spring的WebApplicationUtils来动态修改applicationContext.xml ipAddress = request.getParameter("ipAddress"); System.out.println(ipAddress); ServletContext servletContext = this.getServletContext(); WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext); ComboPooledDataSource cpds = (ComboPooledDataSource) applicationContext.getBean("dataSource"); cpds.setJdbcUrl("jdbc:mysql://"+ipAddress+":3306/ssh"); } } 注意:通过这种方法修改applicationContext.xml文件的时候用c3p0,而不可以用dbcp,dbcp不支持动态修改读取到内存里面的数据。spring 3.1已经支持了。

C. JAVA中如何重新加载.properties文件,使其他引用实时改变

*Spring提供的PropertiesLoaderUtils允许您直接通过基于类路径的文件地址加载属性资源*最大的好处就是:实时加载配置文件,修改后立即生效,不必重启*/privatestaticvoidspringUtil(){Propertiesprops=newProperties();while(true){try{props=PropertiesLoaderUtils.loadAllProperties("message.properties");for(Objectkey:props.keySet()){System.out.print(key+":");System.out.println(props.get(key));}}catch(IOExceptione){System.out.println(e.getMessage());}try{Thread.sleep(5000);}catch(InterruptedExceptione){e.printStackTrace();}}}

D. java热部署:tomcat运行中,动态修改配置文件(java文件)中的static属性并生效

<Context path="/tomcatTest" reloadable="true" docBase="E:\workplace\testProject\WebRoot"/>第一个是容器里的项目path 要加/第二个参数是你的workplace的路径,一般是到webroot写个context.xml文件,放到项目的META-INF里.context.xml头部像上面那样写就可以

E. Java读取配置文件的几种方法

在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配置文件来完成,本文根据笔者工作中用到的读取配置文件的方法小小总结一下,主要叙述的是spring读取配置文件的方法。一、读取xml配置文件(一)新建一个java beanpackage chb.demo.vo;public class HelloBean {private String helloWorld;public String getHelloWorld() {return helloWorld;}public void setHelloWorld(String helloWorld) {this.helloWorld = helloWorld;}}(二)构造一个配置文件?xml version=”1.0″ encoding=”UTF-8″?!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN” “”beansbean id=”helloBean” class=”chb.demo.vo.HelloBean”property name=”helloWorld”valueHello!chb!/value/property/bean/beans(三)读取xml文件1.利用ApplicationContext context = new (“beanConfig.xml”);HelloBean helloBean = (HelloBean)context.getBean(“helloBean”);System.out.println(helloBean.getHelloWorld());2.利用FileSystemResource读取Resource rs = new FileSystemResource(“D:/software/tomcat/webapps/springWebDemo/WEB-INF/classes/beanConfig.xml”);BeanFactory factory = new XmlBeanFactory(rs);HelloBean helloBean = (HelloBean)factory.getBean(“helloBean”);System.out.println(helloBean.getHelloWorld());值得注意的是:利用FileSystemResource,则配置文件必须放在project直接目录下,或者写明绝对路径,否则就会抛出找不到文件的异常。二、读取properties配置文拿镇件这里介绍两种启敬技术:利用spring读取properties 文件和利用java.util.Properties读取(一)利用spring读取properties 文件我们还利用上面的HelloBean.java文件,构造如下beanConfig.properties文件:helloBean.class=chb.demo.vo.HelloBeanhelloBean.helloWorld=Hello!chb!属性文件中的”helloBean”名称即是Bean的别名设定,.class用于指定类来消旁粗源。然后利用org.springframework.beans.factory.support.来读取属性文件BeanDefinitionRegistry reg = new DefaultListableBeanFactory(); reader = new (reg);reader.loadBeanDefinitions(new ClassPathResource(“beanConfig.properties”));BeanFactory factory = (BeanFactory)reg;HelloBean helloBean = (HelloBean)factory.getBean(“helloBean”);System.out.println(helloBean.getHelloWorld());(二)利用java.util.Properties读取属性文件比如,我们构造一个ipConfig.properties来保存服务器ip地址和端口,如:ip=192.168.0.1port=8080则,我们可以用如下程序来获得服务器配置信息:InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(“ipConfig.properties”);Properties p = new Properties();try {p.load(inputStream);} catch (IOException e1) {e1.printStackTrace();}System.out.println(“ip:”+p.getProperty(“ip”)+”,port:”+p.getProperty(“port”));

F. java如何动态更新一个.class文件,一个.jar文件

java里面有一个叫动态代理的实现,可以说是动态生成class文件,在调用java里面的打包命令,应该可以做到。

G. WEB项目改了java文件不会自动更新,改了jsp不会自动更新

原因可能是因为改动了java文件之后,存在错误或者引用的jar包有问题专: 1.java compiler选项的设置。大多是选项漏属选,导致部分代码修改后不会重新编译。 2.java build path选项的设置,jar文件引用错误或jre版本不对等等。

H. Springboot 使用@RefreshScope 注解,实现配置文件的动态加载

实现配置文件动态读取的好处不必多说,修改配置文件后不必重启Application ,想想就开心。 合格调包侠的必备技能,从Maven仓库引入依赖的Jar包, 搞好配置文件,默认在application.yml /properties 就行,与本功能相关的配置项(采用yml格式)如下: distributed-id是自定义需要动态部署的配置文件。management:是暴露refresh接口,不加此条配置将无法启用动态加载配置文件的功能(也就是管你理解不理解,别问,加就对了)。 1.编写自定义配置文件的Java对象,一定要在类上加@RefreshScope注解 @data是lombok的注解,别的注解不多说。 2.编写你的conroller ,也一定要加@RefreshScope注解, 不加的话,呵呵,对不起,无法运行。。原因吗—在这 @RefreshScope not working – Spring Boot – Stack Overflow    我这里是返回配置文件中distributed-id.mechineId的值。3.到此为止了,简单吧😒。其实的话,第一步也可以直接在Bean的配置中心(也就是@Configuration注解的类)搞一下,效果是一样的。 使用这种写法,就可以不用在配置对象类上加@Component和@RefreshScope。 测试一下啦,启动应用前,将配置文件设置如下: 运行程序后,在浏览器输入你的测试地址,返回如下,是5没错了: 然后找到你编译后文件,修改配置文件的值,注意是 编译后的配置文件     就是下图中灰色文件的位置 修改如下:mechineId修改为4 接下来向 http://localhost:port/actuator/refresh 发送 POST 请求,get请求是无法识别的呦😘,可以看到返回了配置文件中被更改的属性 测试一下,没错了,返回值为4


赞 (0)