Spring将子类自动注入父类失败
@Controller
public class LoginController{

    @Resource(name="loginService")
    private IService loginService;

    @RequestMapping("/login")
    @Autowired
    public ModelAndView index(BaseClassDo logindo){
        LoginDo ll= (LoginDo) logindo;
        ModelAndView mv = new ModelAndView();
        System.out.println(logindo);
//      mv = loginService.doService(logindo);
        mv.setViewName("login");
        return mv;
    }
}

上面index方法中,初始化的时候使用断点显示logindo是自动注入成功的,显示的是子类的字段,但是之后从页面jsp跳转过来后,注入失败,显示的只有父类的两个字段,而且强转失败java.lang.ClassCastException,难道从前台传到后天还有什么讲究吗
使用@autowrie或@Resource都试过,没有用
下面是实体表

父类

public class BaseClassDo {
    /**
     * 业务类型
     */
    private String ywtype;
    /**
     * 业务状态
     */
    private String ywstate;

    getset方法略

子类


@Component("loginDo")
public class LoginDo extends BaseClassDo {
    private String job_id;
    private int worker_id;
    private String login_pw;
    private String worker_name;

    getset方法略

5个回答

@Controller
public class LoginController{

    @Resource(name="loginService")
    private IService loginService;

    @RequestMapping("/login")
    @Autowired
    public ModelAndView index(BaseClassDo logindo){
        LoginDo ll= (LoginDo) logindo;
        ModelAndView mv = new ModelAndView();
        System.out.println(logindo);
//      mv = loginService.doService(logindo);
        mv.setViewName("login");
        return mv;
    }
}

楼上说的对,你这方法上的@Autowired是想要做什么?和@RequestMapping一起用?
如果想通过方法注入的话,那新创建一个方法然后用上@Autowired注入就行了,至于@RequestMapping是用于处理请求的

qq_36951116
祁东握力 回复liruo233: 指的是根据请求的参数绑定为相应的实体类型吗?只不过这些实体类型都继承自BaseClassDo 是吗?比如前端传job_id、worker_id、login_pw、worker_name这四个参数,这个时候在index方法的参数实际类型就是LoginDo了?然后如果前端传其他参数,那就会绑定到另一个BaseClassDo的具有这些参数对应字段的子类,是这个意思不?
6 个月之前 回复
liruo233
liruo233 我本来是想定义一个拥有如上的index方法的接口或父类,让所有controller类都继承它,这样的话方法里传递的参数就得统一了,我原本想参数的类型定义为Object或父类,然后在通过注释自动注入为对应的子类,你觉得有办法吗
6 个月之前 回复

通过页面请求发现代码真正逻辑是页面请求了一个子类Action的方法,而这个方法里面调用了父类的一个方法,此时父类里面的Service一直无法注入,对于上面所提的这种需求,实际上是需要在子类做Spring注入的同时也进行父类的Spring注入,那么这种需要这样的配置:

<bean id="barePhoneAction" class="com.eshore.crm.contrl.termmgr.mgr.action.BarePhoneAction" scope="prototype" parent="termCommonAction">
        <property name="orderVerifyApiFacade" ref="ord.bizprov.orderVerifyApiFacade"/>
        <property name="orderListQryApiFacade" ref="ord.query.orderListQryApiFacade"/>
        <property name="channelQryApiFacade" ref="cfguse.channel.channelQryApiFacade" />
</bean>

经过上面的设置以后,请求子类的Action方法,子类方法中调用父类方法时,就不会出现父类不发生注入的问题了。

一般来说,实体类是不用交由spring管理的,所以LoginDo类上不用加@Component注解

然后,在Controller层接受参数(指的是前端页面传过来的值),不用@Autowaire注解注入
接受的时候用子类类型接受,因为springmvc在传参的时候会根据你声明的参数类型去实例化对象然后将前端传过来的参数赋给该对象,
因此你声明的父类类型参数没有接受倒子类的属性值

报异常的原因是 实例化了父类对象,转为子类类型肯定要报错的,可以类型强转的原因是必须要有属于关系,才能转成功,父类对象肯定不是子类类型的呀

至于你说的初始化的时候显示的子类属性,我不太明白你说的什么意思,初始化的时候是没有值传过的,怎么确定是子类的字段

@Autowired的使用上有问题。
一般情况下,@Autowired标注在方法上的时候,会在容器初始化时去调用方法“进行赋值”,但也仅此而已了,之后的操作和@Autowired就没关系了

正常的前后端请求传递的参数是由前端传入的,并不是容器注入的

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
spring自动注入失败
启动没有报错,但是注入失败xxService的值为null。应该是哪里的问题搞了一下午都没有解决。。。
spring 子类覆盖父类中注入的bean
我们在设计程序框架的时候,会设计一个抽象基类,子类继承这个基类,共有的方法放到基类中去,使用spring后使代码变的很简单,现在遇到的问题是在基类中注入bean后,子类不可能都会是有这个bean,那么需要考虑到子类需要覆盖或者说重新注入个性化的bean 有三种方法来实现这个效果,以下是一种方法,如下面代码: 抽象基类: public abstract class AbstractNameSe
Spring中父类的依赖可自动注入到子类中吗?
比如有类A和类B, rn public class A rn String a; rn String b; rn //a和b的get,set方法 rn ..... rn rn public class B extends A rn String c; rn String fn() rn return a+b+c; rn rn rn rn rn 现在在a.xml中配置好A,并注入属性 a和b rn rn rn rn rn rn rn 1 rn rn rn 2 rn rn rn rn rn 在b.xml中配置B rn rn rn rn rn rn rn 3 rn rn rn rn rn rn 现在,Spring初始化a.xml,b.xml。获取Bean B后,调用其b.f()方法却输出“3”,可是我希望它输出“123”,因为我需要B的父类即A的依赖自动传递给B,那么我该如何配置?(a.xml和b.xml不能合并)rnrn如果用parent的话是可以解决,但我看有个项目例子不用parent,不知道是什么原因?请知道的回答一下,谢谢。
Spring 自动注入失败异常
在DAO层中使用了HibernateTemplate注入 报错信息如下 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerDao': Injection of autowired dependencies failed; nested except...
Spring 父类注入问题
rn[code=Java]rnpublic class BaseDao()rnrn private Session session;rnrn private SessionFactory sessionFactory;rnrn @Resource(name="sessionFactory")//注入失败 sessionFactory为nullrn public void setSessionFactory(SessionFactory sessionFactory) rn this.sessionFactory = sessionFactory;rn rnrn public Session getSession() rn this.session = sessionFactory.getCurrentSession();rn return this.session;rn rn protected List search(T searchPojo)rn this.getSession();//sessionFactory为null 所以这里报错.rn return null;rn rnrnrn[/code]rnrn[code=Java]rn//JUnit测试类rnpublic class BaseDaoTest extends HibBaseDao rn rn @Testrn public void getMethod()rn Goods goods = new Goods();rn goods.setGoods_name("123");rn super.search(goods);//rn rn rnrn[/code]rnrn[code=XML]rnrn rn rn rn com.onlyking.epai.pojorn rn rn rn rn org.hibernate.dialect.SQLServerDialectrn truern truern rn rn rn[/code]rnrnrn搞纠结了 继承的话 是先实例化父类 再实例化子类rnrn这个该如何注入?rnrn搞得晕乎乎的rnrn任何类继承BaseDao后,调用父类方法操作数据库 前提是自动注入SessionFactoryrnrn怎么注入进去?
Spring 子类注入
1,准备 一个父类接口,三个子类接口,一个子类的子类接口 public interface FatherInterface {} public class InterfaceOne implements FatherInterface{} public class InterfaceThree implements FatherInterface{} public class I...
Spring-父类变量注入失败
昨天遇到一个Action里面Service注入失败,换种说法应该说是根本没有发生注入,本来很简单的一个问题,但由于在项目中多个Action进行了继承,才最终导致了这个看似奇怪的问题。下面小记下这个过程: 收到同事问题,“有个Action请求一直调用报控指针,service一直是空的导致的!” 初步看了代码及配置,没有发现什么问题,起初怀疑是Action没有...
(转)spring使用注解方式通过子类注入到父类中的私有属性
spring版本2.5.6 用于类似HibernateDaoSupport这样的,子类不能重写父类私有属性的gettersetter; 不使用注解方式需要在配置文件中配置一下信息 &amp;lt;bean id=&quot;commonDao&quot; class=&quot;com.zxf.oa.dao.hibernate.HibernateCommonDao&quot; scope=&quot;prototype&quot; &amp;gt; &amp;lt;p...
Spring 父类注入的属性 在子类中调用为null
在spring中注入属性的目标是实例而不是类 子类从父类继承的值是与实例无关的 所以子类的属性并没有被赋值 工作背景: 有一个BaseService 有3个Dao成员 DaoA DaoB DaoC 并且 spring 为这3个成员分别注入了值 这三个Dao是直接可以拿过来执行数据库访问的 但是有一个BaseService的子类 xxx  也是拿来执行数据库访问的  表面上 它
Spring使用@PostConstruct注解完成子类注入父类私有变量
原博在此:http://www.cnblogs.com/walson/p/3897661.html 讲了三个使用注解在子类中替换父类私有变量的方法,其中@PostConstruct方法有误。 注解使用方式应为如下: 父类: public class BaseServiceImpl { private BaseDao baseDao; public BaseDao getB
java反射导致spring自动注入失败
这个地方困扰了我很久,一直没有找到答案,网上的一些回答都只是小片段很是让人为难,这里记录一下自己的解决方式,免得以后忘记。首先我的spring 的bean是我通过扫描包的方式来自动注入的(com.spring.service是我的包名)&amp;lt;context:component-scan base-package=&quot;com.spring.service&quot; /&amp;gt;正是因为我是通过扫描的方式,故只...
Spring父类属性无法注入
配置文件:rn父类:rn rn rn rn rn rn子类:rn rn rn rn rn rnrn父类属性:rn ………………………………………………………………rn protected ApplicationsService applicationsService;rn …………………………………………………………………rn public void setApplicationsService(ApplicationsService applicationsService) rn this.applicationsService = applicationsService;rn rnrn子类属性:rnpublic class UserAction extends BaseServiceSupport rn private MembersService membersService;rn public void setMembersService(MembersService membersService) rn this.membersService = membersService;rn rnrnrn在父类中,抛出applicationsService异常,判断applicationsService == null 为真~~为什么会这样呢?
spring注解注入泛型子类
最近在学spring的注解,但是现在遇到难题了,望各位帮下小弟的忙?rn以下是项目的工程图rn[img=https://img-bbs.csdn.net/upload/201310/17/1382023517_288231.gif][/img]rndao和service都使用了泛型设计rn泛型dao接口rn[code=java]public interface BaseDao [/code]rn泛型dao实现类rn[code=java]public class BaseDaoImpl implements BaseDao [/code]rn学员接口rn[code=java]public interface StudentDao extends BaseDao [/code]rn学员实现类rn[code=java]@Repositoryrnpublic class StudentDaoImpl extends BaseDaoImpl implements StudentDao [/code]rn使用junit进行测试的时候会报下面的异常。rnCaused by: org.springframework.beans.factory.[color=#FF0000]NoUniqueBeanDefinitionException[/color]: No qualifying bean of type [cn.ssh.annotation.dao.BaseDao] is defined: expected single matching bean but found 4: [color=#FF0000]baseDaoImpl,studentDaoImpl,teacherDaoImpl,studentServiceImpl[/color]rn[code=java]ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");rnStudentDao studentDaoImpl = (StudentDaoImpl) context.getBean("studentDaoImpl")[/code]rnrn不知道为什么会这样,难道spring不支持泛型的注解吗?rn还有想问下,ssh整合的话,有哪些注解可以用,每个框架都要相应的jar包吗?rnspring如何对事物进行注解呢?rn或者能够提供相应的文档或者链接地址.rn小弟将万分感谢!!!
spring 注入失败报错
报错代码片段: Error creating bean with name 'itemServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire...
spring bean 注入失败
1.异常信息 2.有可能引起的原因: 1.在applicationContext-service.xml的配置文件里的包扫描不对 2.在web.xml里没有加载spring容器 3.分布式工程,使用dubbo通信,在服务层,或者消费层, 单词写错了 4.还有一种可能,有可能是pom 里的jar包冲突 当时我看了这些就去解决,但是都无效 这个异常属于bean创建异常,原因是我的xml的路径设...
autoware自动注入失败
注入失败,个人总结 有这么几点, 1:spring 配置文件没有扫描包 检查下有没有这句 2:你的被注入的类有没有被spring管理 也就是有没有加注解, 3,:还有一点是一般新手可能遇到的,就是给servlet注入,这点和第二点是一样的,就是servlet不是被spring管理的,而是被tomcat或者其他服务器容器管理的,两者使用的不是同一个对象,这时,如果需要在你的servlet中使用
@autowired自动注入失败
Error creating bean with name 'employeeServiceImpl': Unsatisfied dependency expressed through field 'empMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.hdd.mapper.EmployeeMapper' available: ...rn怎么改啊?
spring 注入失败
web.xmlrnrnrn rn contextConfigLocationrn classpath:applicationContext-*.xmlrn rn rn org.springframework.web.context.ContextLoaderListenerrn rn rn dispatcherServletrn org.springframework.web.servlet.DispatcherServletrn rn contextConfigLocationrn /WEB-INF/profiles/applicationContext.xmlrn rn 1rn rn rn dispatcherServletrn *.dorn rnrnrn CloudServpagern rn index.htmlrn index.htmrn index.jsprn default.htmlrn default.htmrn default.jsprn rnrnrnapplicationContext.xmlrnrnrn rn rn rn org.springframework.web.servlet.view.JstlViewrn rn rn /WEB-INF/web/rn rn rn .jsprn rn rnrn rn rn rn rn loginActionrn loginActionrn loginActionrn loginActionrnrn roleActionrn roleActionrnrn houseActionrn houseActionrn houseActionrn rn rnrn rnrn rn rn rn rnrnrnrnapplicationContext-ibatis.xmlrnrnrnrnrn rn rn rn java:comp/env/jdbc/CloudServpagern rn rnrn rn rn rn rnrn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rnrn rn rn rn rn rnrnrnrn rn rn rn rn rn rn classpath:springSqlMapConfig.xmlrn rn rnrn rn rn rn rn rn rn rn rn rn rn rn rn rn rnrnrnapplicationContext-beans.xmlrnrnrn rn rn rnrn rn rn rn rnrn rn rn rn rnrnrnrnrn以上是我的spring配置文件rn下面是action 使用 对象时,发现对象没有注入,HouseBusiness为null。rnpublic class LoginAction extends MultiActionController rnrn private HouseBusiness housebusiness;rn rn public HouseBusiness getHousebusiness() rn return housebusiness;rn rnrn public void setHousebusiness(HouseBusiness housebusiness) rn this.housebusiness = housebusiness;rn rnrnibatis也注入失败rn严重: Servlet.service() for servlet dispatcherServlet threw exceptionrnjava.lang.IllegalArgumentException: No SqlMapClient specifiedrn
spring 注入失败!!!!!!!!
applicationContext.xmlrn[code=XML]rnrn rn rn rn rn rn rn rn rn rn rn rn rn rn rn[/code]rnstruts.xmlrn[code=XML]rnrn rn rn rn rn rn rn[/code]rnloginAction.xmlrn[code=XML]rn rn rn rn /sendSuccss.jsprn rn rn rn[/code]rnweb.xmlrn[code=XML]rn rn contextConfigLocationrn rn /WEB-INF/applicationContext*.xmlrn rn rn rn org.springframework.web.context.ContextLoaderListenerrn rn[/code]rnUserServiceImpl.javarn[code=Java]rnpublic class UserServiceImpl implements UserService rn public UserDAO userDao;rnrn public UserDAO getUserDao() rn return userDao;rn rnrn public void setUserDao(UserDAO userDao) rn this.userDao = userDao;rn rn[/code]rnrn[code=Java]rnpackage com.quxiuna.struts.action.login;rnrnimport java.util.Map;rnrnimport com.opensymphony.xwork2.ActionContext;rnimport com.opensymphony.xwork2.ActionSupport;rnimport com.quxiuna.hibernate.po.RbacUser;rnimport com.quxiuna.service.UserService;rnimport com.quxiuna.util.Constants;rnrnpublic class LoginAction extends ActionSupport rn rn private static final long serialVersionUID = -1640288447822607013L;rn rn private String name;rn private String password;rn private String message;rn public UserService userService;rnrn public String getName() rn return name;rn rn rn public void setName(String name) rn this.name = name;rn rnrn public String getPassword() rn return password;rn rnrn public void setPassword(String password) rn this.password = password;rn rn rn @SuppressWarnings("unchecked")rn public String execute() rn RbacUser user = userService.login(name, password);rn if (user != null) rn ActionContext ctx = ActionContext.getContext(); rn Map session = ctx.getSession(); rn session.put(Constants.SESSSION_NAME, name);rn return SUCCESS;rn else rn message = "用户名或密码错误!"; rn return ERROR;rn rn rnrn public String getMessage() rn return message;rn rnrn public void setMessage(String message) rn this.message = message;rn rnrn public UserService getUserService() rn return userService;rn rnrn public void setUserService(UserService userService) rn this.userService = userService;rn rnrn[/code]rnrn报错rnjava.lang.NullPointerExceptionrn com.quxiuna.struts.action.login.LoginAction.execute(LoginAction.java:38)rn sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)rn sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)rn java.lang.reflect.Method.invoke(Unknown Source)rn com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:404)rn com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:267)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:229)rn …………………………………………rn …………………………………………
Spring 注入失败的原因
有一次写代码如下   ApplicationContext ctx = new ClassPathXmlApplicationContext(&quot;beans.xml&quot;);  HarborBusiness service = (HarborBusiness) ctx.getBean(&quot;harborBusiness&quot;);   总是报no bean named   查了半天没查出来了,后来把前一...
Spring @Autowired注入失败
今天做项目的时候出现了个问题,异常简要信息如下rn[code=java]Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.yuansheng.jst.permission.service.IPermissionService] is defined: expected single matching bean but found 2: permissonService,IPermissionService[/code]rn接口:rn[code=java]public interface IPermissionService rn[/code]rn实现类:rn[code=java]@Servicernpublic class PermissonService implements IPermissionService rn @Autowiredrn private PermissionMapper permissionMapper;rn rn[/code]rn控制器rn[code=java]@Controllerrn@Path("/permission")rn@Consumes(MediaType.APPLICATION_FORM_URLENCODED)rn@Produces(MediaType.APPLICATION_JSON)rnpublic class PermissionResource rn private Logger logger = Logger.getLogger(this.getClass());rn @Autowiredrn private IPermissionService permissionService;rnrn[/code]rnrn之前一直是这么注入的,没想到会出错,仔细检查后发现是由于实现类的名称中少写了一个字幕i导致的,即PermissionService写成了PermissonService,修改后就正确了。rn那么问题来了,@AutoWired默认是byType注入的,即使我的类名写错,接口也只有一个实现类,为什么会注入失败?而且异常信息是扫描到了两个可注入的类,其中一个还是IPermissionService,那么如果我实现类的名称写正确的话,为什么就能正确注入了,而没有扫描到两个?
@Resource注入spring失败
普通的class中的代码rn1、@Resourcern private AdBannerService bannerService;rnrnAdBannerService为接口 提供了AdBannerServiceImpl实现类rnrn2、spring的配置:是一次性把整个包都注入的rn[color=#FF0000] rn [/color]rn运行List adBanners=bannerService.adBannerList();rn就会报NullpointException错误,框架是SSI的rn不知道为什么会注入失败?求解!rnrn rn
父类与子类
这个份博客的起因 A b = new B (); ,这里的b并不是A的实例,而是B的实例。 如何理解 B c = new B(); A a = c; A是B的父类 当构造函数相同时,优先编译父类如: 但子类有override修饰时,c就可以调用 刚接触:重写,重载,隐藏不太了解 慢慢理解 public abstract class A { public A() ...
父类子类
[code=C/C++]rnclass T1rnrnpublic:rn void Speak()rn rn printf("I am 1\n");rn ; rn;rnrnclass T2 :public T1rnrnpublic:rn void Speak()rn rn printf("I am 2\n");rn ;rnrn;rnrnrnvoid main()rnrn T1 *t1 = new T1();rn T1 *t2 = new T2();rnrn t1->Speak();rn t2->Speak();rnrnrn[/code]rnrn为什么t2调用的是父类的而不是自己的Speak()啊
子类、父类
小弟有一问题:rnclass arnrn b u[];rnrnclass b :public arnrnrnrn这样会出问题吗?(就是在父类中引用子类)
C++ 子类转父类 父类确定子类
将一个结构体转换为字节数组,再讲字节数组还原成结构体,子类转换为父类,通过父类信息确定是哪个子类转换的父类
父类、子类
子类对象创建之前,会产生父类对象。然后在父类对象基础之上,扩展子类特有的属性和方法。所以子类对象中包含完整的父类对象,就可以重用父类中定义的方法和属性。 子类对象创建之前,需要通过super()调用父类的够造方法,产生父类对象。如果子类构造方法中没有书写super(),这是,虚拟机会自动调用super()。如果父类中没有无参构造方法,子类必须通过super(参数)显示调用
子类 父类
[code=Java]rnpublic class SaveBiz rn byte[] message_type = new byte[ConstantSize.MESSAGE_TYPE_SIZE];rn byte[] processing_code = new byte[ConstantSize.PROCESSING_CODE_SIZE];rn byte[] trans_no = new byte[ConstantSize.TRANS_NO_SIZE];rn rn rn public byte[] getMessage_type() rn return message_type;rn rn public void setMessage_type(byte[] message_type) rn this.message_type = message_type;rn rn public byte[] getProcessing_code() rn return processing_code;rn rn public void setProcessing_code(byte[] processing_code) rn this.processing_code = processing_code;rn rn public byte[] getTrans_no() rn return trans_no;rn rn public void setTrans_no(byte[] trans_no) rn this.trans_no = trans_no;rn rn rnrnrnrnpublic class SubSaveBiz extends SaveBizrn byte [][]sortedBiz= new byte[][]rn super.message_type,rn super.processing_code,rn super.trans_no,rn ;rn public byte[][] getSortedBiz() rn return sortedBiz;rn rnrnrn[/code]rnrnrn如上code中,在父类SaveBiz 通过set方法来设置值,影响不到子类的sortedBiz数组里面的值...谁能解释一下??rn 如何才能让其同步??
强转失败的情况,父类转子类。
/** * 一个java文件中只能包含一个public类。 * * @author jiaxutianhuo * */ public class Instanceof { // 内部类 class a { } public static void test(F f){ f.t1(); // C2 c2=(C2)f; // c2.t2();相当于下一行写法,将f强制转
子类调用父类构造失败,为什么?
父类的构造函数如下:rnpublic:rn __fastcall TADOObject();rn __fastcall TADOObject(AnsiString & SQLQuery);rnrn子类的构造函数这么写:rn__fastcall TOrdersDM::TOrdersDM(int ID)rn : TADOObject(Format("SELECT * FROM %s WHERE ID = %d", ARRAYOFCONST((TableName(),ID))))rnrn //rnrnrn报错如下:rn [C++ Error] ProblemDomain.cpp(30): E2034 Cannot convert 'AnsiString' to 'TADOObject'rn [C++ Error] ProblemDomain.cpp(30): E2340 Type mismatch in parameter 1 (wanted 'const TADOObject &', got 'AnsiString')rn--rn
spring继承父类注入失败,报错空指针的一个解决办法
众所周知,抽象出来一个BaseDao的好处是巨大的,可以减少很多后续的DAO的工程量,只需让其他的dao继承这个baseDAO即可,然额问题来了,这个baseDAO,我们要用到HibernateTemplate。 我们把HibernateTemplate注入后,像寻常一样运行,子类像寻常一样调用父类,然后gg,报错空指针,查了半天最终发现,是hibernate没注进去,为啥呢,父类被调用了啊? ...
Spring自动注入
注意,注入的是bean标签下property的name与另一个beasn的id相同时,可以省略 例如当spring整合mybatis时 &amp;amp;amp;lt;bean id=&amp;amp;quot;dataSource&amp;amp;quot; class=&amp;amp;quot;org.springframework.jdbc.datasource.DriverManagerDataSource&amp;amp;quot;&amp;amp;amp;gt; &amp;
Spring 自动注入报错
哪位大侠给瞅瞅啊rnorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: richeninfo.dao.UserMapper richeninfo.service.UserService.userMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [richeninfo.dao.UserMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: @org.springframework.beans.factory.annotation.Autowired(required=true)rnRelated cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userMapper' defined in file [D:\sts\configSystem\target\classes\richeninfo\dao\UserMapper.class]: Cannot resolve reference to bean 'sqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-mybatis.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\sts\configSystem\target\classes\richeninfo\mapping\UserMapper.xml]'; nested exception is java.lang.RuntimeException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving JdbcType. Cause: java.lang.IllegalArgumentException: No enum const class org.apache.ibatis.type.JdbcType.Integerrn at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)rn at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)rn at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)rn at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)rn at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)rn at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)rn at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)rn at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)rn at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)rn at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:93)rn at richeninfo.test.UserTest.findUser(UserTest.java:24)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)rn at java.lang.reflect.Method.invoke(Method.java:597)rn at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)rn at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)rn at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)rn at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)rn at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)rn at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)rn at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)rn at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)rn at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)rn at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)rn at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)rn at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)rn at org.junit.runners.ParentRunner.run(ParentRunner.java:309)rn at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)rn at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)rn at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)rn at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)rn at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)rn at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)rnCaused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: richeninfo.dao.UserMapper richeninfo.service.UserService.userMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [richeninfo.dao.UserMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: @org.springframework.beans.factory.annotation.Autowired(required=true)rn at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)rn at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)rn at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)rn ... 36 morernCaused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [richeninfo.dao.UserMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: @org.springframework.beans.factory.annotation.Autowired(required=true)rn at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:949)rn at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:818)rn at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)rn at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)rn ... 38 morernRelated cause:rnorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userMapper' defined in file [D:\sts\configSystem\target\classes\richeninfo\dao\UserMapper.class]: Cannot resolve reference to bean 'sqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-mybatis.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\sts\configSystem\target\classes\richeninfo\mapping\UserMapper.xml]'; nested exception is java.lang.RuntimeException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving JdbcType. Cause: java.lang.IllegalArgumentException: No enum const class org.apache.ibatis.type.JdbcType.Integerrn at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329)rn at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1391)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1132)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)rn at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)rn at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)rn at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)rn at org.springframework.beans.factory.support.AbstractBeanFactory.getTypeForFactoryBean(AbstractBeanFactory.java:1357)rn
Spring自动注入“失效”
spring的bean被织入切面后,直接访问他的成员变量,会得到null值。 最糟糕的是不会有任何报错,很难发现。 这个问题的真正原因是,spring的AOP基于动态代理实现,也就是说我们拿到的bean的class不是他本身的class,而是spring自动生成的一个代理对象。//比如我们有这样一个类 public final class HelloMessage { //我们期望这个
Spring自动注入byType
**1.applicationContext.xml配置文件** <!-- 配置dao层bean --> <bean id="edi" class="dao.impl.EmpDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> //
Spring的自动装配和自动注入
Spring的自动装配和自动注入是不是一个概念啊?很难理解自动装配是怎么装配,还有什么n自动注入的。。头大。。还有那个什么byType和byName这是什么?用这些东西想要达到n一个什么样的目的?还有setter注入的时候,我怎么没见xml里面有Setter??nn那位大神能说明白点。。。
2. spring自动注入
1.DI的实现方式----自动注入   在bean标签中 加属性  autowire   取值有   no   默认值  byName   根据属性的名字  去容器中找对应的组件id  byType   根据属性的类型去查找   类型有一样就报错  constructor   优先按照构造方法参数的名字去查找  如果找不到 则启用参数的类型去查找               
Spring的自动注入
1. 除非把每个bean都用&amp;lt;bean&amp;gt;标签写入到XML文件中,否则一般情况下我们需要用到&amp;lt;context:component-scan&amp;gt;标签,解析该标签需要用到ComponentScanBeanDefinitionParser,具体的解析接上文https://blog.csdn.net/ph3636/article/details/79397678,在解析过程中,Anno...
Spring自动注入properties文件
1. 因为在下面要用到Spring的和配置,所以,首先要引入其对应的命名空间,即要在applicationContext.xml中加入以下的配置: Xml代码  收藏代码 xmlns:util="http://www.springframework.org/schema/util"       xsi:schemaLocation="        http://www.springfra
spring自动注入问题
使用IDEA工具时使用@Resource和@Autowired自动注解bean时会显示红色,但是项目能运行  解决方法:  File – Settings – Inspections。在Spring Core – Autowring for Bean Class 中将Severity的级别由之前的error改成warning。...
相关热词 c#检测非法字符 c#双屏截图 c#中怎么关闭线程 c# 显示服务器上的图片 api嵌入窗口 c# c# 控制网页 c# encrypt c#微信网页版登录 c# login 居中 c# 考试软件