目测是这行new了之后没有关闭导致的:
protected Dao nutDao = new ClassPathXmlApplicationContext("applicationContext.xml").getBean(Dao.class);
请问该怎么解决。
还有,用Spring注入有没有别的方式
目测是这行new了之后没有关闭导致的:
protected Dao nutDao = new ClassPathXmlApplicationContext("applicationContext.xml").getBean(Dao.class);
请问该怎么解决。
还有,用Spring注入有没有别的方式
试过了,会报错
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: {}
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???
试着在其他类里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)