欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 国际 > Spring IOC容器对Bean管理

Spring IOC容器对Bean管理

2024/11/30 2:19:17 来源:https://blog.csdn.net/m0_74249133/article/details/141004123  浏览:    关键词:Spring IOC容器对Bean管理
一.bean实例化

1.通过构造方法(默认)

2.通过工厂方法

3.通过静态工厂方法

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--===========================beans的实例化==============================--><!--===========================beans的构造方法==============================--><bean id="student" class="com.pojo.Student"></bean><!--===========================beans的工厂方法==============================--><bean id="student" class="com.pojo.Student" factory-bean="factory" factory-method="createstu"></bean><bean id="factory" class="com.Factory.BeansFactory"></bean><!--===========================beans的工厂方法==============================--><bean id="student" class="com.Factory.StaticBeansFactory" factory-method="createstu" ></bean></beans>
public class BeansFactory {public Student createstu(){System.out.println("===>student的普通工厂方法");return new Student();}
}
public class StaticBeansFactory {public static Student createstu(){System.out.println("===>student的静态工厂方法");return new Student();}
}
public class Student {public Student() {System.out.println("===>student构造方法");}
}
二.bean作用域

含义:spring对于创建javaBean实例的方式

语法:<bean scope="属性值"></bean>

属性值:

singleton=====>单例(默认)

prototype=====>多例

request=======>一个请求创建一个

session=======>一个会话创建一个

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--===========================beans的作用域================================--><bean id="student" class="com.Factory.StaticBeansFactory" factory-method="createstu" scope="prototype"></bean></beans>
public class test01 {public static void main(String[] args) {ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext.xml");Student student1 = (Student)applicationContext.getBean("student");System.out.println(student1);Student student2 = (Student)applicationContext.getBean("student");System.out.println(student2);System.out.println(student1==student2);}
}
三.bean生命周期
1.实例化
2.属性赋值(DI)
3.初始化

3.1接口         InitializingBean

3.2属性         init-method=“”

4.操作使用
5.销毁了

5.1接口         DisposableBean

5.2属性         destory-method=“”

<!--===========================beans的生命周期================================--><bean id="teacher" class="com.pojo.Teacher" init-method="doinit" destroy-method="dodel"><property name="name" value="张三"></property></bean>
public class Teacher implements InitializingBean, DisposableBean {public Teacher() {System.out.println("===>teacher的实例化");}public String name;public void setName(String name) {System.out.println("===>teacher的属性赋值");this.name = name;}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("===>teacher的初始化(接口)");}public void doinit(){System.out.println("===>teacher的初始化(属性)");}@Overridepublic void destroy() throws Exception {System.out.println("===>teacher的销毁(接口)");}public void dodel(){System.out.println("===>teacher的销毁(属性)");}
}
public class test02 {public static void main(String[] args) {ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext.xml");Teacher teacher = applicationContext.getBean(Teacher.class);System.out.println(teacher);applicationContext.close();}
}

输出结果:

===>teacher的实例化

===>teacher的属性赋值

===>teacher的初始化(接口)

===>teacher的初始化(属性)

com.pojo.Teacher@6536e911

===>teacher的销毁(接口)

===>teacher的销毁(属性)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com