Person[name=Jack,age=10] style==null:Person@deb6432[name=Jack,age=10,password=Jack'sPassword] SHORT_PREFIX_STYLE:Person[name=Jack,age=10,password=Jack'sPassword] SIMPLE_STYLE:Jack,10,Jack'sPassword MULTI_LINE_STYLE:Person@deb6432[ name=Jack age=10 password=Jack'sPassword ] NO_FIELD_NAMES_STYLE:Person@deb6432[Jack,10,Jack'sPassword]
import org.apache.commons.lang3.builder.ReflectionToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import java.lang.reflect.Field; import java.util.Objects; public class StringUtilsDemo { public static void main(String[] args) { Person person = new Person(); person.setName("Jack"); person.setPassword("Jack'sPassword"); person.setAge((short) 10); System.out.println("" + person); System.out.println("style==null:" + ReflectionToStringBuilder.toString(person)); System.out.println("SHORT_PREFIX_STYLE:" + ReflectionToStringBuilder.toString(person, ToStringStyle.SHORT_PREFIX_STYLE));//截去了包名 System.out.println("SIMPLE_STYLE:" + ReflectionToStringBuilder.toString(person, ToStringStyle.SIMPLE_STYLE)); System.out.println("MULTI_LINE_STYLE:" + ReflectionToStringBuilder.toString(person, ToStringStyle.MULTI_LINE_STYLE)); System.out.println("NO_FIELD_NAMES_STYLE:" + ReflectionToStringBuilder.toString(person, ToStringStyle.NO_FIELD_NAMES_STYLE)); } } class Person { private String name; private Short age; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public Short getAge() { return age; } public void setAge(Short age) { this.age = age; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) { @Override protected boolean accept(Field field) { return super.accept(field) && !Objects.equals(field.getName(), "password"); } }.toString(); } }
package org.test.InitializingBean; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 测试 spring的 InitializingBean 接口 * @author Administrator * */ public class InitializingBeanTest { /** * @param args */ public static void main(String[] args) { ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml", InitializingBeanTest.class); ctx.start(); } }
package org.test.InitializingBean; import org.springframework.beans.factory.InitializingBean; /** * @see {@link org.springframework.beans.factory.InitializingBean#afterPropertiesSet} * @author Administrator * */ public class User implements InitializingBean { public User() { System.out.println(">>>>>>> User 默认的构造函数执行了"); } @Override public void afterPropertiesSet() throws Exception { System.out.println(">>>>>>> 执行了 afterPropertiesSet 方法"); } }
执行的结果如下图所示。可以看到当执行完User默认的构造函数之后,就会调用该类实现afterPropertiesSet方法