Spring Bean工厂和上下文
程序员文章站
2022-05-23 10:19:34
...
package com.svse.entity;
import java.io.Serializable;
import java.util.List;
public class AdminEntity implements Serializable {
private String college;
private String tel;
private List ar;
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public List getAr() {
return ar;
}
public void setAr(List ar) {
this.ar = ar;
}
}
package com.svse.entity;
import java.io.Serializable;
public class UserEntity implements Serializable {
private int id;
private String name;
private AdminEntity admin;
public UserEntity(){}
public UserEntity(int id ,String name,AdminEntity admin){
this.id = id;
this.name = name;
this.admin = admin;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public AdminEntity getAdmin() {
return admin;
}
public void setAdmin(AdminEntity admin) {
this.admin = admin;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 1、解释:AdminEntity adminEntity = new AdminEntity(); -->
<bean id="adminEntity" class="com.svse.entity.AdminEntity">
<!-- AdminEntity 类中有个属性为:college 的,赋值为:value="" -->
<property name="college" value="湖北国土资源职业学院"></property>
<property name="tel" value="1533*******"></property>
<!-- 为集合赋值List -->
<property name="ar">
<list>
<value>上网</value>
<value>吃饭</value>
<value>睡觉</value>
</list>
</property>
</bean>
<!-- 解释:==UserEntity userEntity = new UserEntity(); -->
<bean id="userEntity" class="com.svse.entity.UserEntity">
<property name="id" value="1"></property>
<property name="name">
<value>小王八蛋</value>
</property>
<!-- 属性名:admin,为其依赖注入 adminEntity== admin = new AdminEntity(); 并进行赋值 -->
<property name="admin" ref="adminEntity"></property>
</bean>
<!--
2、bean 中的属性介绍: abstract="true" 设置该bean对应的类为抽象类,不能直接进行使用
parent="id标识,继承的父类"
-->
<!--
===========================定义UserEntity 为抽象类==========================
-->
<bean id="userEntity1" class="com.svse.entity.UserEntity"
abstract="true">
<property name="id" value="123"></property>
<property name="name" value="谢逊"></property>
</bean>
<!--
通过parent这个属性集成:抽象类(根据ID标识找到需要继承的类),继承了对应类中的属性,也可以随时的根据自己的需要更改相关的属性
不需要更改的可以直接的进行引用;
-->
<bean id="xx" parent="userEntity1">
<property name="id" value="456"></property>
</bean>
<!--============================================================== -->
<!-- 3、bean中属性:autowire的用法 ,以及null值的设置-->
<!--
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-->
<bean id="admin" class="com.svse.entity.AdminEntity">
<!-- 将college属性值设为:"" -->
<property name="college">
<value></value>
</property>
<!-- 将tel的属性值设为:null -->
<property name="tel">
<null />
</property>
<property name="ar">
<list>
<value>八嘎</value>
<value>九噶</value>
</list>
</property>
</bean>
<bean id="userEntity3" class="com.svse.entity.UserEntity"
autowire="byName">
<property name="id" value="3"></property>
<property name="name">
<value>小红</value>
</property>
<!--
该实体下包含一个对象名称为:admin的对象,设置了autowire 然后自动的寻找到id名为admin的bean,实现自动匹配
-->
</bean>
<!--
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-->
<!-- 4、bean的name属性、depends-on="标识ID名" -->
<!--
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-->
<!--
bean中的ID 是唯一标识,而name 能多名称标识,相当于给同一个bean起多个名称,可以根据任意的一个别名获取到当前
的bean,命名方式如:name="/user,yy,xx,aa" , id="xx"
代码中可以根据任意你喜欢的名称(user,yy,xx,aa)获取,而 id只能根据xx获取; depends-on: 通过ID
指定依赖的对象,被依赖者执行之后才会执行本身;例如:处理数据之前必须先链接数据库;
-->
<!--
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-->
<!-- 5、通过xml文件给实体中构造方法赋值 传递参数值,以及传递实体对象 -->
<!--
=============================================================================================
-->
<bean id="adminEntity1" class="com.svse.entity.AdminEntity">
<property name="college" value="工业大学"></property>
<property name="tel">
<value></value>
</property>
<property name="ar">
<null />
</property>
</bean>
<bean id="userEntity4" class="com.svse.entity.UserEntity">
<constructor-arg index="0" value="1"></constructor-arg>
<constructor-arg index="1" value="大奖人"></constructor-arg>
<constructor-arg index="2" ref="adminEntity1"></constructor-arg>
</bean>
<!--
=============================================================================================
-->
<!-- 6、BeanFactory 的作用域 ,scope属性-->
<!--
scope="singleton" : 单例模式 ,查询一次后数据存入内存,然后常驻内存,再次查询的时候仍然是内存中的数据,
数据没有和数据库同步; scope="prototype" :代理模式 , 只负责封装数据,能保证页面数据和数据库的同步
-->
<bean id="adminEntity6" class="com.svse.entity.AdminEntity"></bean>
<bean id="userEntity6" class="com.svse.entity.UserEntity" scope="singleton"></bean>
<!-- 單例模式下:
UserEntity user1 = app.getBean("userEntity6",UserEntity.class);
UserEntity user2= app.getBean("userEntity6",UserEntity.class);
此時:user1==user2 : true
-->
<bean id="userEntity7" class="com.svse.entity.UserEntity" scope="prototype"></bean>
<!-- 代理模式下:
UserEntity user1 = app.getBean("userEntity7",UserEntity.class);
UserEntity user2= app.getBean("userEntity7",UserEntity.class);
此時:user1!=user2 : false
-->
<!-- ==================================================================================== -->
</beans>
推荐阅读
-
spring定义和装配bean详解
-
Spring的并发问题——有状态Bean和无状态Bean
-
荐 Spring-boot-study02-spring.xml配置文件注入组件和@Bean注解注入组件差别
-
bean装载到Spring应用上下文的生命周期
-
struts和spring的上下文结合的地方(上下文结合)
-
Spring5 - Bean的初始化和销毁的4种方式
-
[ SSH框架 ] Spring框架学习之二(Bean的管理和AOP思想)
-
spring bean 的生命周期和配置源信息
-
Spring中bean的初始化和销毁几种实现方式详解
-
Java Bean 和 Spring Bean 的几种定义