Use annotations to convert beans to csv and beancsv
Csv files are
Aaa, bbb, ccc
Aaa, bbb, ccc
Saved
The requirement here is to convert a List <T> Type Linear table into a format similar to the table in html, that is, the first line is followed by the body
The effect of using annotations is as follows:
List <User> users = new ArrayList <User> (); users. add (new User ("Liu xiannan", 23, "male"); users. add (new User ("Liu xiannan", 23, "male"); users. add (new User ("Liu xiannan", 23, "male"); writeBeanToCsvFile ("D: \ test.csv", users );
Csv file:
The bean user is as follows:
Package bean; import annotation.csv Field; public class User {@ csvField ("name") private String name; @ csvField ("age") private Integer age; private String sex; public User (String name, Integer age, String sex) {super (); this. name = name; this. age = age; this. sex = sex;} public String getName () {return name;} public void setName (String name) {this. name = name;} public Integer getAge () {return age;} public void setAge (Integer age) {this. age = age;} public String getSex () {return sex;} public void setSex (String sex) {this. sex = sex ;}}
The result is
Fields with @ csvField annotation will be written into the csv file.
And the value of @ csvField is used as the title of the csv file, that is, the first line.
We use reflection to achieve this effect.
Bean to List <String []> is as follows:
Private static <T> List <String []> getStringArrayFromBean (List <T> beans) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {if (beans. size () <1) throw new IllegalArgumentException ("length cannot be less than 1"); List <String []> result = new ArrayList <String []> (); Class <? Extends Object> cls = beans. get (0 ). getClass (); // obtain the generic Field [] declaredFields = cls. getDeclaredFields (); List <Field> annoFields = new ArrayList <Field> (); for (int I = 0; I <declaredFields. length; I ++) {// filter fields with annotations csvField anno = declaredFields [I]. getAnnotation (csvField. class); // obtain the annotation if (anno! = Null) annoFields. add (declaredFields [I]);} String [] title = new String [annoFields. size ()]; for (int I = 0; I <annoFields. size (); I ++) {title [I] = declaredFields [I]. getAnnotation (csvField. class ). value (); // obtain the annotation value} result. add (title); for (T each: beans) {String [] item = new String [annoFields. size ()]; for (int I = 0; I <annoFields. size (); I ++) {String fieldName = annoFields. get (I ). getName (); String methodName = "get" + fieldName. substring (0, 1 ). toUpperCase () + fieldName. substring (1); Method method = each. getClass (). getMethod (methodName, null); String val = method. invoke (each, null ). toString (); item [I] = val;} result. add (item) ;}return result ;}
Then use the csv toolkit javacsv. jar to write the csv file.
Public static <T> void destroy (String csvFilePath, List <T> beans) throws IOException, NoSuchMethodException, SecurityException, IllegalAccessException, expiration, InvocationTargetException {File file = new File (csvFilePath ); if (! File. exists () {// if the file does not exist, create a file. createNewFile ();} CsvWriter wr = new CsvWriter (csvFilePath, ',', Charset. forName ("GBK"); List <String []> contents = getStringArrayFromBean (beans); for (String [] each: contents) {wr. writeRecord (each);} wr. close ();}
How does spring use annotation to inject beans to common classes?
@ Service ("personManager") is an annotation, which is processed by the processor during spring initialization!
The service annotation operation is specified in the annotation: First, find the id with the same name in the bean. xml file according to the name.
Instantiation. If it does not exist, it seems that you want to find the same type (this is forgotten); otherwise, an exception is reported!
How does jsp obtain bean objects when spring25 uses annotation?
WebApplicationContext ctx = WebApplicationContextUtils. getRequiredWebApplicationContext (request). getSession (). getServletContext ());
AdvertiseService advertiseService = (AdvertiseService) ctx. getBean ("advertiseService ");
This is to call a service in the container