关于spring的注解问题

清晨v 2017-08-08 10:16:11
问下spring大神们,以注解的方式管理bean和在xml配置能同时存在吗?为啥我就报错了呢
错误信息如下:
严重: 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 'clearDataImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.jiezisoft.opermanager.service.ServerStatusInfoService com.jiezisoft.opermanager.service.impl.ClearDataImpl.serverStatusInfoService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.jiezisoft.opermanager.service.ServerStatusInfoService] 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:289)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:410)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5077)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5591)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:677)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1939)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.jiezisoft.opermanager.service.ServerStatusInfoService com.jiezisoft.opermanager.service.impl.ClearDataImpl.serverStatusInfoService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.jiezisoft.opermanager.service.ServerStatusInfoService] 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$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:517)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
... 26 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.jiezisoft.opermanager.service.ServerStatusInfoService] 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.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:988)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:858)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489)
... 28 more
八月 07, 2017 8:19:49 下午 org.apache.catalina.core.StandardContext startInternal


service层的代码如下:
@Transactional
@Service(value = "clearDataImpl")
public class ClearDataImpl
{
private static final Log log = LogFactory.getLog(ClearDataImpl.class);
@Autowired
public ServerStatusInfoService serverStatusInfoService;

@Autowired
public ServiceStatusInfoHisService serviceStatusInfoHisService;

//清理5天前的服务器状态信息
public void clearServerStatusData(String updateTime)
{

serverStatusInfoService.clearDataInTime(updateTime);
}

public void clearServerStatusData()
{
log.debug("==========定时执行1=======");
serverStatusInfoService.clearDataInTime(null);
}

public void clearServiceStatusInfoHisData(String updateTime)
{
serviceStatusInfoHisService.clearDataInTime(updateTime);
}

public void clearServiceStatusInfoHisData()
{
log.debug("==========定时执行2=======");
serviceStatusInfoHisService.clearDataInTime(null);
}
}


spring配置如下:


<task:annotation-driven/>
<bean id="clearDataImpl1" class="com.jiezisoft.opermanager.service.impl.ClearDataImpl"></bean>
<task:scheduled-tasks>
<task:scheduled ref="clearDataImpl" method="clearServerStatusData" cron="0/30 * * * * ?"/><!-- 每天0点执行 -->
<task:scheduled ref="clearDataImpl" method="clearServiceStatusInfoHisData" cron="0/30 * * * * ?"/><!-- 每天0点执行 -->
<!-- <task:scheduled ref="BatDealServiceImpl" method="batDealTask" cron="0/30 * * * * ? "/> --><!-- 30秒执行1次 -->
<!-- <task:scheduled ref="BatDealServiceImpl" method="batDealTask" cron="0 0,30 * * * ? "/> --><!-- 半小时执行一次,00分,30分 -->
</task:scheduled-tasks>

...全文
335 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
strong_mz 2017-08-08
  • 打赏
  • 举报
回复
如果找不到 你就把spring的配置文件贴出来看看 应该就是那个类没加入spring管理的问题
浮云若水 2017-08-08
  • 打赏
  • 举报
回复
采用了注解 你是否配置了注解扫描 ,配置了注解扫描的包下面是否包含 @Autowired public ServerStatusInfoService serverStatusInfoService; @Autowired public ServiceStatusInfoHisService serviceStatusInfoHisService; 这两个service ,如果有你这两个service是否有设置注解 从异常信息来看,是这两个bean实例不在spring容器中,而你当前service中依赖,导致bean无法装配 所以报错 好好检查下代码
qq_35942223 2017-08-08
  • 打赏
  • 举报
回复
看看这个类ServerStatusInfoService
清晨v 2017-08-08
  • 打赏
  • 举报
回复
引用 1 楼 qq_38740863 的回复:
ServerStatusInfoService这个类加入spring管理了么 在注入这个类的时候发生的错误
在他的实现类里有@Service 注解,应该是管理了吧?
strong_mz 2017-08-08
  • 打赏
  • 举报
