spring通过注解配置Bean


Spring通过注解配置Bean

一、注解类型及基础配置
@Component: 基本注解, 标识了一个受 Spring 管理的组件
@Respository: 标识持久层组件
@Service: 标识服务层(业务层)组件
@Controller: 标识表现层对于扫描到的组件Spring 有默认的命名策略使用非限定类名第一个字母小写也可以在注解中通过 value 属性值标识组件的名称组件

在这里,我们不希望扫描到RoleService接口,而扫描到RoleServiceImpl且为了方便,我们让其Bean的名字为RoleService
	Interface RoleService{
}

@Service(value="RoleService")
public class RoleServiceImpl implements RoleService{
}


除了要在类中使用注释,还应在spring的配置文件中添加声明要扫描的包
首先,在spring配置文件中导入context命名空间,再添加声明
<context:component-scan base-package="com.atguigu.spring.ref"></context:component-scan>
base-package 属性指定一个需要扫描的基类包Spring 容器将会扫描这个基类包里及其子包中的所有类.
当需要扫描多个包时可以使用逗号分隔


 二、通过属性或子节点指定IOC容器对哪些类的注解特殊处理
1、
可以用resource-pattern属性指定只扫描特定包下的类

    <context:exclude-filter>子节点表明要排除哪些指定表达式的组件,如
<context:component-scan 
        base-package="com.atguigu.spring.beans.annotation">
                <context:exclude-filter type="annotation"
        expression="org.springframework.stereotype.Repository">
</context:component-scan>
将排除@Repository组件


<context:include-filter>子节点指定要扫描的指定表达式的组件
需要配合user-default-filters=false"表明取消默认四个组件都扫描的行为
<context:component-scan 
        base-package="com.atguigu.spring.beans.annotation"
        user-default-filters=false">
                <context:exclude-filter type="annotation"
     expression="org.springframework.stereotype.Repository">
</context:component-scan>

<context:component-scan>下可以拥有若干个<context:exclude-filter>和<context:include-filter>


 2、
以下是上面两种子节点的类型
 annotation类是指定组件 (也就是某类注释)
assignable与annotation用法一样,不过他是以某个类为单位的

3、
@Autowired注解自动装配具有兼容类型的单个bean属性
    @Service
public class UserService {

   @Autowired//@Autowired(required=false)
   private UserDao userDao;
   
   public void setUserDao(UserDao userDao) {
      this.userDao = userDao;
   }
   
   public void addNew(){
      System.out.println("addNew...");
      userDao.save();
   }
   
}
所有使用@Autowired注解的属性都需要被设置(即那个属性的类被注释了),不然会报错
如果允许不被设置,可以设置required=false
如果如上图UserDao是一个接口且有两个及以上的实现类,自动装配时容易发生异常,但只需要有一个类的注释的value值为userDao,spring就会自动识别该类为应该被选择的类
或使用@Qulifier注解来标识要配置的bean
    @Service
public class UserService {

   @Autowired//@Autowired(required=false)
   private UserDao userDao;
   
   public void setUserDao(@Qualifier("userdaolmpl")UserDao userDao) {
      this.userDao = userDao;
   }
   
   public void addNew(){
      System.out.println("addNew...");
      userDao.save();
   }
   
}

 作为了解
 

 
三、泛型依赖注入:
若声明两个泛型父类
    1.public class BaseRepository<T> repository{
 	}
    2.public class BaseService<T>{
    @Autowired//注意 这里就是重点    父类在这里使用了,子类会继承且自动识别类型
    protected BaseRepository<T> repository;
    
    public void add(){
        System.out.println("add");
        System.out.println(repository);
        }
}





   有子类
     public class User{
}

@Repository
public class UserRepository extends BaseRepository<User>{
}

@Service
public class UserService extends BaseService<User>{
}


main中getBean("userService")然后调用其add方法
控制台出现
add
com.spring.UserRepository@15099a1
足以说明结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值