我无法使用@Autowired
因为我在运行项目后出错了。错误写Error creating bean with name 'pupilServiceImpl'
但我没有用这个名字创建bean
stacktrace tomcat log
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pupilController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private service.implement.PupilServiceImpl controller.PupilController.pupilServiceImpl; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pupilServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private dao.implement.PupilDAOImpl service.implement.PupilServiceImpl.pupilDAOImpl; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pupilDAOImpl' defined in file [/opt/tomcat/apache-tomcat-8.0.24/webapps/ROOT/WEB-INF/classes/dao/implement/PupilDAOImpl.class]: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private service.implement.PupilServiceImpl controller.PupilController.pupilServiceImpl; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pupilServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private dao.implement.PupilDAOImpl service.implement.PupilServiceImpl.pupilDAOImpl; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pupilDAOImpl' defined in file [/opt/tomcat/apache-tomcat-8.0.24/webapps/ROOT/WEB-INF/classes/dao/implement/PupilDAOImpl.class]: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError
at ...
... 58 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pupilServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private dao.implement.PupilDAOImpl service.implement.PupilServiceImpl.pupilDAOImpl; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pupilDAOImpl' defined in file [/opt/tomcat/apache-tomcat-8.0.24/webapps/ROOT/WEB-INF/classes/dao/implement/PupilDAOImpl.class]: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError
...
... 73 more
Caused by: java.lang.ExceptionInInitializerError
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1098)
... 84 more
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.postgresql.Driver]
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:245)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.loadDriverIfPossible(DriverManagerConnectionProviderImpl.java:200)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildCreator(DriverManagerConnectionProviderImpl.java:156)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:95)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)
at dao.implement.PupilDAOImpl.<clinit>(PupilDAOImpl.java:20)
... 91 more
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.postgresql.Driver
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:230)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:242)
... 106 more
控制器
@RestController
public class PupilController {
@Autowired
private PupilServiceImpl pupilService;
public void setPupilService(PupilServiceImpl pupilService) {
this.pupilService = pupilService;
}
public PupilController(){
}
...
}
服务类
@Service
public class PupilServiceImpl implements PupilService {
@Autowired
private PupilDAOImpl pupilDAO;
public PupilServiceImpl() {
}
public void setPupilDAO(PupilDAOImpl pupilDAO) {
this.pupilDAO = pupilDAO;
}
...
}
弹簧servlet.xml中
<?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:p="http://www.springframework.org/schema/p"
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.xsd">
<context:component-scan base-package="controller"/>
<context:component-scan base-package="dao.implement"/>
<context:component-scan base-package="service.implement"/>
<context:annotation-config/>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="pupilService" class="service.implement.PupilServiceImpl" autowire="byName"/>
<bean id="pupilDAO" class="dao.implement.PupilDAOImpl" autowire="byName"/>
</beans>
的web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
如果我创建classpathxmlcontext我有新的错误
public PupilController(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
TEstst tEstst = (TEstst)context.getBean("tEstst");
tEstst.print();
System.out.println("hello");
}
栈跟踪
SEVERE [RMI TCP Connection(2)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Context initialization failed
java.lang.NoClassDefFoundError: org.springframework.beans.FatalBeanException
答案 0 :(得分:1)
看来你是通过组件扫描创建Spring Beans并在spring-servlet.xml中声明它们。
你应该只执行其中一个。
此外,您的DAO似乎想要一个DataSource,而DataSource又需要PostgreSQL的驱动程序。