回复
ServerStatusInfoService这个类加入spring管理了么 在注入这个类的时候发生的错误
清晨v 2017-08-08
  • 打赏
  • 举报
回复

   package com.xxxx.opermanager.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xxxx.opermanager.common.util.DateUtil;
import com.xxxx.opermanager.common.util.PropertyPlaceholder;
import com.xxxx.opermanager.dao.ServerStatusInfoDao;
import com.xxxx.opermanager.dao.ServiceStatusInfoHisDao;
import com.xxxx.opermanager.service.ServerStatusInfoService;
import com.xxxx.opermanager.service.ServiceStatusInfoHisService;

/***
 * 
* @ClassName: ClearDataImpl 
* @Description: 清理数据
* @author yinjh 
* @date 2016-10-9 下午4:53:53 
* @version 1.0
 */
@Transactional
@Service(value = "clearDataImpl")
public class ClearDataImpl implements ClearData
{
    private static final Log log = LogFactory.getLog(ClearDataImpl.class);
    
    @Autowired
    private ServerStatusInfoDao serverStatusInfoDao;
    
    @Autowired
    private ServiceStatusInfoHisDao serviceStatusInfoHisDao;
    
    @Override
    public void clearServerStatusData()
    {
        log.debug("==========定时清理服务器状态=======执行时间:"+DateUtil.date2String(new Date()));
        int date = Integer.parseInt(PropertyPlaceholder.getProperty("clear_data"));
        String updateTime = DateUtil.date2String(DateUtil.addDays(new Date(), date * -1));
        serverStatusInfoDao.clearDataInTime(updateTime);
    }
    
    @Override
    public void clearServiceStatusInfoHisData()
    {
        log.debug("==========定时清理服务状态=======执行时间"+DateUtil.date2String(new Date()));
        int date = Integer.parseInt(PropertyPlaceholder.getProperty("clear_data"));
        String updateTime = DateUtil.date2String(DateUtil.addDays(new Date(), date * -1));
        serviceStatusInfoHisDao.clearDataInTime(updateTime);
    }
}

浮云若水 2017-08-08
  • 打赏
  • 举报
回复
引用 14 楼 youxiaoyue 的回复:
[quote=引用 13 楼 a276202031 的回复:] [quote=引用 10 楼 youxiaoyue 的回复:] [quote=引用 9 楼 a276202031 的回复:] [quote=引用 7 楼 youxiaoyue 的回复:] [quote=引用 5 楼 qq_38740863 的回复:] 如果找不到 你就把spring的配置文件贴出来看看 应该就是那个类没加入spring管理的问题
见楼上,帮忙看下哦,实在不知道啥问题。。。。[/quote] 或者用@Resource 代替@Autowired 试试 [/quote] @Component 和 @Resource 都试了下,都不行啊,[/quote] 那就在@Autowired 上面加上@Qualifier("serverStatusInfoService") 。。。。试下 如果还不行 那就是你的写法有问题了 一般来说service中只调用持久层的 你这样service层调用service层很是混乱。把代码结构调整下,分别调用@Autowired public ServerStatusInfoService serverStatusInfoService; @Autowired public ServiceStatusInfoHisService serviceStatusInfoHisService;这两个service对应的持久层来做你的业务逻辑处理,别service嵌套 [/quote] 确实是这个service写法问题,我直接调用dao就好了,非常感谢![/quote] 解决了就好
清晨v 2017-08-08
  • 打赏
  • 举报
