gpt4 book ai didi

spring - 为什么我在 Spring-boot 应用程序中通过 SessionFactory 产生循环依赖?

转载 作者:行者123 更新时间:2023-12-04 04:24:12 25 4
gpt4 key购买 nike

我在启动 Spring-Boot 应用程序时遇到错误。

现在,我想在 DAO 存储库中使用 HibernateDaoSupport,因为 Spring boot 不会自动创建 SessionFactory。因此,我从 EntityManagerFactory 创建了 SessionFactory bean,并尝试在 DAO 类中 Autowiring 它。

但是我得到了以下错误:

Description:

The dependencies of some of the beans in the application context form a cycle:

fooDao defined in file [/home/user/test/out/production/classes/by/test/testing_the_java_service_layer/repository/FooDao.class]
┌─────┐
| sessionFactory defined in class path resource [by/test/testing_the_java_service_layer/configuration/Config.class]
└─────┘

我不明白为什么 SessionFactory 是指 FooDao 类。

以下是代码示例:

FooDao.java

@Repository
public class FooDao extends HibernateDaoSupport
{
@Autowired
public void setSessionFactories( SessionFactory sessionFactory )
{
setSessionFactory( sessionFactory );

}

@Transactional
public int create( Foo entity )
{
return (int) this.getHibernateTemplate().save( entity );
}
}

配置.java

@ComponentScan( basePackages = { "by.test" } )
@Configuration
public class Config
{
/*
* Spring boot doesn't create SessionFactory bean, so we have to create it manually, using EntityManagerFactory
*/
@Bean
public SessionFactory sessionFactory( EntityManagerFactory entityManagerFactory )
{
return entityManagerFactory.unwrap( SessionFactory.class );
}
}

Foo.java

@Entity
@Table( name = "bibis" )
public class Foo
{
@Id
@Column( name = "foo", nullable = false )
public int foo;

@Column( name = "bar" )
public String bar;
}

TestApplication.java

@SpringBootApplication
public class TestApplication
{

public static void main( String[] args )
{
SpringApplication.run( TestApplication.class, args );
}
}

application.yaml

spring:
datasource:
username: 'bibis'
password: 'bibis'
schema: 'bibis'
host: 'localhost:3306'
url: 'jdbc:mariadb://localhost:3306/bibis'

和来自 build.gradle 的 gradle 依赖项

implementation('org.mariadb.jdbc:mariadb-java-client')
developmentOnly 'org.springframework.boot:spring-boot-devtools'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.1.8.RELEASE'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

我尝试使用@Lazy 注释、构造函数或字段注入(inject)但没有帮助。

最佳答案

我引用了Spring Boot - Handle to Hibernate SessionFactory的答案

Config 类返回 HibernateJpaSessionFactoryBean obj 而不是 SessionFactory 对象。

修改后的代码为:

@ComponentScan( basePackages = { "by.test" } )
@Configuration
public class Config
{
@Bean
public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory entityManagerFactory)
{
HibernateJpaSessionFactoryBean hibernateJpaSessionFactoryBean = new HibernateJpaSessionFactoryBean();
hibernateJpaSessionFactoryBean.setEntityManagerFactory(entityManagerFactory);
return hibernateJpaSessionFactoryBean;
}
}

或者你可以这样做:

@Repository
public class FooDao extends HibernateDaoSupport
{
@Autowired
public void setSessionFactories(EntityManagerFactory entityManagerFactory)
{
setSessionFactory(entityManagerFactory.unwrap(SessionFactory.class));
}

@Transactional
public int create(Foo entity)
{
return (int) this.getHibernateTemplate().save( entity );
}
}

关于spring - 为什么我在 Spring-boot 应用程序中通过 SessionFactory 产生循环依赖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58223600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com