Results 1 to 3 of 3

Thread: Injecting objs via Spring in non-web application

  1. #1
    Join Date
    Oct 2005
    Posts
    11

    Default Injecting objs via Spring in non-web application

    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:

    Code:
    	private IADataObjectService iaDataObjectService;
    
    	public void setIaDataObjectService(IADataObjectService service) {
    		iaDataObjectService = service;
    	}
    And then use that service to do my db dirty work.

    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:

    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());
    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.

    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 &#40;alternative to JTA&#41; -->
    	<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>

  2. #2
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default

    I don't see what the problem is. Since its a stand alone app, you have an entry point, like main(), so that main can load the app context in a "singleton" manner, and all the classes you need can be in that context, with their dataSource and other related objects injected.

    Sorry if I mis interpreted the question and provided a bad response.

  3. #3
    Join Date
    Oct 2005
    Posts
    11

    Default

    Hmm,

    I see what you mean. I thought I had done that. In my main method I have that chunk of code. I then initialize another class that is described and detailed in the model.xml file. But the DAO interface reference variable is not "injected". It's always null.

    I'll have to have a play with this.


    Thanks!

Similar Threads

  1. Replies: 5
    Last Post: Aug 9th, 2008, 05:30 AM
  2. Replies: 2
    Last Post: Jul 19th, 2005, 07:30 AM
  3. Spring http invoker: retrieving the application context...?
    By gvl@foundation.be in forum Remoting
    Replies: 1
    Last Post: Jun 21st, 2005, 12:59 AM
  4. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM
  5. Questioning the core component
    By Martin Kersten in forum Swing
    Replies: 6
    Last Post: Feb 21st, 2005, 03:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •