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?

asked Jan 29, 2015 at 14:19
9
  • 3
    Make EmployeeDAO available in the class that instantiates MyThread, then pass the DAO into MyThread (ideally via the constructor) before running it. Commented Jan 29, 2015 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 Commented Jan 30, 2015 at 7: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. Commented Jan 30, 2015 at 7:18
  • @chrylis If I have required to use multiple DAO inside my thread then Is this good to pass every DAO inside constructor? Commented Jan 30, 2015 at 7:25
  • 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. Commented Jan 30, 2015 at 11:10

1 Answer 1

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);
}
} 
answered Jan 30, 2015 at 13:17
Sign up to request clarification or add additional context in comments.

1 Comment

This is the Singleton (anti-)pattern. For a discussion, see this question: stackoverflow.com/questions/11292109/…

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.