Skip to content

Instantly share code, notes, and snippets.

@bxb100
Created April 11, 2022 05:59
Show Gist options
  • Save bxb100/338948bcba6fdb252cfb26287fb51d84 to your computer and use it in GitHub Desktop.
Save bxb100/338948bcba6fdb252cfb26287fb51d84 to your computer and use it in GitHub Desktop.
Spring boot post process 后置处理器以及 Scope 代码
package com.pers.test.post;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@Slf4j
public class BeanModifier implements BeanFactoryPostProcessor {
private static final AtomicInteger integer = new AtomicInteger(0);
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
try {
BeanDefinition welcomerDef = beanFactory.getBeanDefinition("WC");
welcomerDef.getPropertyValues().add("welcomeText", "Good" + integer.incrementAndGet());
log.info("执行了 {} 次", integer.get());
} catch (Exception e) {
log.error("An error occurred on setting welcomeText", e);
}
}
}
package com.pers.test.post;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author John Bi
*/
@SpringBootApplication
public class Starter implements CommandLineRunner {
@Autowired
private BeanFactory beanFactory;
public static void main(String[] args) {
SpringApplication.run(Starter.class, args);
}
@Override
public void run(String... args) throws Exception {
for (int i = 0; i < 10; i++) {
Welcome welcome = (Welcome)beanFactory.getBean("WC");
System.out.println(welcome.hashCode());
System.out.println(welcome.getWelcomeText());
}
}
}
package com.pers.test.post;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
/**
* @author John Bi
*/
@Slf4j
public class Welcome {
@Value("${welcomeText:Good morning}")
private String welcomeText;
public void initWelcomer() {
log.info("Welcomer is initialized, {}", welcomeText);
}
public String getWelcomeText() {
return this.welcomeText;
}
public void setWelcomeText(String welcomeText) {
log.info("Setting welcomeText to: " + welcomeText);
this.welcomeText = welcomeText;
}
@Override
public String toString() {
return "Welcomer {text: " + this.welcomeText + "}";
}
}
package com.pers.test.post;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
/**
* @author John Bi
*/
@Configuration
public class WelcomeConfiguration {
@Bean(initMethod = "initWelcomer", name = "WC")
@Scope(scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE )
public Welcome welcome() {
return new Welcome();
}
@Bean
public BeanModifier beanModifier() {
return new BeanModifier();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment