2002年春
1.豆スコープ
7つのBeanスコープがあり、最も一般的に使用されるのはシングルトンとプロトタイプです
1.1シングルトンシングルトン
シングルトンはSpringコンテナのデフォルトスコープです。Beanのスコープがシングルトンの場合、Springコンテナには共有Beanインスタンスが1つだけ存在します。シングルトンスコープは、セッション状態のないBean(Daoコンポーネント、サービスコンポーネントなど)にとって最も理想的な選択肢です。
singleton 1.在容器启动只创建一个 id 为student1 bean 2.并且放置在容器中,容器管理他的生命周期
<!--
声明让容器创建一个 Student类型的bean id student1
scope="singleton" 容器创建bean scope 默认的 作用域是单例singleton
singleton 1.在容器启动只创建一个 id 为student1的 bean 2.并且放置在容器中,容器管理他的生命周期
-->
<bean class="com.yth.entity.Student" id="student1" scope="singleton">
<property name="id" value="100"></property>
<property name="name" value="赵四"></property>
</bean>
テスト
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("scope/student.xml");
//关于singleton 单例的测试
@Test
public void Test01(){
Student student = (Student) applicationContext.getBean("student1");
System.out.println(student);
}
1.2propertypeのプロトタイプ
ユーザーがコンテナからBeanを取得すると、コンテナが作成されます。コンテナは、その作成のみを担当し、ライフサイクルの維持と維持は担当しません。
<!--
声明让容器创建bean 作用域prototype
prototype 1.用户向容器获取时 创建id student2对象 2.每次都创建新的对象
3.容器只负责帮我们创建 不会放置在容器中,不会管理bean 的生命周期 ,由用户自己负责
-->
<bean class="com.yth.entity.Student" id="student2" scope="prototype">
<property name="id" value="101"></property>
<property name="name" value="广坤"></property>
</bean>
テスト
//关于propertype 原型的测试
@Test
public void Test02(){
Student student1 = (Student) applicationContext.getBean("student2");
System.out.println(student1.hashCode());
Student student2 = (Student) applicationContext.getBean("student2");
System.out.println(student2.hashCode());
}
2.Beanのライフサイクル
豆の誕生、老後、病気と死
シングルトンBean:コンテナが起動すると、Beanが作成され、コンテナはBeanのライフサイクルを維持する責任があります。
プロトタイプBean:Beanは、コンテナーの起動時に作成されず、ユーザーがBeanを取得したときにコンテナーによって作成されます。新しいBeanになるたびに、コンテナー部門がそのライフサイクルを管理します。
Springコンテナは、スコープのBean部分のライフサイクルを管理できます。説明は次のとおりです。
- シングルトン
Springコンテナは、シングルトンスコープでBeanのライフサイクルを管理できます。このスコープでは、Springは、Beanがいつ作成され、いつ初期化され、いつ破棄されるかを正確に知ることができます。
- プロトタイプ
Springは、プロトタイプスコープでのBeanの作成のみを担当します。コンテナがBeanインスタンスを作成すると、Beanインスタンスは管理のためにクライアントコードに渡され、Springコンテナはそのライフサイクルを追跡しなくなります。
例:
1.学生のライフサイクルに関連するメソッドを追加します
/**
* 初始化方法
*/
public void init(){
System.out.println("对象初始化");
}
/**
* 销毁方法
*/
public void destroy(){
System.out.println("对象销毁了");
}
2. lifebean.xmlを作成し、対応するメソッドを設定します
<!--
init-method="init" 容器创建bean 时调用 初始化方法
destroy-method="destroy" 当容器销毁时调用 销毁方法
-->
<bean class="com.yth.entity.Student" id="student1" scope="singleton"
init-method="init" destroy-method="destroy">
<property name="id" value="104"></property>
<property name="name" value="赵四"></property>
</bean>
<!--
声明 一个原型的bean 当用户获取时创建,容器只负责创建 不负责bean 的生命周期,由用户自己管理
当容器销毁时 bean 的destroy 不会被容器调用
-->
<bean id="student2" class="com.yth.entity.Student"
scope="prototype" init-method="init" destroy-method="destroy">
<property name="id" value="105"></property>
<property name="name" value="广坤"></property>
</bean>
3.テスト
//关于bean的生命周期
@Test
public void Test03(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("life\\lifeBean.xml");
// 向容器获取原型 对象此时 触发原型的创建
Student student1 = (Student) applicationContext.getBean("student1");
// 显示调用容器的销毁方法 销毁前会将容器中所有的bean 先销毁, 调用bean 的destroy()
Student student2 = (Student) applicationContext.getBean("student2");
applicationContext.destroy();
}
3.注釈に基づく組み立て
XMLベースのアセンブリにより、XML構成ファイルが肥大化しすぎて、その後のメンテナンスやアップグレードに特定の問題が発生する可能性があります。この目的のために、SpringはAnnotation(annotation)テクノロジーの包括的なサポートを提供します。
3.1挿入された注釈
@Autowired:Beanプロパティ変数、プロパティセッターメソッド、および構築メソッドに注釈を付け、対応する注釈プロセッサと連携してBeanの自動構成を完了するために使用されます。
@Qualifier:@Autowiredアノテーションと組み合わせて使用すると、Beanタイプ別のデフォルトのアセンブリは、Beanのインスタンス名によるアセンブリに変更されます。Beanのインスタンス名は、@ Qualifierアノテーションのパラメーターによって指定されます。
@Resource:その役割はAutowiredと同じです。@Resourceには、名前とタイプという2つの重要な属性があります。Springは、name属性をBeanインスタンス名に解決し、type属性をBeanインスタンスタイプに解決します。
// @Autowired // 根据类型进行注入若有,多个类型的实例,需要配合 @Qualifier("studentDaoImpl2")
// @Qualifier("studentDaoImpl2")
@Resource() // 默认按照注解的字段名称进行装配, 若没有找到根据类型装配,如果对应类型的实例有多个需要限定
private StudentDao studentDao ;
3.2生成されたインスタンスの注釈
@Component:SpringのBeanを説明するために使用されます。これは一般化された概念であり、コンポーネントのみを表します。
@Repository:Springでデータアクセスレイヤー(DAO)クラスをBeanとして識別するために使用されます。daoオブジェクト、データベースにアクセスできます
@Service:Springでビジネスレイヤー(サービス)クラスをBeanとして識別するために使用されます。サービスオブジェクトは、ビジネス処理を実行でき、業務などの機能を備えています。
@Controller:Springでコントロールレイヤー(コントローラー)のクラスをBeanとして識別するために使用されます。コントローラオブジェクトは、ユーザーによって送信されたパラメータを受け入れ、要求の処理結果を表示できます。
3.3半自動注釈アセンブリ
挿入された注釈を有効にするためにのみ挿入できます。Beanの生成もxmlで構成する必要があります。
実装はcontext:annotation-config </ context:annotation-config>です。
1.最初にコンテナでBeanを宣言します
2.対応する注入注釈を使用してBeanを注入します
3.挿入された注釈が有効になります
<!--
作用就是让 注入的注解生效
-->
<context:annotation-config></context:annotation-config>
<!--1. 声明 bean-->
<bean id="studentDao1" class="com.yth.dao.impl.StudentDaoImpl"></bean>
<bean id="studentDao2" class="com.yth.dao.impl.StudentDaoImpl"></bean>
<bean id="studentService" class="com.yth.service.impl.StudentServiceImpl">
</bean>
public class StudentServiceImpl implements StudentService {
//注入注解 注入bean
@Autowired //去容器中 查找bean,并设置到当前属性中 如果容器有 多个类型的bean则报错
// @Qualifier("studentDao1")// @Qualifier 与@Autowired 配合 ,当根据类型找到多个时 使用 @Qualifier 按照id 或者name 查找为一个bean
// @Resource(name = "studentDao")// 首先按照 id 为属性名studentDao1 去容器中找到对应的bean 2.如果找不到 在按照类型去查找 如果找到多个 报错
private StudentDao studentDao;
public StudentDao getStudentDao() {
return studentDao;
}
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
public Student findStudentById(int id) {
return studentDao.fingStudentById(id);
}
}
<!--
作用就是让 注入的注解生效
-->
<context:annotation-config></context:annotation-config>
テスト
public class Test02 {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("annotation\\ annotation.xml");
//关于半自动注解装配测试
@Test
public void Test01(){
StudentService studentService = applicationContext.getBean(StudentService.class);
Student studentById = studentService.findStudentById(102);
System.out.println(studentById);
}
//增加studentDao 的bean的声明,id不同的测试,再添加一个注解@Qualifier("studentDao1"),获取id为studentDao1的bean
@Test
public void Test02(){
// StudentService studentService = applicationContext.getBean(StudentService.class);
StudentService studentService = (StudentService) applicationContext.getBean("studentService");
Student studentById = studentService.findStudentById(102);
System.out.println(studentById);
}
//@Resource(name = "studentDao1") 使用resource注解
@Test
public void Test03(){
StudentService studentService = (StudentService) applicationContext.getBean("studentService");
Student studentById = studentService.findStudentById(102);
System.out.println(studentById);
}
}
3.4自動注釈
コンテキスト:コンポーネントスキャン
自動注釈は、注釈とアセンブリによるBeanの自動生成です。
実装:コンテキスト:コンポーネントを宣言します-xmlでスキャンして、挿入された注釈(@Autowired @Qualifie @Resource)で生成されたBean注釈(@ Component @ Repository @Service @Controller)を対応するパッケージで有効にします
成し遂げる
1.自動注釈xmlをオンにします
<!--
开启 自动注解功能 使 注入的注解(@Autowired @Qualifie @Resource )生成bean的注解(@Component@Repository @Service @Controller)生效
-->
<context:component-scan base-package="com.qfedu"></context:component-scan>
2.Beanと主な注入プロパティを生成する必要性をマークします
ダオ
@Repository // 标记当前类是dao层
public class StudentDaoImpl implements StudentDao {
public Student fingStudentById(int id) {
Student student =new Student();
student.setId(102);
student.setName("刘能");
return student;
}
}
サービス
@Service
public class StudentServiceImpl implements StudentService {
//注入注解 注入bean
@Autowired //去容器中 查找bean,并设置到当前属性中 如果容器有 多个类型的bean则报错
// @Qualifier("studentDao1")// @Qualifier 与@Autowired 配合 ,当根据类型找到多个时 使用 @Qualifier 按照id 或者name 查找为一个bean
// @Resource(name = "studentDao")// 首先按照 id 为属性名studentDao1 去容器中找到对应的bean 2.如果找不到 在按照类型去查找 如果找到多个 报错
private StudentDao studentDao;
public StudentDao getStudentDao() {
return studentDao;
}
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
public Student findStudentById(int id) {
return studentDao.fingStudentById(id);
}
}
テスト
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("autoAnnotation\\autoAnnotation.xml");
//全自动注解的测试
@Test
public void Test01(){
StudentService studentService = (StudentService) applicationContext.getBean(StudentService.class);
Student studentById = studentService.findStudentById(102);
System.out.println(studentById);
}