spring注解定义bean

通过注解的形式将bean以及相应的属性值放入ioc容器

1)利用注解声明该类

@Component("student") //id名为:student
public class Student {
	private int stuNo;
	private String stuName;
	private int stuAge;
	private Teacher teacher;
}

2)配置扫描器

xmlns:context="http://www.springframework.org/schema/context"
<context:component-scan base-package="org.lanqiao.entity"></context:component-scan>//扫描某包(多个包用逗号分隔)

 执行流程:Spring在启动的时候,会根据base-package在 该包中扫描所有类,查找这些类是否有注解@Component("studentDao"),如果有,则将该类 加入spring Ioc容器。

在程序设计中:@Component()包含的范围太大,不易于理解。
对其@Component细化:
->dao层注解:@Repository
->service层注解:@Service
->控制器层注解:@Controlle

猜你喜欢

转载自blog.csdn.net/qq_41877184/article/details/103149196