Hey guys, Spring newbie here.
Ok. I have successfully written up some code in a web based application under WAS using Spring and Hibernate. I had Spring inject a DAO into the relevant objects that needed it. I defined a datasource in my spring xml file (jndi via WAS), setup the hibernate props, defined my sessionFactory and DAO beans, etc. And it works/ed fine. In my class I could have:
And then use that service to do my db dirty work.Code:private IADataObjectService iaDataObjectService; public void setIaDataObjectService(IADataObjectService service) { iaDataObjectService = service; }
Now I need to do the same thing, but in a stand alone Java app. The app in question is a multi-threaded app, running 24/7. I wrote up some test code in a test class to try and figure out the basics:
And this works. Now the thing is, various classes need to have db connectivity. And I don't want to have to implement the code above in each class. I wanted to have each class, upon initialization, to have it's defined iaDataObjectDAO set via Spring.Code:ApplicationContext ac = new ClassPathXmlApplicationContext(getConfigLocations()); IADataObjectDAO iaDataObjectDAO = (IADataObjectDAO) ac.getBean("IADataObjectDAO"); Session session = iaDataObjectDAO.getHibernateSession(); System.out.println(this.getClass() + "::session open : " + session.isOpen()); System.out.println(this.getClass() + "::session connected : " + session.isConnected()); session.close(); System.out.println(this.getClass() + "::session open : " + session.isOpen());
But I cannot see how to do this without the use of the web server environment. Does anyone have any examples or a link to a similar problem (in a stand alone app)? I'd greatly appreciate it.
Thanks
Oh - here's my current XML file.
Code:<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>com.ibm.db2.jcc.DB2Driver</value> </property> <property name="url"> <value>myurl</value> </property> <property name="username"> <value>user</value> </property> <property name="password"> <value>name</value> </property> </bean> <!--Hiberate session factory setup --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"/> </property> </bean> <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="IADataObjectDAO" class="com.test.HibernateIADataObjectDAO"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- the IADataObjectServiceTarget links to the IADataObjectDAO bean above --> <bean id="IADataObjectServiceTarget" class="com.test.IADataObjectServiceImpl"> <property name="iaDataObjectDAO"> <ref bean="IADataObjectDAO"/> </property> </bean> <!-- the IADataObjectService links to the IADataObjectServiceTarget bean above --> <bean id="IADataObjectService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager"/> </property> <property name="target"> <ref local="IADataObjectServiceTarget"/> </property> <property name="transactionAttributes"> <props> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="remove*">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- MODEL DEFINITIONS --> <!-- list out the specific classes where the data access service is to be injected upon class creation --> <bean id="SpringTestInjector" class="com.test.SpringTestInjector"> <property name="iaDataObjectService"> <ref bean="IADataObjectService"/> </property> </bean>


Reply With Quote