我像这样编写一个spring xml架构
<route:urls id="urlHandlerMap">
<route:url pattern="user/api/**" beforeHandlers="defaultBeforeHandler"/>
</route:urls>
这是我的架构解析器
public class ApiRouteParser extends AbstractSingleBeanDefinitionParser {
private static String DEFAULT_BEFORE_HANDLER = "defaultBeforeHandler";
private static String ROUTE_URL_FIELD = "route:url";
private static String PATTERN_FIELD = "pattern";
private static String BEFORE_HANDLER_FIELD = "beforeHandlers";
@Override
protected Class getBeanClass(Element element) {
return UrlHandlerMap.class;
}
@Override
protected void doParse(Element element,ParserContext parserContext, BeanDefinitionBuilder builder) {
NodeList urlList = element.getElementsByTagName(ROUTE_URL_FIELD);
ManagedMap<String, BeanMetadataElement> urlHandlerMapperDefinition = new ManagedMap<String, BeanMetadataElement>();
for (int i=0; i < urlList.getLength(); i++){
Element urlNode = (Element)urlList.item(i);
String pattern = urlNode.getAttribute(PATTERN_FIELD);
String beforeHandlers = urlNode.getAttribute(BEFORE_HANDLER_FIELD);
if (StringUtils.isEmpty(beforeHandlers)){
beforeHandlers = DEFAULT_BEFORE_HANDLER;
}
BeanDefinitionBuilder urlHandlerBuilder = BeanDefinitionBuilder.genericBeanDefinition(UrlHandler.class);
urlHandlerBuilder.addPropertyValue("beforeHandlerList", parseList(beforeHandlers));
urlHandlerMapperDefinition.put(pattern, urlHandlerBuilder.getBeanDefinition());
}
builder.addPropertyValue("urlHandlerMapper", urlHandlerMapperDefinition);
}
private List<BeanMetadataElement> parseList(String handlers){
List<BeanMetadataElement> definitionList = new ManagedList<BeanMetadataElement>();
String[] handlerArray = handlers.split(",");
for (String handler : handlerArray){
// this handler is inject with @Component
definitionList.add(new RuntimeBeanReference(handler));
}
return definitionList;
}
}
但是当我跑它时,抛出:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'defaultBeforeHandler' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:680)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1183)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:275)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
这是defaultBeforeHandler
@Component
public class DefaultBeforeHandler extends BaseController implements BeforeHandler {
private static Logger logger = LoggerFactory.getLogger(DefaultBeforeHandler.class);
@Override
public void execute(RouteRequest routeRequest, HttpServletRequest request) throws Exception{
logger.debug("defaultBeforeHandler");
}
}
但是当我在xml中定义defaultBeforeHandler时,它会没问题。 如何在我的xml解析器中使用@Component注释?
我已在<context:component-scan base-package="com.my.app" />
applicationContext.xml
春季版:4.3
答案 0 :(得分:0)
您需要通过配置启用组件扫描。通过注释或XML。 E.g。
org.hibernate.envers packages
该示例将启用注释配置并扫描com.acme.app.services下的包以获取任何带注释的类,并将它们注册为bean。更新该字符串以使包与其中带注释的类匹配。
有关详细信息,请参阅docs。