- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我使用springMvc和mybatis。从项目中复制BaseService和BaseServiceImpl。
public interface BaseService<Record, Example> {
//init mybatis mapper
void initMapper();
}
BaseServiceImpl
public abstract class BaseServiceImpl<Mapper, Record, Example> implements BaseService<Record, Example> {
public Mapper mapper;
@Override
public void initMapper() {
this.mapper = SpringContextUtil.getBean(getMapperClass());
}
public Class<Mapper> getMapperClass() {
return (Class<Mapper>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
EntityServiceImpl
@Service
@Transactional
@BaseService
public class EntityServiceImpl extends BaseServiceImpl<EntityMapper, Entity, EntityExample> implements EntityService {
}
我初始化 BaseService 使用此代码。
@Component
public class ApplicationContextListener implements ApplicationListener<ContextRefreshedEvent> {
private static final Logger LOGGER = Logger.getLogger(ApplicationContextListener.class);
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
// root application context
if(null == contextRefreshedEvent.getApplicationContext().getParent()) {
LOGGER.debug(">>>>> spring init finished <<<<<");
// call BaseService initMapper method
ConfigurableApplicationContext applicationContext = (ConfigurableApplicationContext)contextRefreshedEvent.getApplicationContext();
Map<String, Object> baseServices = applicationContext.getBeansWithAnnotation(BaseService.class);
for(Object service : baseServices.values()) {
try {
Method initMapper = service.getClass().getMethod("initMapper");
initMapper.invoke(service);
} catch (Exception e) {
LOGGER.error("init BaseService initMapper failed", e);
e.printStackTrace();
}
}
}
}
}
项目启动时,entityServiceImpl.initMapper方法已被调用
@Override
public void initMapper() {
this.mapper = SpringContextUtil.getBean(getMapperClass());
}
但是当我在 Controller 中使用entityService时。无法使用已由ApplicationListener初始化的entityService。这就是我尝试使用entityService的方式
@Conroller
public class LoginController {
@Resource
EntityService EntityService1;
@Autowired
EntityService EntityService2;
EntityService EntityService3 = (EntityService)SpringContextUtil.getBean(EntityService.class);
}
使用Idea调试,我可以找到一个具有非空映射器的EntityService。但在 Controller 中,三个 EntityService 映射器全部为空。我如何将 EntityService 与由 ApplicationListener 初始化的映射器一起使用?
最佳答案
Spring使用beanPostProcessors链,它可以通过CGLIB或动态代理修改您的初始类。所以 this.mapper = SpringContextUtil.getBean(getMapperClass());可能无法按您的预期工作
关于java - 如何使用ApplicationListener ConfigurableApplicationContext启动的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53795204/
public class Tasker{ ConfigurableApplicationContext context ; public void load(){
当我尝试在 Spring Tool Suite 中创建我的第一个应用程序时,出现以下错误: Multiple markers at this line - The type org.springfra
我在 jsf 调用的方法中有以下代码。 它获得了 spring 的 ApplicationContext,这意味着我可以用它获得一个 bean。 FacesContext fc = FacesCont
我们有一个托管在共享 tomcat 实例上的 Spring 应用程序。 有时我们必须重新加载 spring 应用程序上下文,但不想重新启动 Tomcat 服务器,因为其他应用程序也托管在那里。 正在通
我是 spring boot 的新手,在尝试使用 spring initializer 在 intellij 中构建我的第一个 spring boot 项目 (Maven) 时,在创建项目后运行我的项
我在主方法中遇到了阻塞问题“关闭此“ConfigurableApplicationContext”” public static void main(String[] args) { Spri
我正在学习 spring.io 上的教程使用 spring boot 构建一个 spring 应用程序。 我可以让程序在一台计算机上完美运行。当我在另一台计算机上尝试时,出现以下错误 The type
我的 springboot 应用程序一直在运行,但在运行应用程序时突然出现以下错误。 线程“main”中的异常 java.lang.NoSuchMethodError: org.springframe
我是一名优秀的程序员,十分优秀!