NutzCN Logo
问答 NutTxDao 无法从ioc中获取
发布于 2390天前 作者 yy321973351 1521 次浏览 复制 上一个帖子 下一个帖子
标签:
package com.yx.product.factory;

import com.alibaba.druid.pool.DruidDataSource;
import org.nutz.dao.Dao;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.impl.NutTxDao;
import org.nutz.ioc.annotation.InjectName;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import javax.sql.DataSource;

/**
 * Created by Administrator on 2018/6/6.
 */
@IocBean
public class MyFactory {


    @IocBean
    public PropertiesProxy conf() {
        return new PropertiesProxy("properties/");
    }

    @IocBean
    public DataSource dataSource(@Inject PropertiesProxy conf) {
        return conf.make(DruidDataSource.class, "db.");
    }

    @IocBean
    public Dao dao(@Inject DataSource dataSource) {
        return new NutDao(dataSource);

    }

    @IocBean
    protected NutTxDao nutTxDao(@Inject  Dao dao) {
        return  new NutTxDao(dao);
    }
}

@IocBean
public class ProductServiceImpl implements ProductService {

    @Inject("refer:$ioc")
    protected Ioc ioc; // 获取ioc容器
    @Inject
    protected Dao dao;


   
    @Override
    public void updateSort(ProductBean productBean)  {
       String[] strings =ioc.getNames();
        System.out.println(Arrays.toString(strings));
        NutTxDao nutTxDao=ioc.get(NutTxDao.class);
        try {
            nutTxDao.beginRC();
            if (productBean.getProduct_sort() != null || productBean.getProduct_sort() != 0) {
                int sortold = dao.fetch(ProductBean.class, productBean.getProduct_id()).getProduct_sort();
                int sortnow = productBean.getProduct_sort();
                int max = dao.func(ProductBean.class, "max", "product_id", Cnd.where("product_sort", "between", sortold < sortnow ? new Object[]{sortold, sortnow} : new Object[]{sortnow, sortold}));
                int min = dao.func(ProductBean.class, "min", "product_id", Cnd.where("product_sort", "between", sortold < sortnow ? new Object[]{sortold, sortnow} : new Object[]{sortnow, sortold}));
                nutTxDao.update(ProductBean.class, Chain.makeSpecial("product_sort", sortnow < sortold ? "+1" : "-1"), Cnd.where("product_id", "between", new Object[]{min, max}));
            }
            if (isQuique(productBean) && isGeshi(productBean)) {
                nutTxDao.updateIgnoreNull(productBean);
            }
            // 提交事务
            nutTxDao.commit();
        } catch (Throwable e) {
            // 回滚事务
            nutTxDao.rollback();

        } finally {
            // 关闭事务
            nutTxDao.close();
        }
    }


}

[productBean, productServiceImpl, dao, conf, myFactory, dataSource, productModule] ← 所有在容器中定义了的对象名称列表
18-06-07 10:34:02.857 DEBUG [http-nio-8080-exec-10] Get 'nutTxDao'<class org.nutz.dao.impl.NutTxDao>
18-06-07 10:34:02.858 DEBUG [http-nio-8080-exec-10] 	 >> Load definition name=nutTxDao
18-06-07 10:34:02.858 WARN [http-nio-8080-exec-10] Error@/product/updatesort :
org.nutz.ioc.IocException: IocBean[nutTxDao] For object [nutTxDao] - type:[class org.nutz.dao.impl.NutTxDao]
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:249)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:270)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:160)
	at com.yx.product.service.impl.ProductServiceImpl.updateSort(ProductServiceImpl.java:161)
	at com.yx.product.mvc.ProductModule.updateSort(ProductModule.java:159)
	at com.yx.product.mvc.ProductModule$FM$updateSort$5d7429638aca0bc6bac0962cd3532e13.invoke(ProductModule.java)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:31)
	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 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.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:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	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:748)
Caused by: org.nutz.ioc.ObjectLoadException: Object 'nutTxDao' without define!
	at org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:157)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:194)
	... 39 more

1 回复

@IocBean标在方法上的时候只能是public的

另外, NutTxDao应该随用随建, 它不是线程安全的.

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