Last active
June 11, 2018 09:28
-
-
Save xie-qianyue/43c3c6a87654dfdc6a744b0f8c2c32a6 to your computer and use it in GitHub Desktop.
Spring - Register bean by BeanDefinitionBuilder
This file contains 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
public class BeanDefinitionBuilderExample { | |
public static void main (String[] args) { | |
DefaultListableBeanFactory beanFactory = | |
new DefaultListableBeanFactory(); | |
BeanDefinitionBuilder b = | |
BeanDefinitionBuilder.rootBeanDefinition(MyBean.class) | |
.addPropertyValue("str", "myStringValue"); | |
beanFactory.registerBeanDefinition("myBean", b.getBeanDefinition()); | |
MyBean bean = beanFactory.getBean(MyBean.class); | |
bean.doSomething(); | |
} | |
} |
This file contains 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
public class MyBean { | |
private String str; | |
public void setStr (String str) { | |
this.str = str; | |
} | |
public void doSomething () { | |
System.out.println("from MyBean " + str); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment