AbstractMethodError和在服务中调用DAO时为null映射器

问题描述 投票:0回答:1

[我正在使用Spring Boot,Mybatis(Spring和Generator)构建一个独立的应用程序,我认为我已经配置了所有东西,但是自动装配映射器为null,调用它时出现AbstractMethodError。

实际上,我认为是因为null映射器。我一直在寻找它,并且一直在寻找所有可能的解决方案,但是没有任何效果。

关于代码:我也使用JavaFX,但这对我的问题没有任何影响。

my.project.core.Main.java

@SpringBootApplication
@MapperScan({"my.project.persistence"})
public class Main extends Application{

    private ConfigurableApplicationContext springContext;

    public static void main(String[] args){
        launch(args);
    }

    public void start(Stage stage) throws Exception {

        springContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);

        //Some code to display the views

        System.out.println("test 1: " + springContext.getBean(UsuarioMapper.class));

        stage.show();
    }
}

(由MyBatis-Generator生成的DAO)my.project.persistence.UsuarioMapper.java

@Mapper //I've also tried @Component
public interface UsuarioMapper {

    int countByExample(UsuarioExample example);

    int deleteByExample(UsuarioExample example);

    int deleteByPrimaryKey(String codUsuario);

    int insert(UsuarioBean record);

    //Etc etc etc
}

((服务)my.project.services.ServicioUsuario.java

@Service
public class ServicioUsuario {

    @Autowired private UsuarioMapper usuarioMapper;

    public boolean userExists(String user){

        System.out.println("test 2: " + usuarioMapper);
        UsuarioBean bean = usuarioMapper.selectByPrimaryKey(user);
        return bean!=null;
    }

}

((Config)my.project.config.SpringConfiguration.java

@Configuration
public class SpringConfiguration {

    /*========== SERVICES ==========*/

    @Bean 
    public ServicioUsuario servicioUsuario() throws Exception{
        return new ServicioUsuario();
    }

    /*========== DAO ==========*/

    //I've already tried passing userMapper().getObject() in ServicioUsuario
    //constructor, but the output is the same, null mapper
    @Bean
    public MapperFactoryBean<UsuarioMapper> userMapper() throws Exception {
        MapperFactoryBean<UsuarioMapper> factoryBean = new MapperFactoryBean<UsuarioMapper>(UsuarioMapper.class);
        factoryBean.setSqlSessionFactory(sqlSessionFactory());
        return factoryBean;

    /*========== CONNECTION ==========*/

    @Bean
    public DataSource dataSource() throws ClassNotFoundException, SQLException{
        OracleDataSource dataSource = new OracleDataSource();

        dataSource.setDriverType("thin");
        dataSource.setServerName("localhost");
        dataSource.setDatabaseName("xe");
        dataSource.setPortNumber(1521);
        dataSource.setUser("testuser");
        dataSource.setPassword("pass123");

        Class.forName("oracle.jdbc.driver.OracleDriver");

        return dataSource;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource());
        SqlSessionFactory sqlSessionFactory = factoryBean.getObject();
        return sqlSessionFactory;
    }

    @Bean
    public DataSourceTransactionManager transactionManager() throws ClassNotFoundException, SQLException {
        return new DataSourceTransactionManager(dataSource());
    }

    @Bean
    public SqlSessionTemplate sqlSession() throws Exception {
          return new SqlSessionTemplate(sqlSessionFactory());
        }

}

输出(摘要):

18:30:41.023 [JavaFX Application Thread] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'springConfiguration'
18:30:41.095 [JavaFX Application Thread] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'servicioUsuario'
18:30:41.096 [JavaFX Application Thread] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'userMapper'
18:30:41.097 [JavaFX Application Thread] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'sqlSessionFactory'
18:30:41.107 [JavaFX Application Thread] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'dataSource'
18:30:41.231 [JavaFX Application Thread] DEBUG org.mybatis.spring.SqlSessionFactoryBean - Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration
18:30:41.273 [JavaFX Application Thread] DEBUG org.mybatis.spring.SqlSessionFactoryBean - Property 'mapperLocations' was not specified or no matching resources found
18:30:41.481 [JavaFX Application Thread] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'transactionManager'
18:30:41.490 [JavaFX Application Thread] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'sqlSession'
test 1: null
test 2: null
18:32:26.250 [JavaFX Application Thread] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
18:32:26.501 [JavaFX Application Thread] DEBUG java.sql.Connection - ooo Connection Opened
//And now the full stacktrace
Exception in thread "JavaFX Application Thread" java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransactionFactory.newTransaction(Ljava/sql/Connection;Z)Lorg/apache/ibatis/transaction/Transaction;
    at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:77)
    at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:40)
    at org.mybatis.spring.SqlSessionUtils.getSqlSession(SqlSessionUtils.java:100)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:428)
    at com.sun.proxy.$Proxy14.selectOne(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:166)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:66)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:35)
    at com.sun.proxy.$Proxy15.selectByPrimaryKey(Unknown Source)
    at my.project.services.ServicioUsuario.userExists(ServicioUsuario.java:42)
    at my.project.gui.LoginController.btAceptar(LoginController.java:70)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8411)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177)
    at java.lang.Thread.run(Unknown Source)

我应该实现MyBatis Generator生成的映射器接口吗?我是否将任何Bean配置错误?我已经尝试了一切,并且该映射器只想保持为空。谢谢。

java spring-boot mybatis spring-mybatis mybatis-generator
1个回答
0
投票
堆栈跟踪清楚地表明已注入映射器,并且它不为null:
© www.soinside.com 2019 - 2024. All rights reserved.