回复
引用 13 楼 a276202031 的回复:
[quote=引用 10 楼 youxiaoyue 的回复:] [quote=引用 9 楼 a276202031 的回复:] [quote=引用 7 楼 youxiaoyue 的回复:] [quote=引用 5 楼 qq_38740863 的回复:] 如果找不到 你就把spring的配置文件贴出来看看 应该就是那个类没加入spring管理的问题
见楼上,帮忙看下哦,实在不知道啥问题。。。。[/quote] 或者用@Resource 代替@Autowired 试试 [/quote] @Component 和 @Resource 都试了下,都不行啊,[/quote] 那就在@Autowired 上面加上@Qualifier("serverStatusInfoService") 。。。。试下 如果还不行 那就是你的写法有问题了 一般来说service中只调用持久层的 你这样service层调用service层很是混乱。把代码结构调整下,分别调用@Autowired public ServerStatusInfoService serverStatusInfoService; @Autowired public ServiceStatusInfoHisService serviceStatusInfoHisService;这两个service对应的持久层来做你的业务逻辑处理,别service嵌套 [/quote] 确实是这个service写法问题,我直接调用dao就好了,非常感谢!
浮云若水 2017-08-08
  • 打赏
  • 举报
回复
引用 10 楼 youxiaoyue 的回复:
[quote=引用 9 楼 a276202031 的回复:] [quote=引用 7 楼 youxiaoyue 的回复:] [quote=引用 5 楼 qq_38740863 的回复:] 如果找不到 你就把spring的配置文件贴出来看看 应该就是那个类没加入spring管理的问题
见楼上,帮忙看下哦,实在不知道啥问题。。。。[/quote] 或者用@Resource 代替@Autowired 试试 [/quote] @Component 和 @Resource 都试了下,都不行啊,[/quote] 那就在@Autowired 上面加上@Qualifier("serverStatusInfoService") 。。。。试下 如果还不行 那就是你的写法有问题了 一般来说service中只调用持久层的 你这样service层调用service层很是混乱。把代码结构调整下,分别调用@Autowired public ServerStatusInfoService serverStatusInfoService; @Autowired public ServiceStatusInfoHisService serviceStatusInfoHisService;这两个service对应的持久层来做你的业务逻辑处理,别service嵌套
梨花剑君 2017-08-08
  • 打赏
  • 举报
回复
帮忙顶贴,不明觉厉。
xiangbei_ 2017-08-08
  • 打赏
  • 举报
回复
我觉得可能是你spring配置文件没加扫描
清晨v 2017-08-08
  • 打赏
  • 举报
回复
引用 9 楼 a276202031 的回复:
[quote=引用 7 楼 youxiaoyue 的回复:] [quote=引用 5 楼 qq_38740863 的回复:] 如果找不到 你就把spring的配置文件贴出来看看 应该就是那个类没加入spring管理的问题
见楼上,帮忙看下哦,实在不知道啥问题。。。。[/quote] 或者用@Resource 代替@Autowired 试试 [/quote] @Component 和 @Resource 都试了下,都不行啊,
浮云若水 2017-08-08
  • 打赏
  • 举报
回复
引用 7 楼 youxiaoyue 的回复:
[quote=引用 5 楼 qq_38740863 的回复:] 如果找不到 你就把spring的配置文件贴出来看看 应该就是那个类没加入spring管理的问题
见楼上,帮忙看下哦,实在不知道啥问题。。。。[/quote] 或者用@Resource 代替@Autowired 试试
浮云若水 2017-08-08
  • 打赏
  • 举报
回复
引用 7 楼 youxiaoyue 的回复:
[quote=引用 5 楼 qq_38740863 的回复:] 如果找不到 你就把spring的配置文件贴出来看看 应该就是那个类没加入spring管理的问题
见楼上,帮忙看下哦,实在不知道啥问题。。。。[/quote] 在service实现类上加个@Component 注解试试 配置没啥问题
清晨v 2017-08-08
  • 打赏
  • 举报
回复
引用 5 楼 qq_38740863 的回复:
如果找不到 你就把spring的配置文件贴出来看看 应该就是那个类没加入spring管理的问题
见楼上,帮忙看下哦,实在不知道啥问题。。。。
清晨v 2017-08-08
  • 打赏
  • 举报
