【问题标题】:could not autowire no beans of type found无法自动装配没有找到类型的 bean
【发布时间】:2019-09-16 13:10:23
【问题描述】:

我是 java 新手,我有 3 个关于 java 中 springboot 的问题

1 - 我的 BaseRepository 班级

@Repository
@Transactional
public class BaseBaseRepository<Entity extends Base> implements IBaseRepository<Entity> {

    private SessionFactory sessionFactory;

    public BaseBaseRepository(SessionFactory sessionFactory) // this line give me an error : Could not autowire. No beans of 'sessionFactory' type found. when i put @Autowired nothing changed
    {
        this.sessionFactory = sessionFactory;
    }


    @Override
    public Entity Add(Entity entity) {

        try {
            var session = sessionFactory.getCurrentSession();
            session.beginTransaction();
            session.save(entity);
            session.flush();
        } catch (Exception e) {
            throw e;
        }

        return entity;
    }

    // other method
}

2- 我的第二个问题是我的应用程序 applicationContext-dataSource.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/jdbc
            http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jdbc/datasource"/>
        <property name="lookupOnStartup" value="true"/>
        <property name="proxyInterface" value="javax.sql.DataSource"/>
    </bean>
</beans>

及其我的 applicationContext-hibernate.xml

 <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">



    <import resource="applicationContext-dataSource.xml"/>


    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" p:dataSource-ref="dataSource" p:mappingResources="com.payment.Repository.ModelAndMapper.BaseModel.Acceptor.Acceptor.hbm.xml">
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="eventListeners">
            <map>
                <entry key="merge">
                    <bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
                </entry>
            </map>
        </property>
    </bean>


    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
            p:sessionFactory-ref="sessionFactory"/>


    <context:annotation-config/>


    <tx:annotation-driven/>


    <context:mbean-export/>


    <bean id="BaseBaseRepository" class="com.payment.Repository.Implement.BaseBaseRepository"/>

</beans>

但是在 spring boot 中我没有 web.xml 可以说使用这个配置。

我该怎么办?

3 - 最后一个问题是否有任何软件可以输入我的 java 模型并给我hbm.xml 文件?

【问题讨论】:

  • 你使用什么版本的 spring、boot 和 hibernate?这似乎已经过时了

标签: java hibernate spring-boot


【解决方案1】:

使用以下代码

@Configuration
@ImportResource({"classpath*:applicationContext-hibernate.xml"})
public class HibernateConfiguration {
}

【讨论】:

    猜你喜欢
    • 2017-02-17
    • 2014-10-06
    • 2020-01-31
    • 2018-09-22
    • 1970-01-01
    • 2022-01-09
    • 2016-09-30
    • 2019-06-14
    • 1970-01-01
    相关资源
    最近更新 更多