public class PrivateOverride {
private void f() {
System.out.println("private f()");
}
}
public class Derived extends PrivateOverride {
public void f() { //this method is never run.
System.out.println("public f()");
}
}
public static void main(String[] args) {
// instantiate Derived and assign it to
// object po of type PrivateOverride.
PrivateOverride po = new Derived();
// invoke method f of object po. It
// chooses to run the private method of PrivateOveride
// instead of Derived
po.f();
}
}
Run Code Online (Sandbox Code Playgroud)
所以,这段代码的输出是 …
我在我的一个Spring配置文件中定义了以下属性文件:
<context:property-placeholder location="classpath:project.properties"/>
Run Code Online (Sandbox Code Playgroud)
现在我想从一些不在类路径中的外部属性文件中覆盖一些属性.
假设我已将项目部署到某处,我需要进行一些动态配置更改.我不想更新容器中的项目代码库(tomcat或任何东西).
1.)所以我需要一种方法,用我在外部属性文件中的最新更新来更新(覆盖)spring的加载属性文件的值.
2.)如果有人也可以共享刷新预加载属性的方法,那将会很棒.
String s1 = "String1";
System.out.println(s1.hashCode()); // return an integer i1
Field field = String.class.getDeclaredField("value");
field.setAccessible(true);
char[] value = (char[])field.get(s1);
value[0] = 'J';
value[1] = 'a';
value[2] = 'v';
value[3] = 'a';
value[4] = '1';
System.out.println(s1.hashCode()); // return same value of integer i1
Run Code Online (Sandbox Code Playgroud)
即使我在反射的帮助下更改了字符,也会保留相同的哈希码值.
这里有什么我需要知道的吗?
考虑下面的例子为什么在内部类中继承静态变量没有任何限制时,我们被限制在内部类中声明静态成员变量?
public class Outer {
public class Inner {
public static String notAllowed;
/* Above line give following compilation error
The field notAllowed cannot be declared static in a non-static inner type, unless initialized with a constant expression
*/
}
}
Run Code Online (Sandbox Code Playgroud)
但是现在如果我的内部类扩展了包含静态变量的其他类,那么这样可以正常工作.考虑以下代码:
public class Outer {
public class Inner extends InnerBase {
/* Since it extends InnerBase so we can access Outer.Inner.allowed */
public Inner(){
Outer.Inner.allowed = null; // Valid statement
}
}
}
public class …
Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
<bean id="busmessageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:bundles/resource</value>
<value>classpath:bundles/override</value>
<value>file:/C:/mmt/override</value>
</list>
</property>
<property name="cacheSeconds" value="100" />
</bean>
Run Code Online (Sandbox Code Playgroud)
这里的属性来自bundles/resource
和bundles/override
我打电话时获取busmessageSource.getMessage("anykey", null, null)
但是当我尝试获取属性的值时失败了 C:/mmt/override
file:/C:/mmt/override
覆盖值,classpath:bundles/override
如果有任何相同的键存在.如何覆盖war文件夹之外的外部文件中的属性?