我是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();
}