InitializingBean afterPropertiesSet

简介: Person[name=Jack,age=10] style==null:Person@deb6432[name=Jack,age=10,password=Jack'sPassword] SHORT_PREFIX_STYLE:Person[name=Jack,age=10,passwo...

 

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方法

 

 

相关文章
|
5月前
|
Java Spring
ApplicationContextAware 的理解和应用
ApplicationContextAware 的理解和应用
49 2
|
4月前
ApplicationContextAware
ApplicationContextAware
20 0
|
Java Spring 容器
@PostConstruct使用
@PostConstruct使用
67 0
|
Java 开发者 Spring
BeanFactory和FactoryBean
BeanFactory和FactoryBean
96 0
|
Java Spring 容器
通过实现ApplicationContextAware接口获取Bean
通过实现ApplicationContextAware接口获取Bean
151 0
|
缓存 Java API
注解@PostConstruct与@PreDestroy详解及实例
注解@PostConstruct与@PreDestroy详解及实例
678 0
注解@PostConstruct与@PreDestroy详解及实例
|
Java Spring 容器
Spring源码解析 - FactoryBean&&BeanFactory(中)
Spring源码解析 - FactoryBean&&BeanFactory
176 0
Spring源码解析 - FactoryBean&&BeanFactory(中)
|
XML JSON Java
Spring源码解析 - FactoryBean&&BeanFactory(上)
Spring源码解析 - FactoryBean&&BeanFactory
132 0
Spring源码解析 - FactoryBean&&BeanFactory(上)
|
XML Java Maven
BeanFactory 和 FactoryBean 的理解
大家好,我是本周的值班编辑 江南一点雨 ,本周将由我为大家排版并送出技术干货,大家可以在公众号后台回复“springboot”,获取最新版 Spring Boot2.1.6 视频教程试看。 我们或多或少都会经历过若干个面试,而每每问到 Spring 的时候都少不了IOC容器的影子,那么这个 IOC 容器是什么呢? 本篇文章就来了解一下 BeanFactory 和 FactoryBean