NutzCN Logo
问答 和Spring整合后内存溢出
发布于 2845天前 作者 qq_4cb3f8b1 1967 次浏览 复制 上一个帖子 下一个帖子
标签:

目测是这行new了之后没有关闭导致的:

protected Dao nutDao = new ClassPathXmlApplicationContext("applicationContext.xml").getBean(Dao.class);

请问该怎么解决。

还有,用Spring注入有没有别的方式

17 回复
@AutoWired
protected Dao dao;

public void setDao(Dao dao) {
    this.dao = dao;
}

试过了,会报错

No qualifying bean of type [org.nutz.dao.Dao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

spring没加载那个xml?

用的是spring boot?所以没有xml?

@Resource(type = Dao.class)
    public void init(Dao dao) {
        this.dao = dao;
    }

还真是这样。。启动时现在不会报错了,但是调用时还是空值。。

调用Nutdao是空值,而且在一个普通类里能调到,在module里就是空值

@qq_4cb3f8b1 上module的代码

package com.nutz_spring.module;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import com.nutz_spring.model.Pet;
import com.nutz_spring.service.PetService;

@Controller
@At("pet")
// @Filters(@By(type = CheckSession.class, args = { "me", "/" }))
public class PetModule extends BaseModule {

	
	private PetService petService; 
	
	@Autowired
	@Qualifier("petService")
	public void setPetService(PetService petService) {
		this.petService = petService;
	}
	
	private Log log = Logs.get();

	@At
	@Ok("jsp:jsp.petlist")
	public Map<String, Object> show() {
		log.info(nutDao);
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			log.info(this.petService);
			List<com.nutz_spring.model.Pet> list = this.petService.query();
			map.put("petlist", list);

		} catch (Exception e) {
			e.printStackTrace();
			map.put("errMsg", "fail to query");
		}
		return map;
	}

	protected String checkPet(Pet pet, boolean create) {
		if (pet == null) {
			return "空对象";
		}
		if (create) {
			if (Strings.isBlank(pet.getName()))
				return "名称不能为空";
		}
		if (create) {
			int count = nutDao.count(Pet.class, Cnd.where("name", "=", pet.getName()));
			if (count != 0) {
				return "名称已经存在";
			}
		} else {
			if (pet.getId() < 1) {
				return "用户Id非法";
			}
		}
		if (pet.getName() != null)
			pet.setName(pet.getName().trim());
		return null;
	}

	@At
	public Object add(@Param("..") Pet pet) {
		NutMap re = new NutMap();
		String msg = checkPet(pet, true);
		if (msg != null) {
			return re.setv("ok", false).setv("msg", msg);
		}
		pet.setCreateDate(new Date());
		pet.setUpdateDate(new Date());
		pet = nutDao.insert(pet);

		return re.setv("ok", true).setv("data", pet);
	}
}

nutDao是BaseModule 里面属性?? 所以我说用@Autowired是没错的呢

问题是把nutDao放入spring的上下文,通过配置文件/其他方式.

用的是不是spring boot? 不然怎么没读取applicationContext.xml里面的bean定义呢? applicationContext.xml里面只写了nutDao???

用的就是spring,配置文件也能加载到了。
问题是在别的类里能成功注入nutDao,只有在这个module会报空指针。是不是父类里的属性注入不了??

应该不是父类的原因,因为petService也是null,所以这个类有什么错误吗。。。

spring mvc与spring ioc之间的问题?

试着在其他类里new一个petModule对象再去调nutDao,依旧空指针

严重: Context initialization failed
java.lang.NullPointerException
	at com.nutz_spring.module.PetModule.count(PetModule.java:70)
	at com.nutz_spring.base.Globals.test(Globals.java:61)
	at com.nutz_spring.base.Globals.onApplicationEvent(Globals.java:45)
	at com.nutz_spring.base.Globals.onApplicationEvent(Globals.java:1)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:151)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:128)
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:331)
	at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:773)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:483)
	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4717)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5179)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
	at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:596)
	at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1805)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	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)

二月 07, 2017 5:02:41 下午 org.apache.catalina.core.StandardContext listenerStart
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.NullPointerException
	at com.nutz_spring.module.PetModule.count(PetModule.java:70)
	at com.nutz_spring.base.Globals.test(Globals.java:61)
	at com.nutz_spring.base.Globals.onApplicationEvent(Globals.java:45)
	at com.nutz_spring.base.Globals.onApplicationEvent(Globals.java:1)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:151)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:128)
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:331)
	at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:773)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:483)
	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4717)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5179)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
	at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:596)
	at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1805)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	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)

new出来肯定不行啊, 必须从ioc容器里面取出来

改过之后在测试类里可以成功了,但是从浏览器访问就空指针。看来的确是spring mvc的问题

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