自定义标签如何进行spring的@Autowired

catch不住的爱 2013-11-07 10:43:38
自定义了一个tag,里面需要引用到service层,利用@Autowired注解,永远得不到service对象,这是为什么呢
public class CataTag extends BodyTagSupport {
/**
*
*/
private static final long serialVersionUID = 1L;

@Autowired
public HSListViewComponent sysCataViewComponent;
...全文
513 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_24195447 2014-12-03
  • 打赏
  • 举报
回复
这个问题解决了吗?
超级菜鸟 2013-11-11
  • 打赏
  • 举报
回复
换一个能用的service注入试试看,如果可以用的话就说明你的这个service,spring没有初始化
linapex888 2013-11-10
  • 打赏
  • 举报
回复
因为你的注解内没有被spring管理,所以获取不到。 你可以通过 Spring的 beanFactory 获取对象,完成注入。
catch不住的爱 2013-11-09
  • 打赏
  • 举报
回复
引用 1 楼 defonds 的回复:
spring 配置文件 applicationContext.xml 里加入
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>  
了没,Spring 通过一个 BeanPostProcessor 对 @Autowired 进行解析,所以要让 @Autowired 起作用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。
我加了还是没有用,下面是我的配置文件
<!-- 
	①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 
	-->
	<context:annotation-config/>
	<context:component-scan base-package="com.honestspring.controller"></context:component-scan>
	<context:component-scan base-package="com.honestspring.dao"></context:component-scan>
	<context:component-scan base-package="com.honestspring.service"></context:component-scan>
	<context:component-scan base-package="com.honestspring.sys.component"></context:component-scan>
	<context:component-scan base-package="com.honestspring.sys.tld"></context:component-scan>
	<context:component-scan base-package="com.honestspring.model"></context:component-scan>
	<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

	<!-- 
	②:启动Spring MVC的注解功能,完成请求和注解POJO的映射,添加拦截器,类级别的处理器映射 
	-->
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <list>
                <bean class="com.honestspring.util.HandlerInterceptor"/>
            </list>
        </property>
	</bean>
	
	<!-- 
	②:启动Spring MVC的注解功能,完成请求和注解POJO的映射,
	配置一个基于注解的定制的WebBindingInitializer,解决日期转换问题,方法级别的处理器映射
	-->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
	    <property name="cacheSeconds" value="0" />
	    <property name="webBindingInitializer">
	        <bean class="com.honestspring.util.WebBinding" />
	    </property>
		 <!-- 配置一下对json数据的转换 -->
 	<property name="messageConverters">
	    	<list>
	    		<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
	    	</list>
	    </property>
	</bean>
catch不住的爱 2013-11-09
  • 打赏
  • 举报
回复
不要沉啊,大家帮我顶起啊,不然我晚上睡不着了
小菜_ 2013-11-08
  • 打赏
  • 举报
回复
写了你不配置没用啊
sunbo624 2013-11-08
  • 打赏
  • 举报
回复
因为它没被Spring托管 所以写注解白写
Defonds 2013-11-08
  • 打赏
  • 举报
回复
spring 配置文件 applicationContext.xml 里加入
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>  
了没,Spring 通过一个 BeanPostProcessor 对 @Autowired 进行解析,所以要让 @Autowired 起作用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。

81,094

社区成员

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

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