jparepository在服务中未自动连接

huangapple go评论92阅读模式
英文:

jparepository not autowired in service

问题

以下是翻译好的内容:

@Service
@Transactional
public class ArticleService {

    @Autowired
    private ArticleRepository articleRepository;
}
@Repository
public interface ArticleRepository extends JpaRepository<ArticleRepository, Long>{
    
}
<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/gestion_de_stock?createDatabaseIfNotExist=true"></property>
    <property name="username" value="root"></property>
    <property name="password" value=""></property>
</bean>

<bean name="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    <property name="persistenceXmlLocations">
        <list>
            <value>classpath*:META-INF/persistence.xml</value>
        </list>
    </property>
    <property name="defaultDataSource" ref="dataSource"></property>
</bean>

<bean name="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitManager" ref="persistenceUnitManager"></property>
    <property name="persistenceUnitName" value="UP_GESTION_DE_STOCK_MVC"></property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>

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

如果您需要更多帮助,请随时提问。

英文:

I am trying to autowire a repository in a service but I keep getting the following error
<pre>
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'articleService': Unsatisfied dependency expressed through field 'articleRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mvc.stock.repository.ArticleRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1238)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1151)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:828)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mvc.stock.repository.ArticleRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
... 38 more
</pre>

Here is my configuration

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

&lt;/bean&gt;

&lt;bean name=&quot;persistenceUnitManager&quot;
		class=&quot;org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager&quot;&gt;
	&lt;property name=&quot;persistenceXmlLocations&quot;&gt;
		&lt;list&gt;
			&lt;value&gt;classpath*:META-INF/persistence.xml&lt;/value&gt;
		&lt;/list&gt;
	&lt;/property&gt;
	&lt;property name=&quot;defaultDataSource&quot; ref=&quot;dataSource&quot;&gt;&lt;/property&gt;
&lt;/bean&gt;

&lt;bean name=&quot;entityManagerFactory&quot;
		class=&quot;org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean&quot;&gt;
	&lt;property name=&quot;persistenceUnitManager&quot; ref=&quot;persistenceUnitManager&quot;&gt;&lt;/property&gt;
	&lt;property name=&quot;persistenceUnitName&quot; value=&quot;UP_GESTION_DE_STOCK_MVC&quot;&gt;&lt;/property&gt;
&lt;/bean&gt;

&lt;bean id=&quot;transactionManager&quot; class=&quot;org.springframework.orm.jpa.JpaTransactionManager&quot;&gt;
	&lt;property name=&quot;entityManagerFactory&quot; ref=&quot;entityManagerFactory&quot;&gt;&lt;/property&gt;
&lt;/bean&gt;

&lt;context:component-scan base-package=&quot;com.mvc.stock&quot; /&gt;

here is my code

@Service
@Transactional
public class ArticleService {

    @Autowired
    private ArticleRepository articleRepository;
}

and the repository

 @Repository
 public interface ArticleRepository extends JpaRepository&lt;ArticleRepository, Long&gt;{

}

答案1

得分: 1

错误信息如下:

org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'articleService': Unsatisfied dependency expressed through field 'articleRepository'; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 'com.mvc.stock.repository.ArticleRepository' available: 
expected at least 1 bean which qualifies as autowire candidate. 
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

UnsatisfiedDependencyException 是由于 ArticleRepository 中的嵌套异常 NoSuchBeanDefinitionException 引起的。

大多数情况下,这是由于缺少 @Repository 注解引起的。但在你的情况下,请检查 ArticleRepository 接口,你使用了 JpaRepository<ArticleRepository, Long>,而应该使用 JpaRepository<Article, Long>

ArticleRepository:

@Repository
public interface ArticleRepository extends JpaRepository<Article, Integer> {

}

ArticleService:

@Service
class ArticleService {

    @Autowired
    ArticleRepository articleRepository;
    //其他字段
    
}
英文:

Error says:

org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name &#39;articleService&#39;: Unsatisfied dependency expressed through field &#39;articleRepository&#39;; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type &#39;com.mvc.stock.repository.ArticleRepository&#39; available: 
expected at least 1 bean which qualifies as autowire candidate. 
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

UnsatisfiedDependencyException due to nested Exception NoSuchBeanDefinitionException in ArticleRepository.

Most of the time it comes due to absence of @Repository. But in your case check ArticleRepository interface you have used JpaRepository&lt;ArticleRepository, Long&gt; instead of JpaRepository&lt; Article, Long&gt;

ArticleRepository:

@Repository
public interface ArticleRepository extends JpaRepository&lt; Article, Integer&gt; {

}

ArticleService

    @Service
    class articleService{
    
    @Autowired
    ArticleRepository articleRepository;
    //other fields 
    
    }

答案2

得分: 1

你没有指示Spring生成存储库。这可以通过以下方式完成:

<jpa:repositories base-package="foo.bar" />

你可能还想启用注释驱动的事务管理:

<tx:annotation-driven />

请参阅 https://stackoverflow.com/questions/25049778/how-to-configure-spring-data-jpa-using-xml

英文:

You didn’t instruct Spring to generate the repositories.
This is done via:

&lt;jpa:repositories base-package=&quot;foo.bar&quot; /&gt;

You probably also want to enable annotation driven transaction management:

&lt;tx:annotation-driven /&gt;

See https://stackoverflow.com/questions/25049778/how-to-configure-spring-data-jpa-using-xml

huangapple
  • 本文由 发表于 2020年3月15日 06:47:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/60688028.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定