CuriousMind
CuriousMind

Reputation: 8953

What location to put spring bean configuration file

I am implementing a simple application to do CRUD operations, using Spring framework.

Source code:

User.java is the model class.

package com.vipin.model;

    public class User {

    private int ssn;
    private String firstName;
    private String lastName;
    private String emailId;

    public int getSsn() {
        return ssn;
    }
    public void setSsn(int ssn) {
        this.ssn = ssn;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getEmailId() {
        return emailId;
    }
    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }
}

Dao layer:

package com.vipin.dao;
import com.vipin.model.User;

public interface DBOpsDao {
    boolean add(User user);
    boolean find(int ssnId);
}

The class which implements (skelton) implementation is:

package com.vipin.dao;

import java.sql.Connection;
import java.sql.SQLException;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.vipin.model.User;

public class DefaultDBOpsDaoImpl implements DBOpsDao {

    private DataSource dataSource;

    public DataSource getDataSource() {
        return dataSource;
    }

    @Resource(name="dataSource")
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        System.out.println("Datasource value is " + dataSource);
    }

    public boolean add(User user) {

        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

        return false;
    }

    public boolean find(int ssnId) {
        // TODO Auto-generated method stub
        return false;
    }
}

Sample Main class:

package com.vipin.app;

import com.vipin.dao.DBOpsDao;
import com.vipin.dao.DefaultDBOpsDaoImpl;
import com.vipin.model.User;

public class MainApp {

    public static void main(String[] args) {

        System.out.println("Inside main...");
        DBOpsDao dao = new DefaultDBOpsDaoImpl();

        User user = new User();
        user.setFirstName("xxx");
        user.setLastName("yyy");
        user.setSsn(1);
        user.setEmailId("[email protected]");

        dao.add(user);
    }

}

I am using maven to build this, so the java source code is in:

src/main/java (top level package com.vipin)

When i run this program it is throwing exception complaining that spring.xml doesn't exist. I used ApplicationContext, one of implementation ClassPathXmlApplicationContext.

In which location do i need to put spring.xml file?

Any inputs would be helpful.

Upvotes: 2

Views: 13321

Answers (3)

StevenWernerCS
StevenWernerCS

Reputation: 868

Place the xml file at the root of your classpath

For maven that is src/main/resources/ if the directory doesn't exist yet, create it.

src/main/resources/applicationContext.xml

Also src/main/resources/spring/ works as well.

Upvotes: 0

asg
asg

Reputation: 2456

You will need to add spring.xml file at the location - src/main/resources folder. You can have your directory structure inside this directory as - src/main/resources/com/vipin/dao.

src/main/java directory is preferred for java classes.

If you are debugging from eclipse, make sure that you are adding your folder in the project's classpath.

<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
   <attributes>
      <attribute name="maven.pomderived" value="true"/>
   </attributes>
</classpathentry>

If you create your maven project using maven archetype and you import into eclipse, you need to edit your .classpath file.

Upvotes: 3

Efe Kahraman
Efe Kahraman

Reputation: 1438

You have to initilalize application context properly in your main method. You can check this link for example.

Upvotes: 1

Related Questions