Created
April 11, 2022 05:59
-
-
Save bxb100/338948bcba6fdb252cfb26287fb51d84 to your computer and use it in GitHub Desktop.
Spring boot post process 后置处理器以及 Scope 代码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "}"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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