NutzCN Logo
问答 java中无法注入配置文件的信息
发布于 2510天前 作者 zp8821138 1692 次浏览 复制 上一个帖子 下一个帖子
标签:

是beetl中自定义标签中无法注入properties中的信息,写法如下

@Inject("java:$conf.get('system.http.protocol'}")
public String httpProtocol 

就是因为beetl的自定义标签是不受nutz ioc容器管理的所以注入也没办法进行吗?这个不想写死,有啥好的解决方法吗

8 回复

在分享区找

是这个吧

@IocBean
public class SysDicSelectOptionTag extends GeneralVarTagBinding {
    @Inject
    private SysDictService sysDictService;

    @Override
    public void render() {
        String dicCode = Strings.sNull(this.getAttributeValue("code"));
        Sys_dict dict;
        if (Strings.isNotBlank(dicCode)) {
            dict = sysDictService.fetch(Cnd.where("code", "=", dicCode));
        }else{
            return;
        }

        String parentId= dict.getId();

        List<Sys_dict> lst= sysDictService.query(Cnd.where("parentId","=",parentId).asc("location"));

        for (Sys_dict channel : lst) {
            this.binds(channel);
            this.doBodyRender();
        }
    }
}
加入tag

<#dic_bycode_list code="workflow" var="o2">
${o2.name!}
</#dic_bycode_list>

别忘了注册在setup.java
for (ViewMaker vm : config.getViewMakers()) {
if (vm instanceof BeetlViewMaker) {
((BeetlViewMaker)vm).groupTemplate.registerTagFactory("dic_bycode_list", ()->ioc.get(SysDicSelectOptionTag.class));
}
}

我试了 service可以注入,但是读取配置文件的没办法注入 报错了,错误如下

org.nutz.ioc.IocException: IocBean[categoryListTag] throw Exception when creating
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:152)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:240)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:270)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:160)
at com.kanq.common.core.Setup.lambda$2(Setup.java:639)
at org.beetl.ext.tag.HTMLTagVarBindingWrapper.init(HTMLTagVarBindingWrapper.java:88)
at org.beetl.core.statement.TagStatement.execute(TagStatement.java:86)
at org.beetl.core.statement.Program.execute(Program.java:70)
at org.beetl.core.Template.renderTo(Template.java:137)
at org.beetl.core.Template.renderTo(Template.java:103)
at org.beetl.ext.web.WebRender.render(WebRender.java:120)
at org.beetl.ext.nutz.BeetlView.render(BeetlView.java:28)
at org.nutz.mvc.view.ViewZone.render(ViewZone.java:51)
at org.nutz.mvc.impl.processor.ViewProcessor.process(ViewProcessor.java:60)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:33)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:30)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at com.kanq.processor.XssSqlFilterProcessor.process(XssSqlFilterProcessor.java:33)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at com.kanq.processor.NutShiroProcessor.process(NutShiroProcessor.java:47)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at com.kanq.processor.LogTimeProcessor.process(LogTimeProcessor.java:17)
at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2466)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2455)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: IocBean[categoryListTag] fail at field=[httpProtocol]
at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:40)
at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:67)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:138)
... 67 more
Caused by: java.lang.NullPointerException
at org.nutz.ioc.java.FieldNode.getValue(FieldNode.java:15)
at org.nutz.ioc.java.ObjectFunctionNode.getValue(ObjectFunctionNode.java:22)
at org.nutz.ioc.java.ChainNode.eval(ChainNode.java:24)
at org.nutz.ioc.java.ChainNode.eval(ChainNode.java:27)
at org.nutz.ioc.java.ChainNode.eval(ChainNode.java:19)
at org.nutz.ioc.val.JavaValue.get(JavaValue.java:69)
at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:32)
... 69 more

```

@Inject("java:$conf.get('system.http.protocol'}") // 末尾的是括号,你写了大括号

// 应该是
@Inject("java:$conf.get('system.http.protocol')")

额。 。。二比了 。。我还找了半天。。。

()->ioc.get(DictTag.class) 这个转成JDK1.7咋写。。

匿名内部类

这不是lambda的写法吗 ,1.7用匿名内部类?

添加回复
请先登陆
回到顶部