spring之注解配置bean

  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{
@Override
public 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”;
指定不包含什么类。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值