一、使用注解的方式注册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注解,可以自动装配(起到的作用就是中的,给成员变量初始化)
- 默认按照ByType来进行装配。如果找到直接赋值。
- 如果没找类会抛异常。
- 如果有多个类型一样的类(子类继承父类),此时按照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相同的功能。但是有一些区别
- @Resource是jdk提供的功能。@Autowride是spring提供的。
- @Resource可以在其他框架使用。
- @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("保存学生");
}
}