【发布时间】:2018-05-27 09:47:30
【问题描述】:
我真的不知道为什么我的 CRUDRepository 接口无法注入。
我在 Spring Boot 上有一个小应用程序并使用 CrudRepository:
public interface ActionLogRepository extends CrudRepository<ActionLog, Integer> {
}
在我的服务类中,我有一个方法:
@Service
public class ActionRecordServiceImpl implements ActionRecordService {
@Override
@Transactional
public void deleteActionRecord(Date date, String domain) {
Set<Action> actions= myActions...(the code delibrately shortened)
for (Action a : actions) {
actionRepository.delete(a);
}
}
}
}
接下来我有我的拦截器 - 我想捕获所有删除操作并将这些日志保存在数据库中:
public class ActionTrackerInterceptor extends EmptyInterceptor {
// called when record is deleted.
public void onDelete(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types) {
Action act = (Action) entity;
String action = "DELETED";
ActionLog actionLog = new ActionLog(...some data inserted...);
MyActionLogUtil util = new MyActionLogUtil();
util.logIt("DELETED", actionLog);
actionLogRepository.save(actionLog);
System.out.println("Record " + entity + " is deleted.");
}
而我的方法 util.LogIt 看起来像:
@Component
public class MyActionLogUtilImpl implements ActionyLogUtil {
private ActionLogRepository actionLogRepository;
@Autowired
public void setActionLogRepository(ActionLogRepository actionLogRepository) {
this.actionLogRepository = actionLogRepository;
}
@Override
public void logIt(String action, ActionLog entity) {
try {
ActionLog actionLog = null; // = new ActionLog(...some data inserted...)
actionLogRepository.save(actionLog);
} finally {
}
}
}
当我的应用程序启动时,MyActionLogUtilImpl 中的方法 setActionLogRepository 被设置并且 ActionLogRepository 被正确注入(不是空值)。但是在调试的时候,在方法 MyActionLogUtilImpl.LogIt IS MY actionLogRepository NULL!
我的 @SpringBootApplication 主类位于包的顶部,因此 ComponentScan 问题不是这里的重点。
问题是为什么?另一笔交易?我真的不知道有什么问题。我已经阅读了互联网上所有类似的主题......请帮助我。谢谢。
问候 马特利
【问题讨论】:
标签: hibernate spring-boot spring-data-jpa autowired interceptor