我编写了一个示例应用程序,我试图将我的 CustomerService bean Autowiring 到我的 Application.class 中以执行 System.out.println()
但是它抛出了一个异常,说工厂层次结构的根,我不明白为什么我不能这样做,除了使用注释代码检索 bean?
我的错误
Aug 03, 2014 9:52:42 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5f3d285f: startup date [Sun Aug 03 21:52:42 SGT 2014]; root of context hierarchy
Aug 03, 2014 9:52:42 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Aug 03, 2014 9:52:43 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@25bcb56b: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,customerRepository,customerService,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
we are using constructor injection
Exception in thread "main" java.lang.NullPointerException
at Application.print(Application.java:20)
at Application.main(Application.java:30)
这是我的代码。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.pluralsight.service.CustomerService;
public class Application {
@Autowired
private CustomerService service;
public Application(CustomerService service) {
this.service = service;
}
public Application() {
}
public void print() {
System.out.println(service.findAll().get(0).getFirstName());
}
public static void main(String[] args) {
//ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//CustomerService service = context.getBean("customerService", CustomerService.class);
//System.out.println(service.findAll().get(0).getFirstName());
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Application application = new Application();
application.print();
}
}
这是我的应用程序上下文
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.pluralsight"></context:component-scan>
</beans>
我的客户服务类(class)
package com.pluralsight.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pluralsight.model.Customer;
import com.pluralsight.repository.CustomerRepository;
@Service("customerService")
public class CustomerServiceImpl implements CustomerService {
//@Autowired
private CustomerRepository customerRepository;
@Autowired
public CustomerServiceImpl(CustomerRepository customerRepository) {
System.out.println("we are using constructor injection");
this.customerRepository = customerRepository;
}
//@Autowired
public void setCustomerRepository(CustomerRepository customerRepository) {
System.out.println("we are using setter injection");
this.customerRepository = customerRepository;
}
/* (non-Javadoc)
* @see com.pluralsight.service.CustomerService#findAll()
*/
public List<Customer> findAll() {
return customerRepository.findAll();
}
}
最佳答案
你需要移动
@Autowired
private CustomerService service;
在应用程序类之外。
要使 Autowiring 工作,所有“连线”类必须由 Spring 容器实例化,但 Application 类已经在容器之前实例化,因此您无法将容器管理的依赖项连接到其中。
关于java - 应用程序上下文+ Autowiring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25105359/