spring IOC的注解使用

一、使用注解的方式注册bean

添加到类上,用于注册bean的注解

注解名标识
@Component组件
@Controller放在控制层
@Service放在业务逻辑层
@Repository放在数据访问层

以上任意一个注解都可以完成注册bean的功能,不会对注册有影响。只是用于提高可读性。

在注解没有定义id时,默认是以类名的首字母小写

默认是单例
@Scope设置bean是否是单例(prototype多例)

1、需要在xml文件中添加配置

xml头需要定义命名空间,也可以自动添加
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:	后面加	https://www.springframework.org/schema/context/spring-context.xsd

二、定义扫描的包时要包含的类

可以根据需要做到更细粒度的控制。选择想要扫描的类和不想扫描的类。

 <!--必须指明从哪个包开始扫描-->
    <context:component-scan base-package="com.mashibing">
        <!--定义好扫描包后,可以做更细粒度的控制-->
        <!--不包含哪个类-->
        <!--<context:exclude-filter type="assignable" expression="com.mashibing.controller.PersonController"></context:exclude-filter>-->
        <!--包含哪个注释-->
        <!--<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:include-filter>-->
    </context:component-scan>

filter有两个属性

type是规则类型expression是表达式
assignable指定类的名称,必须是完全限定名
annotation指定注释,必须是完全限定名
regex正则表达式,一般不用
aspectj使用切面的方式,一般不用
custom使用自定义的方式,一般不用

三、@Autowired注解

1、给成员变量添加@Autowired注解,可以自动装配(起到的作用就是中的,给成员变量初始化)

  1. 默认按照ByType来进行装配。如果找到直接赋值。
  2. 如果没找类会抛异常。
  3. 如果有多个类型一样的类(子类继承父类),此时按照id查找。
public class BaseController<T> {
   @Autowired
   TeacherDao teacherDao;

   public void save(){
      personDao.save();
   }
}

2、当@Autowired添加到方法上的时候,此方法在创建对象的时候会默认调用,同时方法里的参数会自动装配。

@Autowired
public void test(PersonDao personDao){
    System.out.println("此方法被调用:"+personDao);
}

3、@Qualifier如果想要通过id查找,可以规定自己的想用的名称。还可以用到参数上。

public class PersonController {
   @Autowired
   @Qualifier("personService")
   private PersonService service;
   public void save(){
      service.save();
   }
}

4、@Resource:可以完成跟@Autowired相同的功能。但是有一些区别

  1. @Resource是jdk提供的功能。@Autowride是spring提供的。
  2. @Resource可以在其他框架使用。
  3. @Resource是按照名称装配的,找不到在找类型。@Autowired是按照类型装配,找不到使用名称。

四、泛型依赖注入

父类有泛型,子类规定具体泛型类型。在自动注入是可以根据泛型选择适当子类注入。

controller

public class BaseController<T> {
	@Autowired
	PersonService<T> personService;

	public void save(){
		personService.save();
	}
}
/*-------------------------Teacher-----------------------------*/
@Controller
public class TeacherController extends BaseController<Teacher> {
}
/*-------------------------Student-----------------------------*/
@Controller
public class StudentController extends BaseController<Student> {
}

service

public class PersonService<T> {
   @Autowired
   private PersonDao<T> dao;
   public void save(){
      dao.save();
   }
}
/*-------------------------Teacher-----------------------------*/
@Service
public class TeacherService extends PersonService<Teacher> {
}
/*-------------------------Student-----------------------------*/
@Service
public class StudentService extends PersonService<Student> {
}

dao

public abstract class PersonDao<T> {
	public abstract void save();
}
/*-------------------------Teacher-----------------------------*/
@Repository
public class TeacherDao extends PersonDao<Teacher> {
	public void save(){
		System.out.println("保存老师");
	}
}
/*-------------------------Student-----------------------------*/
@Repository
public class StudentDao extends PersonDao<Student>{
   public void save(){
      System.out.println("保存学生");
   }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值