-1

I'm trying to integrate Mybatis with spring. Here you can see my application context of Spring

<context:annotation-config /> 

<context:component-scan base-package="com" />

<tx:annotation-driven />  

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
    <property name="url" value="jdbc:mysql://localhost:3306/DB" />  
    <property name="username" value="root" />  
    <property name="password" value="" />  
</bean> 

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:/com/mybatis/mybatis-config.xml" />
    <!--<property name="transactionFactory" ref="springManagedTransactionFactory" />-->
</bean>

<!--
<bean id="springManagedTransactionFactory" class="org.mybatis.spring.transaction.SpringManagedTransactionFactory">  
    <constructor-arg index="0" ref="dataSource" />  
</bean> -->

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

<bean id="registroClimaMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">  
    <property name="mapperInterface" value="com.mybatis.dao.RegistroClimaMapper" />  
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
</bean> 

First of all I have commented springManagedTransactionFactory in XML because it's giving me an exception

Error creating bean with name 'springManagedTransactionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)

Here is my interface RegistroClimaMapper. I do not have any annotation here because I have defined the context:component scan.

public interface RegistroClimaMapper {

  void insertarRegistroClima(RegistroClima registro) throws SQLException;

  List<RegistroClima> getRegistrosClima() throws SQLException;

  List<RegistroClima> getRegistrosClima(@Param("Validado") boolean Validado) throws SQLException;

}

I try to use this interface in a ManagedBean and when I'm going to use registroClimaPersistence inside a method of TablaRegistroClimaBean I get a NullPointerException

@ManagedBean(name = "tablaRegClimaBean")
@ViewScoped
public class TablaRegistroClimaBean implements Serializable {

  @Autowired
  private RegistroClimaMapper registroClimaPersistence;

  public void setRegistroClimaPersistence(RegistroClimaMapper registroClimaPersistence) {
    this.registroClimaPersistence = registroClimaPersistence;
  }
}
Alexander
  • 2,925
  • 3
  • 33
  • 36
JuanDYB
  • 590
  • 3
  • 9
  • 23
  • What `NullPointerException` do you get? Do you have any Bean implementing the `RegistroClimaMapper` interface? Because defining an Interface just isn't enough. ;) – Alexander Mar 08 '15 at 23:28

1 Answers1

0

As seen here: http://javadox.com/org.mybatis/mybatis-spring/1.1.1/org/mybatis/spring/transaction/SpringManagedTransactionFactory.html#SpringManagedTransactionFactory%28%29, SpringManagedTransactionFactory has a default constructor, but none that takes a datasource. You need to pass the datasource to the newTransaction() method, but not to the constructor: http://javadox.com/org.mybatis/mybatis-spring/1.1.1/org/mybatis/spring/transaction/SpringManagedTransactionFactory.html#newTransaction(javax.sql.DataSource,%20org.apache.ibatis.session.TransactionIsolationLevel,%20boolean)

Change the piece of code to

<bean id="springManagedTransactionFactory" class="org.mybatis.spring.transaction.SpringManagedTransactionFactory">  
  <!--<constructor-arg index="0" ref="dataSource" />-->
</bean>

or completely remove the constructor-arg.

What NullPointerException do you get? It might come from you just defining the RegistroClimaMapper interface, but not implementing this interface in any bean. Please add a Bean that implements this interface and the Autowired annotation should work.

Alexander
  • 2,925
  • 3
  • 33
  • 36
  • I get a NullPointerException in TablaRegistrosClima bean in the method where I use registroClimaPersistence object. How can I fix the problem with springManagedTransactionFactory? You say me that it have a constructor with one argument and in my xml I'm specifying the dataSource argument – JuanDYB Mar 08 '15 at 23:31
  • Could you add the stacktrace and the corresponding line of code where the Exception occurs, please? – Alexander Mar 08 '15 at 23:32
  • I have a class that implements this interface but is done with Mybatis before integrating it with spring. I do not see any example of the implementation of the interface when using mybatis with spring. Could you show me a good tutorial about mybatis with spring? – JuanDYB Mar 08 '15 at 23:40
  • I don't know anything about mybatis, but the fix above (comment out the constructor-arg, not the whole bean) should fix your NPE – Alexander Mar 08 '15 at 23:42
  • Sorry for my question but i'm starting with spring and I do not have idea about some things. I'm not pretty sure about if I have to put @component on the classes that i want to inject or if it's enought with context:component scan ... or somethings like that. – JuanDYB Mar 08 '15 at 23:53
  • `` scans for **components** - i.e. Classes that have a `@Component` annotation. So, yes, you need it when you want to define a component. The other way is to implicitly declare beans in your application context. – Alexander Mar 09 '15 at 08:27
  • For example. I have declared the beans like you can see in my question like bean registroClimaMapper. And I also have If I put Autowired(Annotation) in a instance of registroClimaMapper in my ManagedBean it should work well? At the moment I have a NullPointerException but I have done a test using Resource(Annotation) instead Autowired and it works ok. I have also tryed to put Resource in ManagedBean and it doesn't work. – JuanDYB Mar 09 '15 at 10:48
  • Hello @JuanDYB. First of all you don't need @Autowired and a setter - choose either one. Since you're using JSF `@ManagedBean` and Spring, have a look at this: http://stackoverflow.com/questions/28421777/autowired-service-inside-a-managedbean-component-class-is-null-during-jsf-req . Looks like you JSF ManagedBean dosn't get instanciated by spring, therefore the @Autowired annotation doesn't work – Alexander Mar 10 '15 at 17:28