| - purpose
Design a small tool, to achieve a set of strings, create an object
| - Ideas
By way of reflection, to obtain a class attribute, attribute name string concatenation by acquiring all setter methods, these methods are not assigned
| - Code
. 1 Package com.ioc; 2 . 3 . 4 Import java.lang.reflect.Constructor; . 5 Import as java.lang.reflect.Field; . 6 Import the java.lang.reflect.Method; . 7 Import java.util.Scanner; . 8 . 9 / * * 10 * 9527 :: @auther . 11 * @Description: design of a small tool, to achieve a set of strings, creating an object 12 is * @program: inversion of control object controls inversion 13 is * @Create: 2019-10- 17:18 04 14 * / 15 public class MySpring { 16 . 17 18 is / ** . 19 * Design a way to create a control object 20 * @param className String class full name 21 * @return returns an object Object 22 * Dependency Injection Control object is someone else's, to create objects of other people at the same time, help us to automatically inject property value 23 is * / 24 public Object the getBean (String className) { 25 Object obj = null ; 26 is Scanner INPUT = new new Scanner (the System.in); 27 the try { 28 // method for obtaining a parameter passed in the corresponding class 29 class clazz = class .forName (className); 30 // create an object by clazz 31 is obj = clazz.newInstance (); 32 // here make an automatic dependency injection (DI) all attribute values assigned are the object 33 // Find all set methods for each different object, assigns them 34 / / ideas: their treatment by means of a string of splicing 35 // 1, all the properties of this class found by clazz == >> Get the name of each attribute == >> set attributes 36 Field, [] = Fields clazz.getDeclaredFields ( ); 37 [ for (Field, Field: Fields) { 38 is // Get the name attribute 39 string the fieldName = field.getName (); 40 // manual splicing string concatenation method set corresponding to the attribute name 41 // capitalized 42 FirstLetter fieldName.substring = String (0,. 1 ) .toUpperCase (); 43 is // addition Acronym other letters 44 is String otherLetter fieldName.substring = (. 1 ); 45 the StringBuilder setMethodName = new new the StringBuilder ( "SET" ); 46 is setMethodName.append (firstLetter); 47 setMethodName.append (otherLetter); 48 // Get the corresponding attribute type field - >> Looking method set time passes parameters 49 Class = the fieldType Field.getType (); 50 // by Deal strings to find the name of the method 51 is method, setMethod =clazz.getMethod (setMethodName.toString (), the fieldType); 52 is // claim assignment 53 is System.out.println ( "please" + fieldName + "attribute provides value" ); 54 is String value = input.nextLine (); 55 / / value of the attribute file is read by all the Scanner type String 56 // when performing set method, the method requires a value of type String not necessarily, the values for a conversion of a value of type String attribute type 57 // except char, may be processed using a constructor String class of the packaging 58 the constructor fieldType.getConstructor CON = (String. class ); // constructor corresponding to the attribute 59 60 // perform a method 61 setMethod.invoke(obj,con.newInstance(value)); 62 } 63 64 65 } catch (Exception e) { 66 e.printStackTrace(); 67 } 68 return obj; 69 } 70 71 }
1 package com.ioc; 2 3 /** 4 * @auther::9527 5 * @Description: 测试反射 6 * @program: jstl2 7 * @create: 2019-10-04 18:13 8 */ 9 public class Preson { 10 private Integer age; 11 private String name; 12 private String sex; 13 14 public Preson() { 15 } 16 17 public Preson(Integer age, String name, String sex) { 18 this.age = age; 19 this.name = name; 20 this.sex = sex; 21 } 22 23 @Override 24 public String toString() { 25 return "Preson{" + 26 "age=" + age + 27 ", name='" + name + '\'' + 28 ", sex='" + sex + '\'' + 29 '}'; 30 } 31 32 public Integer getAge() { 33 return age; 34 } 35 36 public void setAge(Integer age) { 37 this.age = age; 38 } 39 40 public String getName() { 41 return name; 42 } 43 44 public void setName(String name) { 45 this.name = name; 46 } 47 48 public String getSex() { 49 return sex; 50 } 51 52 public void setSex(String sex) { 53 this.sex = sex; 54 } 55 }
1 package com.ioc; 2 3 /** 4 * @auther::9527 5 * @Description: 实现控制反转的javaBean 6 * @program: jstl2 7 * @create: 2019-10-04 17:27 8 */ 9 public class Question { 10 private String title; 11 private String answer; 12 13 public Question() { 14 } 15 16 public Question(String title, String answer) { 17 this.title = title; 18 this.answer = answer; 19 } 20 21 public String getTitle() { 22 return title; 23 } 24 25 public void setTitle(String title) { 26 this.title = title; 27 } 28 29 public String getAnswer() { 30 return answer; 31 } 32 33 public void setAnswer(String answer) { 34 this.answer = answer; 35 } 36 37 @Override 38 public String toString() { 39 return "Question{" + 40 "title='" + title + '\'' + 41 ", answer='" + answer + '\'' + 42 '}'; 43 } 44 }
. 1 Package com.ioc; 2 . 3 / ** . 4 * 9527 :: @auther . 5 * @Description: . 6 * @program: jstl2 . 7 * @Create: 2019-10-04 17:29 . 8 * / . 9 public class the testMain { 10 public static void main (String [] args) { . 11 // creates a Question object control object is to invert the handling MySpring 12 // test Question 13 is MySpring = Spring new new MySpring (); 14 Question question = (Question) spring.getBean ( " com.ioc.Question"); 15 System.out.println(question); 16 //测试一下Preson 17 Preson p = (Preson) spring.getBean("com.ioc.Preson"); 18 System.out.println(p); 19 } 20 }
| - operating results