【问题标题】:Why @Cacheable not working when calling cacheable method from method of non bean class为什么从非bean类的方法调用可缓存方法时@Cacheable不起作用
【发布时间】:2015-03-28 17:04:35
【问题描述】:

当我从非 bean 类内部的方法调用可缓存方法时,我突然发现 @Cacheable 不起作用。

请在下面找到我的代码并帮助我解决问题或我想念的东西。

EmployeeDAO.java

@Component("employeeDAO")
public class EmployeeDAO {
private static EmployeeDAO staticEmployeeDAO;

public static EmployeeDAO getInstance(){
    return staticEmployeeDAO;
}

@PostConstruct
void initStatic(){
    staticEmployeeDAO = this;
}

@Cacheable(value = "employeeCache")
public List<Employee> getEmployees() {
    Random random = new Random();
    int randomid = random.nextInt(9999);
    System.out.println("*** Creating a list of employees and returning the list ***");
    List<Employee> employees = new ArrayList<Employee>(5);
    employees.add(new Employee(randomid, "Ben", "Architect"));
    employees.add(new Employee(randomid + 1, "Harley", "Programmer"));
    employees.add(new Employee(randomid + 2, "Peter", "BusinessAnalyst"));
    employees.add(new Employee(randomid + 3, "Sasi", "Manager"));
    employees.add(new Employee(randomid + 4, "Abhi", "Designer"));
    return employees;
}    

MyThread.java

class MyThread{
public void run(){
    //How to get Employee data. ?????
}
}    

UtilityClass.java

public class UtilityClass {
public static void getEmployee(){
    EmployeeDAO.getInstance().getEmployees();
}
}    

Main.java

public class Main {


public static void main(String[] args) {

    ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    EmployeeDAO dao = (EmployeeDAO)context.getBean("employeeDAO");

    System.out.println("1'st call");
    dao.getEmployees();
    System.out.println("2'nd call");
    dao.getEmployees();
    System.out.println("Call cache method using utility class");

    System.out.println("1'st call on utilityclass");
    UtilityClass.getEmployee();
    System.out.println("2'nd call on utilityclass");
    UtilityClass.getEmployee();
}
}    

输出:

1'st call
*** Creating a list of employees and returning the list ***
2'nd call
Call cache method using utility class
1'st call on utilityclass
*** Creating a list of employees and returning the list ***
2'nd call on utilityclass
*** Creating a list of employees and returning the list ***  

谁能帮帮我?

【问题讨论】:

标签: java multithreading spring caching ehcache


【解决方案1】:

Spring 使用代理来应用 AOP,但是代理是在构建 bean 之后创建的。

在您的 @PostConstruct 注释方法中,您正在设置对 this 的引用,但此时这是 bean 的未代理实例。你真的需要代理实例。

我还要指出,恕我直言,您的解决方案非常糟糕,无法通过我的 QA 检查。但那是恕我直言。

【讨论】:

  • @Denium 如何从我的实用程序类或线程中的不是 bean 的数据库中获取数据?我不让豆子成为我的主线
  • 找到一种方法来获取上下文,检索 bean 并使用它。但是,我认为您尝试做的是一个很好的解决方案...检索数据的静态方法...看起来您正在尝试发明自己的缓存层。
  • @Deinum 感谢您的帮助,目前我很难在我的线程类中获取数据,所以您能否建议和共享代码以在我的线程上检索员工数据。是在我的每个线程类中获取上下文并从中获取 bean 的好方法吗?
  • 你为什么不把服务注入你的班级。为什么是丑陋的静态东西。
  • @Deinum 请忘记静态的东西,但是线程类呢。由于我的线程不是 bean,如何在我的线程中注入服务类?
猜你喜欢
  • 1970-01-01
  • 2020-12-29
  • 1970-01-01
  • 2013-05-29
  • 2012-08-20
  • 2010-12-29
  • 1970-01-01
  • 1970-01-01
  • 2019-05-08
相关资源
最近更新 更多