简体   繁体   中英

The Mybatis @MapperScan doesn't inject the mapper interface into the class, and my mapper is always null

Like the tittle say, i have this configuration class where i define all the necesary thing for my batis to work

package my.package.noti.config;

@Configuration
@ComponentScan("my.package")
@MapperScan("my.package.noti.bd")
public class AppConfig {

    private final static Logger logger = LogManager.getLogger(AppConfig.class);

    static {
        System.setProperty("java.awt.headless", "true");
    }

    /**
     * Obtener el datasource
     * @return
     */
    @Bean(destroyMethod = "")
    public DataSource getDataSource() {

        if (logger.isDebugEnabled()) {
            logger.debug("getDataSource");
        }
        InitialContext ic2;
        DataSource ds = null;
        try {
            ic2 = new InitialContext();
            ds = (DataSource) ic2.lookup("java:jboss/datasources/MyDS");
        } catch (NamingException e) {
            logger.error(e.getMessage(), e);
        }

        return ds;
    }

    /**
     * Obtener el transactionManager
     * @return
     */
    @Bean
    public DataSourceTransactionManager transactionManager() {
        if (logger.isDebugEnabled()) {
            logger.debug("transactionManager");
        }
        return new DataSourceTransactionManager(getDataSource());
    }

    /**
     * Obtener el sqlSessionFactory
     * @return
     * @throws Exception
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        if (logger.isDebugEnabled()) {
            logger.debug("sqlSessionFactory");
        }
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(getDataSource());
        return sessionFactory.getObject();
    }

    /**
     * Obtener el propertySourcesPlaceholderConfigurer
     * @return
     */
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

}

and this is my mapper:

@Mapper
public interface NotiMapper {

    @Select(value="{ CALL MYPACKAGUE.MYPROCEDURE("
            + "#{response.notis.noti, javaType=java.sql.ResultSet , jdbcType=CURSOR, mode=OUT, resultMap = notification})}")
    @ResultType(notification.class)
    @Options(statementType = StatementType.CALLABLE)
    @Results(id = "notification",
    value = {
            @Result(property = "noti_n", column = "NOTI_N"),
            @Result(property = "noti_t", column = "NOTI_T"),
            @Result(property = "tpno_f", column = "TPNO_F"),
            @Result(property = "tpno_tp", column = "TPNO_TP"),
    })
    public void ObtainNoti(@Param("response")Response response);
    
}

But when a class call the Mapper it is always null

@Service
public class PublisherJboss {
@Autowired
    NotiMapper notiMapper ;
    
    public void process() {
        try {
            //Iniciando JMS
            InitialContext ic = getInitialContext(Config.getProperties().getProperty("providerURLJboss"));
            init(ic, DEFAULT_DESTINATION);
            
            Response respuesta = new Response();
            notiMapper .ObtainNoti(response);
            //Do some other things .... but the notiMapper is always null
}
}

Maybe some of you have encunter this problem before, i have read something about the MapperScannerConfigurer is a BeanDefinitionRegistryPostProcessor which fires before BeanFactoryPostProcessor, but i didn't understand it.

In my case it was because i was creating the class PublisherJboss with new PublisherJboss(), so that way spring creates the Mapper bean but doesn´t know about the existence of the PublisherJboss class that i am trying to inject the mapper bean to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM