NutzCN Logo
问答 IdNameEntityService 使用DAO为空值
发布于 2355天前 作者 qq_46ce05a8 1715 次浏览 复制 上一个帖子 下一个帖子
标签:

我根据实例建立了UserService,通过继承IdNameEntityService,想通过初始化的时候增加一个用户,但初始提示DAO为空值,而我如果把这个方法使用@at方式注解,使用网页方式访问,又一切正常,请问一下为何故,是哪里不正确吗?

public class InitSetup implements Setup {

	@Override
	public void init(NutConfig config) {
		Dao dao = config.getIoc().get(Dao.class);
		for (Class<?> klass : Scans.me().scanPackage("net.wendal.nutzbook")) {
			if(null != klass.getAnnotation(Table.class))
			{
				dao.create(klass,false);//查询表是否存在,存在就不重新建表
			}

		}
		if(dao.count(User.class)==0)
		{
	             UserService u=new UserService();
			u.addUser("sddss", "sdsdsdds");
	}
	}
public class UserService extends IdNameEntityService<User> {
	public boolean addUser(String name, String password) {
		User user = new User();
		UserProfile profile = new UserProfile();
		user.setProfile(profile);
		profile.setSex(1);
		user.setName(name);
		user.setSalt(R.UU16());
		user.setPassword(new Md5Hash(password, user.getSalt()).toHex());
		user.setCreateTime(new Date());
		user.setUpdateTime(new Date());
		dao().insert(user);
		return false;
	}
}
8-07-12 20:20:27.585 ERROR [localhost-startStop-1] Error happend during start serivce!
java.lang.NullPointerException
	at net.wendal.nutzbook.service.UserService.addUser(UserService.java:34)
	at net.wendal.nutzbook.InitSetup.init(InitSetup.java:35)
	at org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:278)
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:121)
	at org.nutz.mvc.ActionHandler.<init>(ActionHandler.java:19)
	at org.nutz.mvc.NutFilter._init(NutFilter.java:91)
	at org.nutz.mvc.NutFilter.init(NutFilter.java:69)
	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:4958)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5652)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
18-07-12 20:20:27.586 ERROR [localhost-startStop-1] try to depose ioc
15 回复

没加@IocBean(fields={"dao"})

加了的,没有截全

@IocBean(fields = "dao")
public class UserService extends IdNameEntityService<User> {

 
	public boolean addUser(String name, String password) {
		User user = new User();
		UserProfile profile = new UserProfile();
		user.setProfile(profile);
		profile.setSex(1);
		user.setName(name);
		user.setSalt(R.UU16());
		user.setPassword(new Md5Hash(password, user.getSalt()).toHex());
		user.setCreateTime(new Date());
		user.setUpdateTime(new Date());
		dao().insert(user);
		return false;
	}

如果通过网页方式是没有错的,但如果通过初始化方式就不行,使用测试方式也不行!

UserService u=new UserService();

直接new? 必死无疑

应该如何使用,请指导一下,感谢

刚开始学习,还有很多的点未掌握!

我也使用过IOC.GET的方法去建立,也不行

UserService u=ioc.get(UserService.class)

ioc.get是对的, new是错的

但是依然报错!

public class InitSetup implements Setup {
	private Ioc ioc;


	@Override
	public void init(NutConfig config) {
		ioc = null;
		Dao dao = config.getIoc().get(Dao.class);
		for (Class<?> klass : Scans.me().scanPackage("net.wendal.nutzbook")) {
			if(null != klass.getAnnotation(Table.class))
			{
				dao.create(klass,false);//查询表是否存在,存在就不重新建表
			}
			

			UserService userService = ioc.get(UserService.class);
		
			userService.addUser("sddssd", "ssddsds");

		}
	}


