Spring注解问题,[action中注入service失败

Posted 寻找孩她妈

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring注解问题,[action中注入service失败相关的知识,希望对你有一定的参考价值。

pring-mvc.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 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-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/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

	<!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
	<context:component-scan base-package="yzyq.controller" />

	<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
	<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>text/html;charset=UTF-8</value>
			</list>
		</property>
	</bean>

	<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->
			</list>
		</property>
	</bean>

	<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" />

	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding">
			<value>UTF-8</value>
		</property>
		<property name="maxUploadSize">
			<value>32505856</value><!-- 上传文件大小限制为31M,31*1024*1024 -->
		</property>
		<property name="maxInMemorySize">
			<value>4096</value>
		</property>
	</bean>

</beans>


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

	<!-- JNDI方式配置数据源 -->
	<!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> -->

	<!-- 配置数据源 -->
	<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
		<property name="url" value="${jdbc_url}" />
		<property name="username" value="${jdbc_username}" />
		<property name="password" value="${jdbc_password}" />

		<!-- 初始化连接大小 -->
		<property name="initialSize" value="0" />
		<!-- 连接池最大使用连接数量 -->
		<property name="maxActive" value="20" />
		<!-- 连接池最大空闲 -->
		<property name="maxIdle" value="20" />
		<!-- 连接池最小空闲 -->
		<property name="minIdle" value="0" />
		<!-- 获取连接最大等待时间 -->
		<property name="maxWait" value="60000" />

		<!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->

		<property name="validationQuery" value="${validationQuery}" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<property name="testWhileIdle" value="true" />

		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
		<property name="minEvictableIdleTimeMillis" value="25200000" />

		<!-- 打开removeAbandoned功能 -->
		<property name="removeAbandoned" value="true" />
		<!-- 1800秒,也就是30分钟 -->
		<property name="removeAbandonedTimeout" value="1800" />
		<!-- 关闭abanded连接时输出错误日志 -->
		<property name="logAbandoned" value="true" />

		<!-- 监控数据库 -->
		<!-- <property name="filters" value="stat" /> -->
		<property name="filters" value="mergeStat" />
		<!-- 连接属性 -->
		<propertyname="connectionProperties">
			<value>clientEncoding=UTF-8</value>
		</property>
	</bean>

	<!-- myBatis文件 -->
	<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
		<propertyname="dataSource"ref="dataSource" />
		<!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
		<propertyname="mapperLocations"value="classpath:yzyq/mapping/*.xml" />
	</bean>

	<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<propertyname="basePackage"value="yzyq.mapper" />
		<propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory" />
	</bean>

	<!-- 配置事务管理器 -->
	<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<propertyname="dataSource"ref="dataSource" />
	</bean>

	<!-- 注解方式配置事物 -->
	<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->

	<!-- 拦截器方式配置事物 -->
	<tx:adviceid="transactionAdvice"transaction-manager="transactionManager">
		<tx:attributes>
			<tx:methodname="add*"propagation="REQUIRED" />
			<tx:methodname="append*"propagation="REQUIRED" />
			<tx:methodname="insert*"propagation="REQUIRED" />
			<tx:methodname="save*"propagation="REQUIRED" />
			<tx:methodname="update*"propagation="REQUIRED" />
			<tx:methodname="modify*"propagation="REQUIRED" />
			<tx:methodname="edit*"propagation="REQUIRED" />
			<tx:methodname="delete*"propagation="REQUIRED" />
			<tx:methodname="remove*"propagation="REQUIRED" />
			<tx:methodname="repair"propagation="REQUIRED" />
			<tx:methodname="delAndRepair"propagation="REQUIRED" />

			<tx:methodname="get*"propagation="SUPPORTS" />
			<tx:methodname="find*"propagation="SUPPORTS" />
			<tx:methodname="load*"propagation="SUPPORTS" />
			<tx:methodname="search*"propagation="SUPPORTS" />
			<tx:methodname="datagrid*"propagation="SUPPORTS" />

			<tx:methodname="*"propagation="SUPPORTS" />
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcutid="transactionPointcut"expression="execution(* yzyq.service..*Impl.*(..))" />
		<aop:advisorpointcut-ref="transactionPointcut"advice-ref="transactionAdvice" />
	</aop:config>


	<!-- 配置druid监控spring jdbc -->
	<beanid="druid-stat-interceptor"class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
	</bean>
	<beanid="druid-stat-pointcut"class="org.springframework.aop.support.JdkRegexpMethodPointcut"scope="prototype">
		<propertyname="patterns">
			<list>
				<value>yzyq.service.*</value>
			</list>
		</property>
	</bean>
	<aop:config>
		<aop:advisoradvice-ref="druid-stat-interceptor"pointcut-ref="druid-stat-pointcut" />
	</aop:config>

</beans>

spring.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" 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
">

	<!-- 引入属性文件 -->
	<context:property-placeholder location="classpath:config.properties" />

	<!-- 自动扫描(自动注入) -->
	<context:component-scan base-package="yzyq.service" />


</beans>






以上是关于Spring注解问题,[action中注入service失败的主要内容,如果未能解决你的问题,请参考以下文章

求springmvc 注入多个bean的方法

Spring 常用的注解及“依赖注入”的实现

Spring中@相关注解的意义

spring in action 学习笔记八:用@Primary 或者@Qualifier消除@Autowired引起的歧义现象

spring 3.2 注解注入普通BEAN的@Resource 总是得到空指针,怎么解决?

如何调用spring配置文件手动注入的bean