Spring NullPointerException与带有@EnableAspectJAutoProxy的@Autowired对象

时间:2019-05-09 13:44:50

标签: java spring dependency-injection inversion-of-control

我是spring框架的新手,并且在尝试使用注入的依赖项时遇到NullPointerException异常。这是我的代码:

DemoConfig:

@Configuration
@EnableAspectJAutoProxy
@ComponentScan("com.trial.classes")
public class DemoConfig {

}

AccountDAO:

@Component
public class AccountDAO {

    @Autowired
    TestClass test;    // the problem: the object is null!


}

TestClass:

@Component
public class TestClass {
    public void testMethod()
    {
        System.out.println("this is a test method");
    }

}

主要:

public static void main(String[] args) {


        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(DemoConfig.class);


    AccountDAO theAccountDAO = context.getBean("accountDAO", AccountDAO.class);

    theAccountDAO.test.testMethod();    //NullPointerException



        context.close();
    }

0 个答案:

没有答案