偶尔发现的

偶尔发现的

我开启注解扫描发现StudentDao居然自动指向StudentDaoImpl

package com.san.test.demo3;

import org.springframework.stereotype.Component;

@Component
public class StudentDaoImpl implements StudentDao {
   @Override
   public void find() {
       System.out.println("学生查询");
   }

   @Override
   public void save() {
       System.out.println("学生保存");
   }

   @Override
   public void update() {
       System.out.println("学生修改");
   }

   @Override
   public void delete() {
       System.out.println("学生删除");
   }
}


package com.san.test.demo3;

public interface StudentDao {
   public void find();
   public void save();
   public void update();
   public void delete();
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">

   <context:component-scan base-package="com.san.test.demo3"/>


   <!---->
  <!-- <bean id="studentDao" class="com.san.test.demo3.StudentDaoImpl"></bean>
   &lt;!&ndash;前置通知类型&ndash;&gt;
   <bean id="myBeforeAdvice" class="com.san.test.demo3.MyBeforeAdvice"/>

   &lt;!&ndash;Spring的Aop产生代理对象&ndash;&gt;
   <bean id="studentDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
       &lt;!&ndash;配置目标类&ndash;&gt;
       <property name="target" ref="studentDao"/>
       &lt;!&ndash;实现接口&ndash;&gt;
       <property name="proxyInterfaces" value="com.san.test.demo3.StudentDao"/>
       &lt;!&ndash;采用拦截器的名称&ndash;&gt;
       <property name="interceptorNames" value="myBeforeAdvice"/>
   </bean>-->
</beans>



@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:Application.xml")
public class SpringTest {
   /*@Resource(name = "studentDao")*/
   @Autowired
   private StudentDao studentDao;

   @Test
   public void demo1(){
       studentDao.delete();
   }
}

http://img1.sycdn.imooc.com//climg/5ea147010922301104650263.jpg

这是为什么呢,我的application.xml就写了开启扫描注解,但却能自动生成实现类的实例化

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

4回答
好帮手慕阿满 2020-04-24 18:49:41

同学你好,使用@Autowired注入的是实现类,并不是接口。在StudentDaoImpl类上添加@Component注解,将会生成StudentDaoImpl类的bean,注入到spring中。

在测试类中,使用@Autowired注入时,注入的StudentDao类型的StudentDaoImpl实例。

StudentDao是接口,不能实例化。

祝:学习愉快~

好帮手慕阿满 2020-04-24 16:39:01

同学你好,在SpringTest中,加载的是Application.xml文件,如:

http://img1.sycdn.imooc.com//climg/5ea2a561092cd19d05770202.jpg

问一下同学上边贴的代码时Application.xml的配置文件吗?建议同学检查一下。

祝:学习愉快~

  • 提问者 cccca #1
    就是这个文件,我在这个配置文件就开启了注释扫描,其他的都没写,也没写生成bean。再定义一个StudentDao接口。写了一个StudentDaoImpl里面继承StudentDao接口的方法。然后用@Component自己创建实例给IOC,测试的时候用@Autowird获得实例化给StudentDao,然后问获得的StudentDao实例化怎么会自动指向StudentDaoImpl,而不是生成他自己类型的StudentDao
    2020-04-24 16:49:55
  • 提问者 cccca #2
    或者这样说吧使用@Component StudentDaoImpl——StudentDaoImpl s=new StudentDaoImpl;而StudentDao是接口没写@Component;但测试时候我利用@Autowird从IOC获取一个实例给StudentDao。这时候的StudentDao就变成了StudentDao s=new StudentDaoImpl(); 我想问为什么不是StudentDao s=new StudentDao()呢?@Autowird不是获取相同类型的实例吗,应该是StudentDao
    2020-04-24 16:57:30
好帮手慕阿满 2020-04-24 15:39:31

同学你好,配置目标类studentDao时,指向了StudentDaoImpl类,如:

http://img1.sycdn.imooc.com//climg/5ea2978d0941d31908620122.jpg

祝: 学习愉快~

  • 提问者 cccca #1
    可是我没写具体的StudentDaoImpl的id啊,我用@Autowired,他根据类型找到的应该是StudentDao啊。不然我写多几个StudentDao的继承方法,他怎么辨别啊
    2020-04-24 15:44:25
  • 提问者 cccca #2
    这bean我没写,我就开启扫描注解<context:component-scan base-package="com.san.test.demo3"/>
    2020-04-24 15:46:29
好帮手慕小班 2020-04-23 18:07:42

同学你好,根据同学贴出的代码内容:

    1、@Component泛指组件,表示把普通类实例化到spring容器中,也就是告诉Spring创建相应的bean。

http://img1.sycdn.imooc.com//climg/5ea1684b09df15d704960156.jpg

如上所示,是StudentDao类型的StudentDaoImpl类。

    2、@Autowired默认是按照类去匹配,将要对应类型的dao注入

http://img1.sycdn.imooc.com//climg/5ea168b8098bbbca03700170.jpg

如上所示,完成了对应的StudentDao的注入。

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

  • 提问者 cccca #1
    StudentDao类型怎么会是StudentDaoImpl类,不应该是StudentDao类吗?
    2020-04-24 14:57:06
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师