如何将 CRUDRepository 接口注入我的 util class
How to inject CRUDRepository interface to my util class
真不知道为什么我的CRUDRepository接口无法注入
我有一个小应用 Spring 启动并使用 CrudRepository:
public interface ActionLogRepository extends CrudRepository<ActionLog, Integer> {
}
在我的服务中class我有一个方法:
@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!
我的主要 class 和 @SpringBootApplication 位于包的顶部,因此 ComponentScan 问题不是这里的重点。
问题是为什么?又一笔交易?我真的不知道出了什么问题。我在 Internet 上阅读了所有类似的主题......请帮助我。谢谢。
问候
马特利
在你的ActionTrackerInterceptor
中:
MyActionLogUtil util = new MyActionLogUtil();
您正在手动创建 "Util" class 的新实例,而不是使用在应用程序启动时创建的 Spring 实例(它实际上具有对您的存储库的正确引用)。
这与事务管理无关,与核心 DI 功能有关。请务必阅读 Spring docs 以了解更多信息,了解 DI 的一般工作原理。
真不知道为什么我的CRUDRepository接口无法注入
我有一个小应用 Spring 启动并使用 CrudRepository:
public interface ActionLogRepository extends CrudRepository<ActionLog, Integer> {
}
在我的服务中class我有一个方法:
@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!
我的主要 class 和 @SpringBootApplication 位于包的顶部,因此 ComponentScan 问题不是这里的重点。
问题是为什么?又一笔交易?我真的不知道出了什么问题。我在 Internet 上阅读了所有类似的主题......请帮助我。谢谢。
问候 马特利
在你的ActionTrackerInterceptor
中:
MyActionLogUtil util = new MyActionLogUtil();
您正在手动创建 "Util" class 的新实例,而不是使用在应用程序启动时创建的 Spring 实例(它实际上具有对您的存储库的正确引用)。
这与事务管理无关,与核心 DI 功能有关。请务必阅读 Spring docs 以了解更多信息,了解 DI 的一般工作原理。