package com.writer;
import java.util.List;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.Entity.UserEntity;
import com.Service.CommonCSVService;
@Component
public class CsvItemWriter implements ItemWriter<UserEntity> {
@Autowired(required =false)
private CommonCSVService commonCSVService;
@Override
public void write(List<? extends UserEntity> users){
try{
for(UserEntity user :users){
System.out.println(user.getName()+"!!!!!!!!!!!!!!!!!!!!!");
if(commonCSVService.insertToDB(user)){
System.out.println("CsvItemWriter :"+" insert successfully!");
}else{
System.out.println("CsvItemWriter :"+" insert failed !");
}
}
}catch(Exception e){
System.out.println("CsvItemWriter: "+e);
}
}
}
我打了断点看了下这个bean commonCSVService是null,
但是
package com.Mapper;
import java.util.Map;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindException;
import com.Entity.*;
import com.Service.CommonCSVService;
import com.Util.DateUtil;
@Component
public class CsvSetMapper implements FieldSetMapper<UserEntity>{
@Autowired(required=true)
@Qualifier("commonCSVService")
private CommonCSVService commonCSVService;
int lineNumber=1;
Map<String, String> csvFileNameMap;
String csvFilePathAndName;
@Override
public UserEntity mapFieldSet(FieldSet fieldSet) throws BindException {
// TODO Auto-generated method stub
UserEntity user;
try{
csvFileNameMap =commonCSVService.getJobParams();
csvFilePathAndName =csvFileNameMap.get("csvFilePathAndName");
System.err.println(csvFilePathAndName);
//csv的结束行为9时 就不再继续读取了 可以用来统计文件的总行数(我这里不包括字段标记行)
if(fieldSet!=null&&!fieldSet.readString(0).equals("9")){
user =new UserEntity();
user.setRecordType(fieldSet.readString(0));
user.setLineNumber(lineNumber);
user.setName(fieldSet.readString(1));
user.setBirthday(DateUtil.parseDate(fieldSet.readString(2)));
user.setChildren(fieldSet.readInt(3));
lineNumber++;
return user;
}
lineNumber=1;
}catch (Exception ex){
System.out.println("mapFieldSet :"+ex);
}
return null;
}
}
这个commonCSVService 在setMapper这个阶段就有实例 不会报空指针
这是为什么 大神们 求解答
commonCSVService 注入可能存在冲突,请检查是否重复注入
这个其实不需要检查的,你在你的这个service类的注解上加上name,比如
@service("commonCSVService")
然后在这里注入的时候写
@Autowired
@Qualifier("commonCSVService")
如果还是报那个问题就证明你这个springbean中有两个相同名字的bean但是包不同,这时候一般都是修改自己的bean的name,因为你总不可能为了一个名字去修改框架中bean的name吧
b/s做不到强制load的,除非一个有个请求在做这个工作,但是显然是不可能的。你可以将权限存在一个缓存中,超管修改其他用户权限时就根据ID把缓存中的数据修改,这样当用户刷新页面是这个权限就会起作用了。