I have a task. I need to define a custom bean definition. For example, define my beans in a txt file and then retrieve them via applicationcontext in spring boot. I do not know what to do. Maybe someone can help me?
I created a class: @Element Public class Human{
private string name; private int age;
}
Created beans.txt
personbean1=com.example.person personbean2=com.example.person
@Configuration Public class custombeanconfig implements beandefinitionregistrypostprocessor {
@override public void postprocessbeandefinitionregistry(beandefinitionregistry registry) { try { inputstream inputstream = getclass().getclassloader().getresourceasstream("beans.txt"); bufferedreader reader = new bufferedreader(new inputstreamreader(inputstream)); string line; while ((line = reader.readline()) != null) { string[] parts = line.split("="); if (parts.length == 2) { string beanname = parts[0].trim(); string classname = parts[1].trim(); registerbean(registry, beanname, classname); } } } catch (ioexception e) { e.printstacktrace(); } } private void registerbean(beandefinitionregistry registry, string beanname, string classname) { try { class<?> beanclass = class.forname(classname); genericbeandefinition beandefinition = new genericbeandefinition(); beandefinition.setbeanclass(beanclass); registry.registerbeandefinition(beanname, beandefinition); } catch (classnotfoundexception e) { e.printstacktrace(); } }
@springbootapplication Public class bookshopapplication {
public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(BookShopApplication.class, args); Person person1 = context.getBean("personBean1", Person.class); Person person2 = context.getBean("personBean2", Person.class); System.out.println(person2); System.out.println(person1); }
}
and get this error: No bean available named 'personbean1'
@configuration @importresource(value = "classpath:config.txt", reader = mycustombeandefinitionreader.class) public class myconfiguration { }
and custom beandefinitionreader:
public class mycustombeandefinitionreader extends abstractbeandefinitionreader { public mycustombeandefinitionreader(beandefinitionregistry registry) { super(registry); } @override public int loadbeandefinitions(final resource resource) throws beandefinitionstoreexception { try { //take all you need from config.txt here string[] config = readconfig(resource.getfile()); // will return [person1='com.example.mybean', 'person2=com.example.mybean'] beandefinitionregistry registry = getregistry(); for (int i = 0; i < config.length; i++) { //get name and class string[] split = config[i].split("="); string beanname = split[0]; string beanclass = split[1]; //register bean definition genericbeandefinition beandefinition = new genericbeandefinition(); beandefinition.setbeanclassname(beanclass); registry.registerbeandefinition(beanname, beandefinition); } return config.length; // amount of registered beans } catch (exception e) { throw new beandefinitionstoreexception("error while loading beans", e); } } public static string[] readconfig(file file) throws ioexception { try (bufferedreader reader = new bufferedreader(new filereader(file))) { string line1 = reader.readline(); string line2 = reader.readline(); return new string[]{line1, line2}; } } }
Configuration.txt:
person1=com.example.mybean person2=com.example.mybean
That's it. You will get person1 and person2.
If you have classes:
public class mybean { @autowired private mynormalservice mynormalservice; public mynormalservice getmynormalservice() { return mynormalservice; } public void setmynormalservice(mynormalservice mynormalservice) { this.mynormalservice = mynormalservice; } }
You can test if spring will inject your beans into the service and all dependencies inside mybean will be handled by spring as well.
@service public class myservice { @autowired private mybean person1; @postconstruct public void test() { system.out.println("test " + person1 + " " + person1.getmynormalservice()); } }
You will get the output in the command line:
test com.example.mybean@3343997b com.example.mynormalservice@5c1dc4e9
Please note that you need to place config.txt in the resources folder.
@Configuration public class MyConfiguration { @Bean public MyCustomBean myCustomBean() { String[] config = readConfig("path/to/your/config.txt"); return new MyCustomBean(config[0], config[1]); } public static String[] readConfig(String filePath) throws IOException { try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { String line1 = reader.readLine(); String line2 = reader.readLine(); return new String[]{line1, line2}; } } }
The above is the detailed content of How to create beans in .txt?. For more information, please follow other related articles on the PHP Chinese website!