java项目编译正常,但在tomcat中发布时,报错,错误信息如下:
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘XXXXX’: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: XXXXXXX; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [XXXXXX] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)
原因是在使用@Autowired注解时,找不到相应的类,产生上述问题是因为XXXXX的实现类中没有加相应的注解。
例如DAO层 @Repository
例如Service层 @Service
解决方法:
在Service层的类上追加代码:
@Service("XXXXX")
在service的实现类XXXXXServiceImpl.java的开始,追加@Service("XXXXX")
,如下:
@Service("XXXXX")
public class XXXXXServiceImpl implements XXXXXService{
......
}
在DAO层:
@Repository
@Autowired
public @interface MyBatisDAO {
}
package com.keymen.dao;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.keymen.annotation.MyBatisDAO;
import com.keymen.entity.User;
@MyBatisDAO
public interface UserDao {
@Autowired
User selectByPrimaryKey(Integer id);
@Autowired
List<User> queryAllUser();
@Autowired
public User findUserByUsername(String username);
@Autowired(required=true)
public Integer getCountOfRecord();
}
代码看起来像这样。