1

Currently i am troubling that how to get data from database inside my thread class.

I can't make bean of my thread class because new instance is created of thread on every request.

MyThread.class

class MyThread implements Runnable{
public void run(){
    //How to get Employee data and also data from other tables.??????
}
}    

EmployeeDAO.class

@Component("employeeDAO")
public class EmployeeDAO {

@Cacheable(value = "employeeCache")
public List<Employee> getEmployees() {
     //got data from database
    return employees;
}
}

Is this good to get context and get dao bean from context and use in every my thread class?

Can you suggest or share code how to solve my above problem?

Kamlesh Kanazariya
  • 1,209
  • 2
  • 15
  • 32
  • 3
    Make `EmployeeDAO` available in the class that instantiates `MyThread`, then pass the DAO into `MyThread` (ideally via the constructor) before running it. – Mike Partridge Jan 29 '15 at 14:29
  • @MikePartridge Is there another solution rather suggested by you because i don't want to pass every dao which required in my thread to constructor ? can you imagine that above solution is bad when i have required to get data from multiple tables inside my thread – Kamlesh Kanazariya Jan 30 '15 at 07:06
  • Use the constructor. Otherwise, you're creating implicit dependencies that aren't clear in your object's API and are just asking for subtle environment bugs. – chrylis -cautiouslyoptimistic- Jan 30 '15 at 07:18
  • @chrylis If I have required to use multiple DAO inside my thread then **Is this good to pass every DAO inside constructor**? – Kamlesh Kanazariya Jan 30 '15 at 07:25
  • How many DAOs are you using? If you're using a whole lot of them, then you probably need to decompose your process. If they really are all necessary, then yes, inject them all. I routinely need to inject three or four into classes for ordinary business operations. – chrylis -cautiouslyoptimistic- Jan 30 '15 at 08:38
  • @chrylis Currently i have require 5 or 6 DAO to use in thread but It is not good to pass DAO inside constructor because if I have required to fetch data from another new database then **i have to change constructor of thread which is not good** – Kamlesh Kanazariya Jan 30 '15 at 10:17
  • 1
    That's exactly the kind of unreliable design I mean by "implicit dependencies". If your class needs new dependencies, then the constructor should reflect them. – chrylis -cautiouslyoptimistic- Jan 30 '15 at 11:10
  • Maybe not the most ideal solution, but in Spring you can inject a `Map` of beans easily, given they all implement the same interface or extend the same class. Make all your DAOs implement an interface, then autowire a `Map` to the bean that instantiates your thread. The key would be the name of the bean, and the value would be the actual DAO bean instance. Pass that map as an argument to the constructor of `MyThread`, and then, inside it, use the DAOs you want by retrieving them from the `Map` using `map.get(beanName)`. – fps Jan 30 '15 at 11:50
  • **Is there another way that i can get solution ?** Is this possible to make `static getInstance()` method which return my dao and i use inside `MyThread` by calling `getInstance()` method – Kamlesh Kanazariya Jan 30 '15 at 12:01

1 Answers1

1

I have found one solution from this site .

Is below solution is good or not? If not good then what are disadvantage of solution

Here is what I tried and got success

@Service
public class StaticContextHolder implements ApplicationContextAware {

public static ApplicationContext CONTEXT;

public StaticContextHolder() {
}

public static Object getBean(String s) {
    return CONTEXT.getBean(s);
}

public static <T> T getBean(String s, Class<T> tClass){
    return CONTEXT.getBean(s, tClass);
}

public static <T> T getBean(Class<T> tClass){
    return CONTEXT.getBean(tClass);
}

public static Object getBean(String s, Object... objects){
    return CONTEXT.getBean(s, objects);
}

public static boolean containsBean(String s) {
    return CONTEXT.containsBean(s);
}

@Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
    System.out.println("ApplicationContext initialized");
    CONTEXT = arg0;
}   
}    


class MyThread implements Runnable{
public void run(){
     EmployeeDAO employeeDAO = StaticContextHolder.getBean(EmployeeDAO.class);
}
} 
Kamlesh Kanazariya
  • 1,209
  • 2
  • 15
  • 32
  • This is the `Singleton` (anti-)pattern. For a discussion, see this question: http://stackoverflow.com/questions/11292109/why-implementing-a-singleton-pattern-in-java-code-is-sometimes-considered-an-a – Ian McLaird Jan 30 '15 at 14:42