Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7577633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:14:18+00:00 2026-05-30T17:14:18+00:00

Im using Spring 3.0 and JDBC I’m currently experimenting with spring for a desktop

  • 0

Im using Spring 3.0 and JDBC I’m currently experimenting with spring for a desktop application. Each time a class would need to use the datasource declared on my spring_bean_file.xml I would need to declare each of them in the xml file and again initialize each of them in my class.
Below is my xml file

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">       
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="#{T(java.lang.System).getenv(DB_URL')}"/>
        <property name="username" value="#{T(java.lang.System).getenv('DB_USER')}"/>
        <property name="password" value="#{T(java.lang.System).getenv('DB_PASS')}"/>        
    </bean>


<bean id="classA" class="com.example.ClassA">
    <property name="dataSource" ref="dataSource"/>
</bean>

<bean id="classB" class="com.example.ClassB">
    <property name="dataSource" ref="dataSource"/>
</bean>

This is my java code:

public static void main(String[] args) {        

        ApplicationContext context = new ClassPathXmlApplicationContext("spring_bean_file.xml");
            ClassA classA = (ClassA) context.getBean("classA");
            ClassB classB = (ClassB) context.getBean("classB");
            try {
                rrRpi.generateRrRpi();
                rrSpi.generateRrSpi();
            } catch (SQLException e) {

                e.printStackTrace();
            } catch (IOException e) {

                e.printStackTrace();
            }

         //close the context             
        ((ClassPathXmlApplicationContext) context).close(); 

    }

Id like to know if theres a more efficient way of doing this. Whats the best approach so I wouldnt need to add a bean for each class that would use the DataSoruce. Thanks in advance guys.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:14:19+00:00Added an answer on May 30, 2026 at 5:14 pm

    You can use:

    Annotations and CLASSPATH scanning

    @Service
    public class ClassA {
        @Resource
        private DataSource dataSource;
    
        //...
    }
    
    @Service
    public class ClassB {
        @Resource
        private DataSource dataSource;
    
        //...
    }
    

    dataSource definition is still required to be in the XML, you also need to replace classA and classB <bean/> definitions with simple:

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    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">
    
        <context:component-scan base-package="com.example" />
    
        <!-- ... -->
    </beans
    

    @Configuration

    Or you can take advantage of very new but exciting Java configuration:

    @Configuration
    public class Cfg {
    
        @Bean
        public DataSource dataSource() {
            BasicDataSource ds = new BasicDataSource();
            ds.setDriverClassName("oracle.jdbc.driver.OracleDriver);
            //...
            return ds;
        }
    
        @Bean
        public ClassA classA() {
            ClassA ca = new ClassA()
            ca.setDataSource(dataSource());
            return ca;
        }
    
        @Bean
        public ClassB classB() {
            ClassB cb = new ClassB()
            cb.setDataSource(dataSource());
            return cb;
        }
    
    }
    

    The benefit of this approach is that you don’t need XML, at all:

    new AnnotationConfigApplicationContext(Cfg.class);
    

    See also

    • What is meant by abstract="true" in spring? – on how to avoid declaring duplicated dependencies (dataSource is actually used as an example).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Spring's support for JDBC. I'd like to use JdbcTemplate (or SimpleJdbcTemplate) to
I have an application that is already using the Spring Framework and Spring JDBC
I'm using Spring 3.0.2 and I have a class called MovieDAO that uses JDBC
We are currently using Spring JDBC with connection pool implementation from DBCP to connect
imagine a transactional, multithreaded java application using spring, jdbc and aop with n classes
i currently build an application use spring as framework. and i want to test
I am using Spring JdbcTemplate/SimpleJdbcTemplate in combination with an Oracle datasource (oracle.jdbc.pool.OracleDataSource) via JNDI
I am using Spring Forms for my web application. For nested properties, the form
I've been using spring for some time, but I always wondered how does it
We're using Spring 2.5.4/Hibernate 3.2/Websphere Application Server 6.1.0.17. We've deployed the application on an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.