2

I have this xml Based configuration

    <?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
             xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        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-4.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
    infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
    up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
    in the /WEB-INF/views directory -->
    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <!--        
    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
                destroy-method="close">
        <beans:property name="driverClassName" value="org.postgresql.Driver" />
        <beans:property name="url"
                        value="jdbc:postgresql://localhost:5432/Hibernate" />
        <beans:property name="username" value="postgres" />
        <beans:property name="password" value="password" />
    </beans:bean>-->


    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
                    destroy-method="close">
        <beans:property name="driverClassName" value="org.postgresql.Driver" />
        <beans:property name="url"
                        value="jdbc:postgresql://192.168.0.9:5435/HGCTEST?autoReconnect=true" />
        <beans:property name="username" value="AppDev" />
        <beans:property name="password" value="hgcadmin" />
    </beans:bean>


    <!-- Hibernate 4 SessionFactory Bean definition -->
    <beans:bean id="hibernate4AnnotatedSessionFactory"
                class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource" />
        <beans:property name="annotatedClasses">
            <beans:list>
                <beans:value>com.bankdeposit.model.Check</beans:value>
                <beans:value>com.bankdeposit.model.Unit</beans:value>

            </beans:list>
        </beans:property>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect
                </beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>

                <beans:prop key="hibernate.c3p0.min_size">5</beans:prop>
                <beans:prop key="hibernate.c3p0.max_size">20</beans:prop>
                <beans:prop key="hibernate.c3p0.timeout">300</beans:prop>
                <beans:prop key="hibernate.c3p0.max_statements">50</beans:prop>
                <beans:prop key="hibernate.c3p0.idle_test_period">3000</beans:prop>

            </beans:props>
        </beans:property>
    </beans:bean>

    <beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </beans:bean>

    <beans:bean id="checkDAO" class="com.bankdeposit.dao.CheckDAOImpl">
        <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </beans:bean>

    <beans:bean id="checkService" class="com.bankdeposit.service.CheckServiceImpl">
        <beans:property name="checkDAO" ref="checkDAO"></beans:property>
    </beans:bean>

    <beans:bean id="unitDAO" class="com.bankdeposit.dao.UnitDAOImpl">
        <beans:property name="sessionFactory2" ref="hibernate4AnnotatedSessionFactory" />
    </beans:bean>

    <beans:bean id="unitService" class="com.bankdeposit.service.UnitServiceImpl">
        <beans:property name="unitDAO" ref="unitDAO"></beans:property>
    </beans:bean>



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

    <tx:annotation-driven transaction-manager="transactionManager"/>




</beans:beans>

after I try to register my other bean with the following code:

<beans:bean id="unitDAO" class="com.bankdeposit.dao.UnitDAOImpl">
    <beans:property name="sessionFactory2" ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>

<beans:bean id="unitService" class="com.bankdeposit.service.UnitServiceImpl">
    <beans:property name="unitDAO" ref="unitDAO"></beans:property>
</beans:bean>

I got an error with the following message

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'unitDAO' defined in ServletContext resource [/WEB-INF/spring/bdservlet/servlet-context.xml]: Error setting property values;

How to register the new Bean created in xml?

here is my UnitDAOImpl

@Repository
public class UnitDAOImpl implements UnitDAO {

    private static final Logger logger = LoggerFactory.getLogger(UnitDAOImpl.class);

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sf) {
        this.sessionFactory = sf;
    }

    @Override
    public List<Unit> listUnits() {
        Session session = this.sessionFactory.getCurrentSession();

        // query that hase no where clause
        List<Unit> unitList = session.createQuery("from Unit order by code asc").list();
        for (Unit u : unitList) {
            logger.info("unit List::" + u);
        }

        return unitList;

    }

}
1
  • can you share your class UnitDAOImpl? Commented Oct 6, 2016 at 9:11

2 Answers 2

1

You need to show your class UnitDAOImpl, but just a suspicion: does your class UnitDAOImpl has actual property called "sessionFactory2"? if your setter and getter methods for that property are "getSessionFactory()" and "setSessionFactory()" (or if it is based on member itself and your variable is called "sessionFactory" then in your configuration for UnitDAOImpl <beans:property name="sessionFactory2" change "sessionFactory2" to "sessionFactory"

Sign up to request clarification or add additional context in comments.

5 Comments

i think this is the problem too
public void setSessionFactory(SessionFactory sf) { this.sessionFactory = sf; }
shoud i change it to setSessionFactory2?
So, I was correct. just change in your configuration "sessionFactory2" to "sessionFactory" and you should be good to go
You could, but it makes much better sense to change "sessionFactory2" to "sessionFactory" in your configuration.
0

Well your configuration surely has more than one issues.

i) The most obvious is what @Michael Gantman mentioned in his answer , that for some reason you wire the propertysessionFactory2 which doesn't seem to exist somewhere.

ii) Your second obvious issue is that you might be scanning the same packages twice , so for your UnitDAOImpl the first reference will be created because of this line

<beans:bean id="unitDAO" class="com.bankdeposit.dao.UnitDAOImpl"> <beans:property name="sessionFactory2" ref="hibernate4AnnotatedSessionFactory" /> </beans:bean

which will be a proper configured spring bean and if you change the property name to sessionFactory the property will be wired as well.

now the second instance will be created because of this line

<context:component-scan base-package="com.bankdeposit" /> and because you dont have implicitly set the the @Autowired annotation on the constructor level , the sessionFactory property won't get wired , so probably when calling the DAO methods you would get a NPE.

Also to confirm it , you could add this to your UnitDAOImpl constructor method , to check that it will be called twice :

public UnitDAOImpl() { System.out.println("Constructor Called"); }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.