3
votes

I have got little problem with my spring app. here is my code:

(repository)

import com.maciej.entities.Etwas;
import com.maciej.repository.EtwasRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.List;

HERE IS THE PROBLEM -----  @Component works, while @Repository doesn't
public class EtwasHibernateRepository implements EtwasRepository {
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public List<Etwas> findAll() {
        return null;
    }

    @Override
    public Etwas create(Etwas etwas) {
        entityManager.persist(etwas);
        return etwas;
    }

    @Override
    public Etwas read(int id) {
        TypedQuery<Etwas> query = entityManager.createQuery("SELECT e FROM Etwas e WHERE e.id=:id", Etwas.class);
        query.setParameter("id", id);
        Etwas etwas = query.getSingleResult();
        return etwas;
    }

    @Override
    public Etwas update(Etwas etwas) {
        return null;
    }

    @Override
    public boolean delete(int id) {
        return false;
    }
}

here is my simple service class:

import com.maciej.entities.Etwas;
import com.maciej.repository.impl.EtwasHibernateRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class EtwasService {
    @Autowired
    private EtwasHibernateRepository etwasHibernateRepository;

    public boolean save(Etwas etwas){
        etwasHibernateRepository.create(etwas);
        return true;
    }

    public Etwas read(int id){
        return etwasHibernateRepository.read(id);
    }
}

and simple testing class:

import com.maciej.Config;
import com.maciej.entities.Etwas;
import com.maciej.services.EtwasService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class Test {
    public static void main(String[] args){
        AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(Config.class);
        Etwas etwas = acac.getBean("etwas", Etwas.class);
        etwas.setSth("STATATASTAT");

        EtwasService etwasService = acac.getBean("etwasService", EtwasService.class);
        for(int i=0; i<=20; i++){
            etwasService.save(etwas);
        }

        System.out.println(etwasService.read(7).toString());

        System.out.println(etwas.getSth());
    }
}

The problem is, that when i annotate EtwasHibernateRepository with @Component, everything works fine. When I add, actually apoperly - @Repository annotation I get a stacktrace: do you have anny suggestions what do I do wrong?

ps the code is really simple, embroiled and made just for tests so dont care about that :)

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'etwasService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.maciej.repository.impl.EtwasHibernateRepository com.maciej.services.EtwasService.etwasHibernateRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.maciej.repository.impl.EtwasHibernateRepository] 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)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) at org.springframework.context.annotation.AnnotationConfigApplicationContext.(AnnotationConfigApplicationContext.java:84) at com.maciej.test.Test.main(Test.java:12) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.maciej.repository.impl.EtwasHibernateRepository com.maciej.services.EtwasService.etwasHibernateRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.maciej.repository.impl.EtwasHibernateRepository] 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)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) ... 17 more Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.maciej.repository.impl.EtwasHibernateRepository] 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)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) ... 19 more

Update: Configuration

import com.maciej.entities.Etwas;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import java.util.Properties;

@Configuration
@ComponentScan
@EnableTransactionManagement
public class Config {
    @Bean
    public Etwas etwas(){
        return new Etwas();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
        LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
        emf.setDataSource(dataSource());
        emf.setPackagesToScan(new String[]{"com.maciej.entities"});

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        emf.setJpaVendorAdapter(vendorAdapter);
        emf.setJpaProperties(additionalProperties());
        return emf;
    }

    @Bean
    public BasicDataSource dataSource(){
        BasicDataSource bds = new BasicDataSource();
        bds.setUsername("root");
        bds.setPassword("maciek");
        bds.setUrl("jdbc:mysql://localhost:3306/test");
        bds.setDriverClassName("com.mysql.jdbc.Driver");
        return bds;
    }


    private Properties additionalProperties(){
        Properties properties = new Properties();
        properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
        properties.setProperty("hibernate.show_sql", "true");
        properties.setProperty("hibernate.format_sql", "true");
        return properties;
    }


    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf); //entityManagerFactory().getObject()
        return transactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
        return new PersistenceExceptionTranslationPostProcessor();
    }

}
1

1 Answers

3
votes

The problem lies in the fact that you don't program to interfaces.

@Service
@Transactional
public class EtwasService {

    @Autowired
    private EtwasHibernateRepository etwasHibernateRepository;

The autowired dependency is of the fully implementing class, whereas it should be EtwasRepository. This will make it work for both @Component and @Repository. Next to that it is also the proper way to do it.

@Service
@Transactional
public class EtwasService {

    @Autowired
    private EtwasRepository etwasHibernateRepository;

Beans annotated with @Repository will have automatic exception translation enabled and this is applied with AOP. By default Spring uses JDK Dynamic proxies which are interface based. It will create a dynamic class implementing the EtwasRepository interface and apply the aop interceptors. This makes a bean of the type EtwasRepository and not of the EtwasHibernateRepository type anymore.

This conversion isn't applied when annotating your bean @Component.

A note about your configuration the explicit declaration of the PersistenceExceptionTranslationPostProcessor isn't needed. That is already taken care of.