Java 为什么@Cacheable在从非bean类的方法调用Cacheable方法时不工作

Java 为什么@Cacheable在从非bean类的方法调用Cacheable方法时不工作,java,multithreading,spring,caching,ehcache,Java,Multithreading,Spring,Caching,Ehcache,当我从NOTBean类中的方法调用Cacheable方法时,我突然发现@Cacheable不起作用 请在下面找到我的代码,并帮助我什么是问题或什么我错过了 EmployeeDAO.java @Component("employeeDAO") public class EmployeeDAO { private static EmployeeDAO staticEmployeeDAO; public static EmployeeDAO getInstance(){ return sta

当我从NOTBean类中的方法调用Cacheable方法时,我突然发现@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;
}    
class MyThread{
public void run(){
    //How to get Employee data. ?????
}
}    
public class UtilityClass {
public static void getEmployee(){
    EmployeeDAO.getInstance().getEmployees();
}
}    
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();
}
}    
UtilityClass.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;
}    
class MyThread{
public void run(){
    //How to get Employee data. ?????
}
}    
public class UtilityClass {
public static void getEmployee(){
    EmployeeDAO.getInstance().getEmployees();
}
}    
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();
}
}    
Main.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;
}    
class MyThread{
public void run(){
    //How to get Employee data. ?????
}
}    
public class UtilityClass {
public static void getEmployee(){
    EmployeeDAO.getInstance().getEmployees();
}
}    
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 ***  

有人能帮我吗

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

@PostConstruct
带注释的方法中,您正在设置对
的引用,但此时它是bean的未经验证的实例。您确实需要代理实例


我还想指出,你的解决方案非常糟糕,无法通过我的QA检查。但这就是伊姆霍

检查这个:这和我在这里描述的问题和解决方案是一样的@Denium如何从我的工具类或线程中的数据库中获取数据,而这些不是bean?我不想让bean成为我的线程,而是想办法获取上下文,检索bean并使用它。然而,我认为你尝试做的是一个好的解决方案。。。检索数据的静态方法。。。看起来您正试图发明自己的缓存层。@Deinum谢谢您的帮助,目前我很难在我的线程类中获取数据,因此您可以建议并共享在我的线程中检索员工数据的代码。在每个my thread类中都是获取上下文和bean的好方法吗?为什么不将服务注入到类中呢。为什么是丑陋的静态内容。@Deinum请忘记静态内容,但是线程类呢。由于我的线程不是bean,我如何在线程中注入服务类?