I'm creating a simple application with Spring, Hibernate and MySQL database. I've done some research, but no answer was accurate. I have a repository interface, which is an extension of BaseCrudRepository, which extends ReadOnlyRepository, which is an extension of org.springframework.data.repository.Repository. Code is as below:
package dziecko.repositories;
import dziecko.models.AreaDate;
import dziecko.utils.BaseCrudRepository;
public interface AreaDateRepository extends BaseCrudRepository<AreaDate, Integer> {}
AreaDate is a simple entity:
@Entity
@Table(name = "AREADATES")
public class AreaDate extends AbstractBase implements DtoGenerator<AreaDateDto> {
private static final long serialVersionUID = -2956040373671316338L;
@Column(name = "START_TIME")
private Date startTime;
@Column(name = "END_TIME")
private Date endTime;
@Column(name = "ALARM", nullable = true)
private Boolean alarm;
public AreaDate() {
}
public Date getStartTime() {
return startTime;
}
public Date getEndTime() {
return endTime;
}
public Boolean getAlarm() {
return alarm;
}
@Override
public AreaDateDto createDto() {
return new AreaDateDto(id, startTime, endTime, alarm);
}
}
Here is my web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://ift.tt/ra1lAU"
xmlns="http://ift.tt/nSRXKP"
xsi:schemaLocation="http://ift.tt/nSRXKP
http://ift.tt/1eWqHMP" version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/app-context.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
app-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU" xmlns:mvc="http://ift.tt/1bHqwjR"
xmlns:beans="http://ift.tt/GArMu6" xmlns:jpa="http://ift.tt/1iMF6wA"
xmlns:context="http://ift.tt/GArMu7" xmlns:tx="http://ift.tt/OGfeU2"
xsi:schemaLocation="http://ift.tt/1bHqwjR http://ift.tt/1fmimld
http://ift.tt/GArMu6 http://ift.tt/1jdM0fG
http://ift.tt/OGfeU2 http://ift.tt/18tm2Tg
http://ift.tt/GArMu7 http://ift.tt/1jdLYo7
http://ift.tt/1iMF6wA http://ift.tt/1jZdjKs">
<!-- Enable @Controller annotation support -->
<mvc:annotation-driven />
<jpa:repositories base-package="dziecko.repositories" />
<context:annotation-config />
<!-- Scan classpath for annotations (eg: @Service, @Repository etc) -->
<context:component-scan base-package="dziecko.rests, dziecko.services">
<context:exclude-filter expression="org.springframework.web.bind.annotation.RestController" type="annotation" />
</context:component-scan>
<!-- JDBC Data Source. -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/dziecko" />
<property name="username" value="root" />
<property name="password" value="" />
<property name="validationQuery" value="SELECT 1" />
</bean>
<!-- Hibernate Session Factory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="packagesToScan">
<array>
<value>dziecko.models</value>
</array>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="jadira.usertype.autoRegisterUserTypes">true</prop>
<prop key="jadira.usertype.databaseZone">jvm</prop>
<prop key="hibernate.id.new_generator_mappings">true</prop>
</props>
</property>
</bean>
<!-- Hibernate Transaction Manager -->
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Activates annotation based transaction management -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
The problem is that even this simple test fails, because repository which is supposed to be autowired is null:
package dziecko.tests;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.Assert;
import org.testng.annotations.Test;
import dziecko.repositories.AreaDateRepository;
public class SpringConfigurationTest {
@Autowired
private AreaDateRepository adRepo;
@Test
public void shouldWireComponents() {
Assert.assertNotNull(adRepo);
}
}
java.lang.AssertionError: expected object to not be null
All entities are created inside the database. I just can't figure out how to configure those Spring repositories properly. Everything seems to be done similar to http://ift.tt/1CEMDVq. Anyone has an idea how autowiring can be fixed here?
Aucun commentaire:
Enregistrer un commentaire