我有一個任務。我需要定義一個自訂 bean 定義。例如,在txt檔案中定義我的bean,然後透過spring boot中的applicationcontext檢索它們。我不知道該怎麼做。也許有人可以幫助我?
我創建了一個類別: @成分 公共類別人{
private string name; private int age;
}
建立了 beans.txt
personbean1=com.example.person personbean2=com.example.person
@配置 公共類別 custombeanconfig 實作 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 公共類別 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); }
}
並出現此錯誤:沒有名為「personbean1」的可用 bean
@configuration @importresource(value = "classpath:config.txt", reader = mycustombeandefinitionreader.class) public class myconfiguration { }
和自訂 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}; } } }
配置.txt:
person1=com.example.mybean person2=com.example.mybean
就是這樣。您將獲得 person1 和 person2。
如果你有課:
public class mybean { @autowired private mynormalservice mynormalservice; public mynormalservice getmynormalservice() { return mynormalservice; } public void setmynormalservice(mynormalservice mynormalservice) { this.mynormalservice = mynormalservice; } }
您可以測試spring是否會將您的bean注入到該服務中,並且mybean內的所有依賴項也將由spring處理。
@service public class myservice { @autowired private mybean person1; @postconstruct public void test() { system.out.println("test " + person1 + " " + person1.getmynormalservice()); } }
您將在命令列中得到輸出:
test com.example.mybean@3343997b com.example.mynormalservice@5c1dc4e9
請注意,您需要將 config.txt 放在 resources 資料夾中。
@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}; } } }
以上是如何在.txt 中建立bean?的詳細內容。更多資訊請關注PHP中文網其他相關文章!