想买个自动删除的行车记录仪多久自动删除那里有的卖?

spring注解 构造函数问题 - ITeye问答
我碰到个问题,我项目用的是spring框架,用注解做的
在初始化时候出错了。报的错误是managerContext为空指针,但是把构造函数去掉就没问题,到底怎么回事呀
JAVA类
public class TcpMessageClient implements MessageClient{
@Autowired
private ManagerContext&&& managerC
@Autowired
private ISocketLog&& iSocketL
public& TcpMessageClient(){
socket=new SocketClient(managerContext.getRouterIP(),managerContext.getRouterPort(),managerContext.getTimeout(),iSocketLog);
socket.setProtocol(SocketClient.PROTOCOL_TCP);
}
}
错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageService': Injection of resource nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageClient' defined in file [D:\tomcat\apache-tomcat-6.0.18\webapps\roamer\WEB-INF\classes\com\ultrapower\roamer\common\message\TcpMessageClient.class]: Instanti nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.mon.message.TcpMessageClient]: Constru nested exception is java.lang.NullPointerException
问题补充:public interface ResourceService extends BaseService{
* &p&根据url获取resource&p/&
* @param url
public Resource getResourceByUrl(String url);
}
@Component("resourceService")
public class ResourceServiceImpl extends BaseServiceImpl implements ResourceService{
* @see com.ultrapower.roamer.customer.user.service.ResourceService#getResourceByUrl(java.lang.String)
@SuppressWarnings("unchecked")
public Resource getResourceByUrl(String url) {
List&Resource& resources = createCriteria(Resource.class)
.add(Restrictions.eq("url", url))
if(resources == null || resources.size() == 0){
return resources.get(0);
@Component("tested")
public class TestClass {
private ResourceService resourceS
public ResourceService getResourceService() {
return resourceS
public void setResourceService(ResourceService resourceService) {
this.resourceService = resourceS
}
public class TaskAction&& extends BaseAction{
private static final long serialVersionUID = 1L;
private& TaskService taskS
private TestC
public void query(){
tested.getResourceService();
}
}
在TaskAction中可以看到tested被注入进去了,但是tested中的resourceService是null。测试好多次都是这样
问题补充:&beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"&
&context:property-placeholder location="classpath:roamer.properties" /&
&context:annotation-config /&
&context:component-scan base-package="com.ultrapower.roamer" /&
&&& &!-- 国际化 --&
&&& &bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&
&&&
&property name="basename" value="classpath:messages_zh_CN" /&
&&& &/bean&
&&& &!-- import --&
&import resource="application-datasource.xml"/&
&import resource="application-security.xml"/&
&import resource="application-debug.xml"/&
&/beans&
因为类首先被Spring实例化的时候,会调用构造函数。只有实例化后,才会注入。你等于没注入就调用了,所以报错。
spring 在初始化bean的时候要先调用构造函数,再set其它属性;
也就是说在TcpMessageClient实例化的时候(调用public& TcpMessageClient()时),managerContext属性还是空的。
已解决问题
未解决问题温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
看例子:public class AnimalServiceImpl implements AnimalService { @Autowired private PersonD public AnimalServiceImpl(){} public AnimalServiceImpl(PersonDao per){
this.per = } public void jump(){
per.add(); }}&&bean id="personDaoB" class="com.dao.person.PersonDaoImpl" &&/bean&& &bean id='animalService' class="com.service.person.AnimalServiceImpl" &AnimalService animal = (AnimalService)cxt.getBean("animalService");animal.jump();那么,能不能让@Autowired以名称查找呢?@Autowired @Qualifier("personDaoB") private PersonD这样就可以了!@Autowired(required=false)当根据类型去找,找不到时,注入一个空。@Autowired(required=true)当根据类型去找,找不到时,抛出异常信息。例如&bean id="personDaoB" class="com.dao.person.PersonDaoImpl" &&/bean&&&bean id='animalService' class="com.service.person.AnimalServiceImpl" &&&/bean&@Autowired(required=false) private PersonSpublic AnimalServiceImpl(PersonDao per,PersonService ps){
this.per =
this.ps = }测试AnimalService animal = (AnimalService)cxt.getBean("animalService");animal.jump();一切正常!如果改为@Autowired(required=true) private PersonS由于找不到PersonService&类型对应的bean,而required=true,所以抛出异常信息!注意,注解注入时一定要同时写带参和不带参的构造方法!如果你不用构造方法行不行呢?就像第一种方式一样:@Autowired private PersonD public void setPd(PersonDao pd) {
this.pd = } public void save(String str){
pd.add(str); }&&context:annotation-config/&&&bean id="personDao" class="com.dao.spring.PersonDaoImpl"&&/bean&&&&bean id='personService' class="com.service.spring.PersonServiceImpl"&同样是可以的!与第一种方式的区别在于,在bean里面,除了配置了两个bean以外,什么也没配,而是在声明属性的时候,写@Autowired之类的注解。而第一种是,在bean的实现类里面,基本什么也没配置,而是在配置文件中写了&property name="personDaoA" ref="personSetDao"&&/property&
阅读(15885)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_',
blogTitle:'spring学习笔记13(注解@Autowired方式注入)',
blogAbstract:'@Autowired与@Resource用法基本相同,区别在于@Autowired是默认以类型去查找,而@Resource默认以字段名去查找。@Autowired是spring提供的,而@Resource是j2ee提供的。看例子:public class AnimalServiceImpl implements AnimalService {\t',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:6,
publishTime:3,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'0',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}@Autowired无法自动注入 - ITeye问答
框架用的是springside4,基于springmvc的,我在我的service的实现类和DAO里面都加了@Component注解,在单元测试的时候可以自动注入,跑项目的时候就报空指针异常了。有没有遇到同样问题的朋友啊
问题补充:问题解决了,因为我引用自动注入的类本身不是spring管理的,给这个类加上@component注解其他spring组件就可以自动注入了
是不是没有扫描到相应的包啊。
如果是必须的对象,试试@Autowired进构造函数,个人感觉@Autowired进构造函数,比@Autowired变量声明要靠谱。
已解决问题
未解决问题

我要回帖

更多关于 行车记录仪多久自动删除 的文章

 

随机推荐