开发者社区 问答 正文

spring如何管理普通类?报错

小弟对spring不熟,下面的代码就是我的普通类,一个在util包下的普通类  LuceneUtil这个类没在spring配置
ICrmQuotationService这个service在 action中使用说明是配置了的
package com.erp.travel.common.utils;

import java.util.List;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.erp.travel.base.model.Article;
import com.erp.travel.module.crmQuotation.service.ICrmQuotationService;

/**
 * 创建索引库
 * @author bzg
 */
public class LuceneUtil {

	ApplicationContext applicationContext = 
			new ClassPathXmlApplicationContext("com/erp/travel/cfg/spring/applicationContext.xml");
	@Test
	public void createIndexWriter(){
		ICrmQuotationService crmQuotationService =
				(ICrmQuotationService) applicationContext.getBean("");
		/**
		 * 查询索引库内容
		 */
		List<Article> article = crmQuotationService.findArticle();
	}
}

然后我执行就报错了


一下是我的applicationContex.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: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.0.xsd 
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

	<!-- DB配置文件读入-->
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>
					classpath:com/erp/travel/cfg/database/jdbc.properties
				</value>
			</list>
		</property>
	</bean>

	<!-- 配置数据源-->
	<bean id="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="${jdbc.driverClassName}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="minPoolSize" value="1" />
		<property name="maxPoolSize" value="300" />
		<property name="maxIdleTime" value="60" />
		<property name="acquireIncrement" value="5" />
		<property name="initialPoolSize" value="1" />
		<property name="idleConnectionTestPeriod" value="60" />
		
	</bean>

   <!-- 为SqlMap绑定数据源 -->
	<bean id="sqlMapClient"
		class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation">
			<value>
				classpath:/com/erp/travel/cfg/ibatis/sqlMapConfig.xml
			</value>
		</property>
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<!-- 事物管理配置 -->
	<bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
            <ref local="dataSource" />
        </property>
    </bean>

    <aop:config>
        <!-- 切入点指明在所有方法产生事务时的拦截操作,这里只拦截业务逻辑层-->
        <aop:pointcut id="serviceMethods"
            expression="execution(* com.erp.travel.module.*.service.*.*(..))" />
        <!-- 定义了将采用何种拦截操作,这里引用到 txAdvice -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" />
    </aop:config>
    
    <!-- 事务的通知操作,使用的事务管理器引用自 transactionManager -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 指定哪些方法需要加入事务,这里使用通配符只加入需要拦截的方法 -->
            <tx:method name="save*"   propagation="REQUIRED" isolation="DEFAULT" rollback-for="-Exception"/>
	        <tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="-Exception"/>
	        <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="-Exception"/>
	        <tx:method name="modify*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="-Exception"/>
	        <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="-Exception"/>
	        <tx:method name="remove*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="-Exception"/>
	        <tx:method name="find*"   read-only="true"/>
	        <tx:method name="select*" read-only="true"/>
	        <tx:method name="get*" read-only="true"/>
	        <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    
  	<bean id="baseDao" class="com.erp.travel.common.dao.BaseDaoImpl">
		<property name="dataSource" ref="dataSource" />
		<property name="sqlMapClient" ref="sqlMapClient" />
	</bean>
	
	<!-- 加入Aspectj配置 -->  
     <aop:aspectj-autoproxy />  
        <bean id="logAspect" class="com.erp.travel.base.advice.SystemLog" /> 
	
	<context:component-scan base-package="com.erp.travel.base.dao.impl" />
	<context:component-scan base-package="com.erp.travel.module.*.service.impl" />
	<context:component-scan base-package="com.erp.travel.module.*.action" />
	<context:component-scan base-package="com.erp.travel.module.*.*.service.impl" />
	<context:component-scan base-package="com.erp.travel.module.*.*.action" />
	<context:component-scan base-package="com.erp.travel.common.*.action" />
</beans>





展开
收起
爱吃鱼的程序员 2020-06-14 18:17:21 663 分享 版权
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

     报错信息很明显,ICrmQuotationService未在spring容器了实例化<aclass='referer'target='_blank'>@ServicepublicinterfaceICrmQuotationService{我加上了还是报找不到这个service错误回复<aclass='referer'target='_blank'>@帅狗:<context:component-scanbase-package="com.erp.travel.module.*.service.impl"/>这段代表你扫描了service.impl包的class类,如这个包里有个类<aclass='referer'target='_blank'>@ServicepublicclassXXXservice{XXXX}那么spring会把XXXservice实例化回复<aclass='referer'target='_blank'>@Black_JackQ:不明白,spring学的不好回复<aclass='referer'target='_blank'>@帅狗:spring扫包时,要加入注解如在service层的class类上加入@Service注解,来引导spring实例化类。ICrmQuotationService如何在spring中配置<divclass='ref'>

    引用来自“Black_JackQ”的评论

     报错信息很明显,ICrmQuotationService未在spring容器了实例化<aclass='referer'target='_blank'>@ServicepublicinterfaceICrmQuotationService{我加上了还是报找不到这个service错误

    interface也能使用@service注解么?

    表示疑惑

    spring,我不太熟悉

    <divclass="linenumber22index21alt1"> ICrmQuotationServicecrmQuotationService=<spanstyle="line-height:1.5;"> (ICrmQuotationService)applicationContext.getBean(" <spanstyle="font-family:Consolas,'BitstreamVeraSansMono','CourierNew',Courier,monospace;font-size:14px;line-height:21px;background-color:#E53333;"> iCrmQuotationService  ");试试。接口好像不能实例化,我换成实现类

    2020-06-14 18:17:37
    赞同 展开评论