	@Override
	public void destroy(NutConfig nc) {
		// TODO Auto-generated method stub

	}

}
8-07-12 21:12:17.108 DEBUG [localhost-startStop-1] select expert : org.nutz.dao.impl.jdbc.sqlserver2005.Sqlserver2005JdbcExpert
18-07-12 21:12:17.118 DEBUG [localhost-startStop-1] JDBC Driver --> 4.0.4621.201
18-07-12 21:12:17.119 DEBUG [localhost-startStop-1] JDBC Name   --> Microsoft JDBC Driver 4.0 for SQL Server
18-07-12 21:12:17.119 DEBUG [localhost-startStop-1] JDBC URL    --> jdbc:sqlserver://127.0.0.1:1433;authenticationScheme=nativeAuthentication;xopenStates=false;sendTimeAsDatetime=true;trustServerCertificate=false;sendStringParametersAsUnicode=true;selectMethod=direct;responseBuffering=adaptive;packetSize=8000;multiSubnetFailover=false;loginTimeout=15;lockTimeout=-1;lastUpdateCount=true;encrypt=false;disableStatementPooling=true;databaseName=Nutz;applicationName=Microsoft JDBC Driver for SQL Server;applicationIntent=readwrite;
18-07-12 21:12:17.119 DEBUG [localhost-startStop-1] Database info --> SQLSERVER:[Microsoft SQL Server - 10.50.1600]
18-07-12 21:12:17.126 DEBUG [localhost-startStop-1] Found 10 resource by src( net/wendal/nutzbook/ ) , regex( ^.+[.]class$ )
18-07-12 21:12:17.127 ERROR [localhost-startStop-1] Error happend during start serivce!
java.lang.NullPointerException
	at net.wendal.nutzbook.InitSetup.init(InitSetup.java:30)
	at org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:278)
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:121)
	at org.nutz.mvc.ActionHandler.<init>(ActionHandler.java:19)
	at org.nutz.mvc.NutFilter._init(NutFilter.java:91)
	at org.nutz.mvc.NutFilter.init(NutFilter.java:69)
	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:4958)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5652)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
18-07-12 21:12:17.127 ERROR [localhost-startStop-1] try to depose ioc
18-07-12 21:12:17.128 INFO [localhost-startStop-1] org.nutz.ioc.impl.NutIoc@1256584083 is closing. startup date [18-07-12 21:12:16.070]
18-07-12 21:12:17.128 DEBUG [localhost-startStop-1] Depose object 'dao' ...
18-07-12 21:12:17.128 DEBUG [localhost-startStop-1] Depose object 'dataSource' ...
18-07-12 21:12:17.129 INFO [localhost-startStop-1] org.nutz.ioc.impl.NutIoc@1256584083 is deposed. startup date [18-07-12 21:12:16.070]
七月 12, 2018 9:12:17 下午 org.apache.catalina.core.StandardContext filterStart
严重: Exception starting filter nutz
org.nutz.mvc.LoadingException: java.lang.NullPointerException
	at org.nutz.mvc.LoadingException$FC$7fbf041f2080c9b836f5c41db56b936b.invoke(LoadingException.java)
	at org.nutz.lang.born.AbstractConstructorBorning.call(AbstractConstructorBorning.java:25)
	at org.nutz.lang.born.ConstructorBorning.born(ConstructorBorning.java:19)
	at org.nutz.lang.born.BornContext.doBorn(BornContext.java:60)
	at org.nutz.lang.Mirror.born(Mirror.java:1022)
	at org.nutz.lang.Lang.wrapThrow(Lang.java:203)
	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:91)
	at org.nutz.mvc.NutFilter.init(NutFilter.java:69)
	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:4958)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5652)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
	at net.wendal.nutzbook.InitSetup.init(InitSetup.java:30)
	at org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:278)
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:121)
	... 15 more

private Ioc ioc;

这样拿不到ioc容器的, config.getIoc() 才对

好的,谢谢,这上是正确的,已经可以了,请问一下,这个是怎么一个原理呢?还没有太搞得清楚!为什么不能够用NEW ,而是要使用这种方法呢?如果其他地方要引用,而又没有传入config,我又怎么样去建立呢?

因为有@Inject和@IocBean, 你应该去了解一下ioc的原理

好的,我再去研究一下,其他获是不是使用Mvcs.getIoc();

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