NutzCN Logo
问答 权限管理 报错org.nutz.ioc.IocException Object 'authorityService' without define
发布于 2922天前 作者 qq_341d4eeb 3092 次浏览 复制 上一个帖子 下一个帖子
标签:

MainSetup中触发AuthorityService,在init方法末尾添加如下代码

AuthorityService as = ioc.get(AuthorityService.class);
as.initFormPackage("net.wendal.nutzbook");
as.checkBasicRoles(dao.fetch(User.class, "admin"));

启动项目报错

严重: Exception starting filter nutz
org.nutz.mvc.LoadingException: org.nutz.ioc.IocException: [authorityService] # For object [authorityService] - type:[interface net.wendal.nutzbook.service.AuthorityService]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at org.nutz.lang.born.ConstructorBorning.born(ConstructorBorning.java:17)
	at org.nutz.lang.born.BornContext.doBorn(BornContext.java:60)
	at org.nutz.lang.Mirror.born(Mirror.java:990)
	at org.nutz.lang.Lang.wrapThrow(Lang.java:198)
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:135)
	at org.nutz.mvc.ActionHandler.<init>(ActionHandler.java:19)
	at org.nutz.mvc.NutFilter._init(NutFilter.java:87)
	at org.nutz.mvc.NutFilter.init(NutFilter.java:65)
	at net.wendal.nutzbook.mvc.NutzBookNutFilter.init(NutzBookNutFilter.java:22)
	at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
	at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
	at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105)
	at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4700)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5340)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
	at java.util.concurrent.FutureTask.run(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Caused by: org.nutz.ioc.IocException: [authorityService] # For object [authorityService] - type:[interface net.wendal.nutzbook.service.AuthorityService]
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:218)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:146)
	at net.wendal.nutzbook.MainSetup.init(MainSetup.java:38)
	at org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:277)
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:121)
	... 16 more
Caused by: org.nutz.ioc.ObjectLoadException: Object 'authorityService' without define!
	at org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:174)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:180)
	... 21 more

不知道是什么问题?

6 回复

AuthorityService

package net.wendal.nutzbook.service;

import org.nutz.ioc.loader.annotation.IocBean;

import net.wendal.nutzbook.bean.Role;
import net.wendal.nutzbook.bean.User;

@IocBean
public interface AuthorityService {

	/**
     * 扫描RequiresPermissions和RequiresRoles注解
     * @param pkg 需要扫描的package
     */
    void initFormPackage(String pkg);

    /**
     * 检查最基础的权限,确保admin用户-admin角色-(用户增删改查-权限增删改查)这一基础权限设置
     * @param admin
     */
    void checkBasicRoles(User admin);

    /**
     * 添加一个权限
     */
    public void addPermission(String permission);

    /**
     * 添加一个角色
     */
    public Role addRole(String role);
}

实现类

package net.wendal.nutzbook.service;

import java.util.List;

import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.entity.Record;
import org.nutz.ioc.loader.annotation.Inject;

import net.wendal.nutzbook.bean.Permission;
import net.wendal.nutzbook.bean.Role;
import net.wendal.nutzbook.bean.User;


public class AuthorityServiceImpl implements AuthorityService{

	@Inject protected Dao dao;
	
	
	@Override
	public void initFormPackage(String pkg) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void checkBasicRoles(User admin) {

		// 检查一下admin的权限
        Role adminRole = dao.fetch(Role.class, "admin");
        if (adminRole == null) {
            adminRole = addRole("admin");
        }
        // admin账号必须存在与admin组
        if (0 == dao.count("t_user_role", Cnd.where("u_id", "=", admin.getId()).and("role_id", "=", adminRole.getId()))) {
            dao.insert("t_user_role", Chain.make("u_id", admin.getId()).add("role_id", adminRole.getId()));
        }
        // admin组必须有authority:* 也就是权限管理相关的权限
        List<Record> res = dao.query("t_role_permission", Cnd.where("role_id", "=", adminRole.getId()));
        OUT: for (Permission permission : dao.query(Permission.class, Cnd.where("name", "like", "authority:%").or("name", "like", "user:%"), null)) {
            for (Record re : res) {
                if (re.getInt("permission_id") == permission.getId())
                    continue OUT;
            }
            dao.insert("t_role_permission", Chain.make("role_id", adminRole.getId()).add("permission_id", permission.getId()));
        };
		
	}

	@Override
	public void addPermission(String permission) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Role addRole(String role) {
		// TODO Auto-generated method stub
		return null;
	}

}

检查一下MainModule的iocby

@wendal 我也遇到了同样的问题 请问下MainModule头部

@SetupBy(value=MainSetup.class)
//请注意星号!!不要拷贝少了
@IocBy(type=ComboIocProvider.class, args={"*js",
											"ioc/", //这个package下所有带@IocBean注解的类,都会登记上
                                        	"*anno", Code.PACKAGE_NAME,
                                        	"*tx", // 事务拦截 aop
//                                        "*async"}) // 异步执行aop
										"*org.nutz.integration.quartz.QuartzIocLoader"})
@Ok("json:full")
@Fail("jsp:jsp.500")
@Localization(value="msg/", defaultLocalizationKey="zh-CN")
@ChainBy(args= "mvc/auth-mvc-chain.js")
@SessionBy(ShiroSessionProvider.class)
@Modules(scanPackage=true)

是没啥问题么 要注意什么呢?

authorityService在Code.PACKAGE_NAME之下吗?

来自炫酷的 NutzCN

在的 找到问题了 AuthorityService的实现类AuthorityServiceImpl的@IocBean(name = "authorityService")没加上... 加上就好了

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