spring - ioc - 注解创建对象与注入属性

1、创建对象有四个注解(类上)

(1)@Component:web层,确定不了事哪一个层的时候使用

(2)@Controller:控制层,就是我们的action层

(3)@Service:业务逻辑层,就是我们的service或者manager层

(4)@Repository:持久层,就是我们常说的dao层

  • 这四个注解的功能都一样,对创建对象
2、创建对象实单实例还是多实例(类上)
  • @Scope(value="singleton"):单实例
  • @Scope(value="prototype"):多实例
3、注入属性(属性上)

(1)@Autowired:根据属性类型自动装配

(2)@Qualifier:根据属性的名称注入要和@AutoWired一起使用

(3)@Resource:可以根据类型输入也可以根据名称注入

(4)@Value:注入普通类型属性

4、开启注解扫描器(application.xml)
    <!--1.开启注解扫描器:扫描com.levi下的所有类-->
    <context:component-scan base-package="com.levi"/>

    <!--2.开启注解扫描器:只描com.levi下类型为annotation(注解),类为Service的类-->
    <context:component-scan base-package="com.levi" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>

    <!--3.开启注解扫描器:不描com.levi下类型为annotation(注解),类为Service的类-->
    <context:component-scan base-package="com.levi">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>

 

posted on 2021-12-08 14:47  每天积极向上  阅读(114)  评论(0编辑  收藏  举报

导航