spring之注解配置bean

来源:互联网 发布:java https post请求 编辑:程序博客网 时间:2024/05/21 11:12
  1. @component:通用 @repository:标识持久层 @service:标识业务层 @controller:标识控制层
    注解之后还要在.xml文件中的中声明要去扫描的,才会起来注解的作用

  2. base-package属性:用来指定要扫描的包,可以有多个,多个的话用“,”来分隔;

  3. 指定要扫描的包;
    指定不会去扫描的包。
    用这两个属性之一定要先设置中的属性值为true。如果一个bean被ioc容器管理了,那么它有右上角会有“s”小标签。

    4.要导入context命名空间才会使注解起作用
    如图所示,这里就是选择context命名空间的

5.要在bean前面标记这个@Repository(“userRepository”)

package com.spring.annotation.repository;import org.springframework.stereotype.Repository;@Repository("userRepository")public class UserRepositoryImpl implements UserRepository{@Overridepublic void save() {System.out.println("userRepository的实现类,save方法");    }}7. resource-pattern属性来过滤特定的类,用了这个之后别的都不会注入了。



8.指定不包含的类型的bean
<!-- 指定Spring ioc容器扫描的包 --><context:component-scan base-package="com.spring.annotation"><!-- 指定不包含的类型的bean -->    <context:exclude-filter type="annotation"        expression="org.springframework.stereotype.Repository" /></context:component-scan>
如上代码,不包含@Repository注解的bean了。9.指定只包含哪种类型组件的bean<!-- 指定Spring ioc容器扫描的包 -->    <context:component-scan base-package="com.spring.annotation" use-default-filters="false">    <!-- 指定只包含特定组件类型的bean --><context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>    </context:component-scan>

如上代码,指定只包含@Repository注解的bean,只个要在context:component-scan中设置use-default-filter属性值为false。
指定只包含什么类,要设置context:component-scan=“true”;
指定不包含什么类。

0 0