-2

I have a main class in which i am creating object of a class which connects to the database. How can i use this object in a controller class ?

public class A {

    public static void main(String[] args) {
            // TODO Auto-generated method stub

        connectToDb x=new connectToDb();  // makes a connection to database


    }

now i want to use this object in spring controller class which has a method

@RequestMapping(value = "/whatever/, method = RequestMethod.GET)
        public @ResponseBody Object getFilteredLogs() {
            ....

        }

so how can i use that object in this controller class ?

Iroch
  • 19
  • 6

1 Answers1

0

Since this is a Spring application, you should consider autowirering. If the class connecttoDB is annotated with @Service or @Repository you can put

@Inject connecttoDB;

In your controller, ant Spring will create a connecttoDB singleton and inject it.

I would also look at spring Data, but that is not necessary to solve your current problem.

LarsErik
  • 138
  • 1
  • 7