Queries related to beans and application Context.

Go To StackoverFlow.com


I am developing a web application using Spring MVC. I want to ask certain things related to beans file and Application Context.

  1. I have classes which have objects. Let's say i have an employee, Product, project Classes with some objects in that Employee(ID, name) and same goes with Product and project. Now i have to write bean for the following object like this :

    <bean id="..." class="..."/>

    Should i make a separate bean file for each class like employee.xml , product.xml or should i list down all the bean in one beans tag in a single file let's say xyz.xml. ? And we have to mention scopes like request, sessions etc. we have to mention that here only while writing down each bean or elsewhere?

  2. We do have methods like to add an employee in database or delete some entry. Do we need to reference them anywhere in bean tag or simply write them in java classes ?

  3. I have a database connectivity involved using JDBC. Right now i have a class where i simply make a connection object and use it. How to make a bean of that. So that it can b used again and again. Currently my database connectivity class is like this :

    package MyPackage;

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement;

    public class HandleConnections {

    public Connection getConnection()
        Connection con = null;
            String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String db = "jdbc:odbc:Practice_Database";
        con = DriverManager.getConnection(db,"","");
        }catch(Exception e){System.out.println(e);}
        return con;
    public void closeConnectionVariables(Connection obj, ResultSet rs, Statement st){
        //try {rs.close();} catch (SQLException e) {e.printStackTrace();}
        try {obj.close();} catch (SQLException e) {e.printStackTrace();}
        try {st.close();} catch (SQLException e) {e.printStackTrace();}
  4. Once i have done with writing all the bean in separate file or a single file. How to use it in Spring MVC. how to bring that into project using Application Context. Let's say i have employee.xml, product.xml or i have a single file xyz.xml. what code i need to write in my applicationContext.xml so that everything works fine. IS there any bean Factory method i have to write.

Please help me out with my problems.. Thanks... :)

2012-04-04 05:05
by Shantanu Tomar
You don't know anything about Spring and DI, you are not familiar with Spring-MVC...., that's not point to start working with Spring (I'm not sure whether you know Java or not). I recommend you to read some books or documents about Spring. Spring's reference manual is a great starting point by the way - Amir Pashazadeh 2012-04-04 06:10
About your 3rd problem, you shall use a DataSource configured in your Spring context, (and not your custom class) to utilize Spring features (such as declarative transaction and....). I recommend you using JdbcTemplate too - Amir Pashazadeh 2012-04-04 06:12
You can use Annotations for defining beans if you are working with Spring 3. Also, before directly starting with Spring MVC, get some fundamental details about how Spring works and what makes it such a powerful framework - Logan 2012-04-13 10:40


1) POJOs shouldn't be defined as beans.Only services,repositories,controllers should be defined as beans.

2) You dont need to mention in the bean tag.

3) Use JDBCTemplate and Set the data source to it.

4) You have set the contextConfigLocation for your dispatcher servlet like this

2012-04-04 07:55
by sathis


For Your Third Problem: To create bean of any class like services write code in your controller as given below

public class ExamEventController implements ApplicationContextAware {
  private ExamEventServiceImpl eventServiceImpl;

  @RequestMapping(value = { "/eventList" }, method = RequestMethod.GET)
  public String listGet(Model model, HttpServletRequest request,
        Locale locale) {
     ExamEvent event=eventServiceImpl.getEventDetails(eventId);
  public void setApplicationContext(ApplicationContext applicationContext)
        throws BeansException {
    eventServiceImpl = (ExamEventServiceImpl)applicationContext.getBean("ExamEventService");


As per satis mentioned , dont create bean for POJO class.

2014-08-25 12:04
by Viraj Dhamal