Saya ada tugas. Saya perlu menentukan definisi kacang tersuai. Sebagai contoh, tentukan kacang saya dalam fail txt dan kemudian dapatkannya melalui konteks aplikasi dalam but musim bunga. Tak tahu nak buat apa. Mungkin seseorang boleh membantu saya?
Saya mencipta kelas: @Elemen manusia kelas awam {
private string name; private int age;
}
Created beans.txt
personbean1=com.example.person personbean2=com.example.person
@config Custombeanconfig kelas awam melaksanakan 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 Aplikasi kedai buku kelas awam {
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); }
}
dan mendapat ralat ini: Tiada kacang tersedia bernama 'personbean1'
@configuration @importresource(value = "classpath:config.txt", reader = mycustombeandefinitionreader.class) public class myconfiguration { }
dan pembaca definisi bean tersuai:
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
Itu sahaja. Anda akan mendapat person1 dan person2.
Jika anda mempunyai kelas:
public class mybean { @autowired private mynormalservice mynormalservice; public mynormalservice getmynormalservice() { return mynormalservice; } public void setmynormalservice(mynormalservice mynormalservice) { this.mynormalservice = mynormalservice; } }
Anda boleh menguji sama ada musim bunga akan menyuntik kacang anda ke dalam perkhidmatan dan semua kebergantungan di dalam mybean akan dikendalikan oleh musim bunga juga.
@service public class myservice { @autowired private mybean person1; @postconstruct public void test() { system.out.println("test " + person1 + " " + person1.getmynormalservice()); } }
Anda akan mendapat output dalam baris arahan:
test com.example.mybean@3343997b com.example.mynormalservice@5c1dc4e9
Sila ambil perhatian bahawa anda perlu meletakkan config.txt dalam folder sumber.
@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}; } } }
Atas ialah kandungan terperinci Bagaimana untuk mencipta kacang dalam .txt?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!