使用註解實現 bean 轉 csv,beancsv
csv 檔案是以
aaa,bbb,ccc
aaa,bbb,ccc
儲存的
這裡的要求是將 List<T> 類型的線性表 轉化成 類似 html 中 table的格式,即第一行是 head 後面是 body
使用註解的效果如下 :
List<User> users=new ArrayList<User>(); users.add(new User("劉夏楠", 23, "男"));users.add(new User("劉夏楠", 23, "男"));users.add(new User("劉夏楠", 23, "男"));writeBeanToCsvFile("D:\\test.csv", users);
csv檔案:
其中bean user 如下:
package bean;import annotation.csvField;public class User {@csvField("姓名")private String name;@csvField("年齡")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;}}
達到的效果就是
擁有 @csvField 註解的欄位才會被 寫入csv檔案
並且 @csvField 的值作為csv檔案的 title 即 第一行
我們使用反射來達到這個效果
bean 轉 List<String[]> 如下:
private static <T> List<String[]> getStringArrayFromBean(List<T> beans) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{if(beans.size()<1)throw new IllegalArgumentException("長度不能小於1");List<String[]> result=new ArrayList<String[]>();Class<? extends Object> cls=beans.get(0).getClass();//擷取泛型型別Field[] declaredFields=cls.getDeclaredFields();List<Field> annoFields=new ArrayList<Field>();for(int i=0;i<declaredFields.length;i++){//篩選出擁有註解的欄位csvField anno=declaredFields[i].getAnnotation(csvField.class);//擷取註解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();//擷取註解的值}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;}然後使用 csv 的一個工具包 javacsv.jar來寫入csv檔案
public static <T> void writeBeanToCsvFile(String csvFilePath,List<T> beans) throws IOException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{File file =new File(csvFilePath);if(!file.exists()){//如果檔案不存在,建立檔案file.createNewFile();} CsvWriter wr =new CsvWriter(csvFilePath,',', Charset.forName("GBK")); List<String[]> contents=getStringArrayFromBean(beans); for(String[] each:contents){ wr.writeRecord(each); } wr.close();}
spring 怎用註解的方式給普通類注入bean
@Service("personManager")是一個註解,spring在初始化的時候處理器會處理該註解!
註解中規定了service註解的操作:首先根據名字去bean.xml檔案中找到名字相同的id進行
執行個體化,如果沒有找到好像要找類型相同的(這個忘了),否則報異常!
spring25 使用註解的時jsp怎獲得bean對象
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext((request).getSession().getServletContext());
AdvertiseService advertiseService = (AdvertiseService)ctx.getBean("advertiseService");
這個是在容器裡調 一個service的