回复
引用 4 楼 a276202031 的回复:
采用了注解 你是否配置了注解扫描 ,配置了注解扫描的包下面是否包含 @Autowired public ServerStatusInfoService serverStatusInfoService; @Autowired public ServiceStatusInfoHisService serviceStatusInfoHisService; 这两个service ,如果有你这两个service是否有设置注解 从异常信息来看,是这两个bean实例不在spring容器中,而你当前service中依赖,导致bean无法装配 所以报错 好好检查下代码
这2个service的实现类ServerStatusInfoServiceImpl 都有注解

package com.jiezisoft.opermanager.service.impl;
import 省略~~~~
@Service//(value = "serverStatusInfoService")
public class ServerStatusInfoServiceImpl implements ServerStatusInfoService {
   -----方法省略
}
springMVC的配置

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<context:annotation-config/>
	<context:component-scan base-package="com.jiezisoft.opermanager">
	      <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
	</context:component-scan>
	
	 <!-- 对静态资源文件的访问 -->
    <mvc:resources  mapping="/static/**" location="/static/"/>
    <!-- 对静态资源文件的访问, 将无法mapping到Controller的path交给default servlet handler处理 -->
    <mvc:default-servlet-handler/>
    <!-- <aop:aspectj-autoproxy proxy-target-class="true"/> -->
    <aop:aspectj-autoproxy /> 
	<mvc:annotation-driven validator="validator">
     <mvc:message-converters register-defaults="true">
        <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>  
        <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
            <property name="supportedMediaTypes">  
                <list>  
                    <value>text/html;charset=UTF-8</value>  
                </list>  
            </property>  
        </bean> 
     
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
	      <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
	    </bean>
	   </mvc:message-converters>
	
	</mvc:annotation-driven>
    
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 配置jsp路径的前缀 -->
		<property name="prefix" value="/WEB-INF/jsp/" />
		<!-- 配置jsp路径的后缀 -->
		<property name="suffix" value=".jsp" />
	</bean>
	 <!-- 加载properties文件配置信息 -->
    <bean id="propertyPlaceholder" scope="singleton" class="com.jiezisoft.opermanager.common.util.PropertyPlaceholder">
        <property name="locations">
            <list>
                <value>classpath:application.properties</value>
            </list>
        </property>
    </bean>
    <!-- 校验器 -->
	<bean id="validator"
		class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
		<!-- hibernate校验器-->
		<property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
		<!-- 指定校验使用的资源文件,在文件中配置校验错误信息,如果不指定则默认使用classpath下的ValidationMessages.properties -->
		<property name="validationMessageSource" ref="messageSource" />
	</bean>
	
    <!-- 校验错误信息配置文件 -->
	<bean id="messageSource"
		class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
		<!-- 资源文件名-->
		<property name="basenames">   
       	 <list>    
            <value>classpath:validator/*</value> 
       	 </list>   
    	</property>
		<!-- 资源文件编码格式 -->
		<property name="fileEncodings" value="utf-8" />
		<!-- 对资源文件内容缓存时间,单位秒 -->
		<property name="cacheSeconds" value="120" />
	</bean>
	<!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->  
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <property name="defaultEncoding" value="UTF-8"/>  
        <!-- 指定所上传文件的总大小不能超过5000KB。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->  
        <property name="maxUploadSize" value="100000000"/>  
    </bean>  
	<!-- SpringMVC在超出上传文件限制时,会抛出org.springframework.web.multipart.MaxUploadSizeExceededException -->  
    <!-- 该异常是SpringMVC在检查上传的文件信息时抛出来的,而且此时还没有进入到Controller方法中 -->  
    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
        <property name="exceptionMappings">  
            <props>  
                <!-- 遇到MaxUploadSizeExceededException异常时,自动跳转到/WEB-INF/jsp/error_fileupload.jsp页面 -->  
                <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error/error_fileupload</prop>  
            </props>  
        </property>  
    </bean>  
	<!-- 全局异常处理器 -->
	<bean class="com.jiezisoft.opermanager.exception.CustomExceptionResolver"></bean>
</beans>

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