无法自动连线,找不到类型的bean

时间:2019-04-27 05:10:07

标签: java hibernate spring-boot

我是Java的新手,我对java中的springboot有3个问题

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>

但是在春季启动中,我没有 web.xml 说要使用此配置。

我该怎么办?

3-还有最后一个问题,它是否有输入我的Java模型并给我hbm.xml文件的软件?

1 个答案:

答案 0 :(得分:-1)

使用以下代码

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