Reputation:
If you cant find the bug at least say yes or no that i have carried out all the correct steps, that way at least I a can think that there is another error in my context xml file
The error is as follows
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [net.com.htts.transaction.service.mapping.FundService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
I have a Junit test which cant load the a repository bean.
1) I created a interface FundRepository view plaincopy to clipboardprint? Note: Text content in the code blocks is automatically word-wrapped package net.com.htts.transaction.data.repository;
import org.springframework.stereotype.Repository;
import net.com.htts.transaction.data.entity.Fund;
@Repository
public interface FundRepository {
public void createFund(Fund fund);
}
package net.com.htts.transaction.data.repository;
import org.springframework.stereotype.Repository;
import net.com.htts.transaction.data.entity.Fund;
@Repository
public interface FundRepository {
public void createFund(Fund fund);
}
The interface has the @Repository
2) I created the FundRepositoryImpl which implements my interface
view plaincopy to clipboardprint? Note: Text content in the code blocks is automatically word-wrapped package net.com.htts.transaction.data.repository;
import java.util.List;
import net.com.htts.transaction.data.entity.Fund;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
public class FundRepositoryImpl implements FundRepository{
@PersistenceContext(type = PersistenceContextType.EXTENDED)
private EntityManager entityManager;
public void createFund(Fund fund) {
entityManager.persist(fund);
}
}
package net.com.htts.transaction.data.repository;
import java.util.List;
import net.com.htts.transaction.data.entity.Fund;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
public class FundRepositoryImpl implements FundRepository{
@PersistenceContext(type = PersistenceContextType.EXTENDED)
private EntityManager entityManager;
public void createFund(Fund fund) {
entityManager.persist(fund);
}
}
Created the spring data context
in this context i added the following
view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped
<!-- Scan the repositories -->
<jpa:repositories base-package="net.com.htts.transaction.data.repository" />
<!-- Scan the data layer -->
<context:component-scan base-package="net.com.htts.transaction.data" />
<!-- Scan the repositories -->
<jpa:repositories base-package="net.com.htts.transaction.data.repository" />
According to everything i have read this should be enough to allow me to @Autowired in FundRepository.
The Junit is as follows
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/spring/fund-test-data.xml")
public class FundRepositoryTest {
@Autowired
private FundRepository repository;
@Test
public void createFund(){
}
}
The context is as follows
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>net.com.htts.transaction.data</value>
<value>net.com.htts.transaction.data.repository</value>
</list>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQLInnoDBDialect" />
<property name="generateDdl" value="true" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.ejb.naming_strategy">
net.com.htts.transaction.data.naming.NamingStrategy</prop>
</props>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/funds" />
<property name="username" value="fund_user" />
<property name="password" value="fund_user" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Scan the data layer -->
<context:component-scan base-package="net.com.htts.transaction.data" />
<!-- Scan the repositories -->
<jpa:repositories base-package="net.com.htts.transaction.data.repository" />
</beans>
Upvotes: 2
Views: 2502
Reputation: 15508
1) From your log snippet No matching bean of type [net.com.htts.transaction.service.mapping.FundService]
indicates that the problem is with a definition of FundService somewhere in your project which I did not see anywhere in the presented sources.
Double check that you are indeed having problems with the repo and attach the appropriate log, or take a look at that service
2) If you want to simplify your code and avoid rewriting again the data access layer, you can take advantage of spring data repositories
Upvotes: 0
Reputation: 1234
annotation do not inherited, you should use annotation in class that implemented interface. if you have to implementation you must use @Qualifire
Upvotes: 0
Reputation: 3589
Afaik the @Repository
annotation must be present on the concrete implementation, not the interface. Spring will not find and instantiate your FundRepositoryImpl
, as it is not marked with any of the component annotations. In general, annotations in java are not inherited. Some frameworks allow annotation inheritance through other means. See also this question.
Upvotes: 3