大家好,欢迎来到IT知识分享网。
Spring框架案例
1.soring容器以及Bean的装配
凡是归spring管理的对象统一称为Bean
不能实例化的组件是不允许创建为Bean(例如:接口,抽象类)
实现类 是普通的类,因此可以创建实例纳入Spring容器中管理
创建容器工厂,在spring框架中存在多种不同的容器工厂, 每种容器工厂都有自身呢个的特别和功能,例如:当我们需要通过解析xml配置文件类初始化一个容器工厂时,可以使用ClassPathXmlApplicationContest这个容器工厂, 而这些工厂最终实现的都是ApplicationContest接口
第一步、在pom.xml文件添加Spring核心依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.23</version> </dependency>
第二步、创建XML文件,右键resources –> New –> XML Configuration File –> Spring Config 命名为beans
配置应用程序上下文
创建一个接口类
public interface UserService {
void say(); }
创建一个实现类,继承接口类
public class UserServiceImpl implements edu.nf.ch01.UserService {
@Override public void say() {
System.out.println("Hello,Word"); } }
在刚刚创建的beans.xml,装配bean对象
id表示在bean容器中是唯一标识
class是指bean的完整类名,spring会使用这个完整类名进行反射
创建实例进入容器中管理
<bean id="userService" class="edu.nf.ch01.impl.UserServiceImpl"/>
在Main函数中创建测试类
public static void Main(String[] args) {
/* 可以使用ClassPathXmlApplicationContest这个容器工厂解析xml配置文件 ApplicationContext接口是工厂最终实现 */ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); //从容器中根据Bean的id获取Bean的实例 UserService service = (UserService)context.getBean("userService"); service.say(); }
自定义工厂
在edu.nf.ch01目录下创建factory包,然后再创建一个自定义工厂类
public class PeopleServiceFactory {
/ * 简单的工厂,用来创建PeopleServiceImpl实例 * @return */ public UserService create() {
return new PeopleServiceImpl(); } }
在beans.xml文件中装配自定义工厂,spring会将我们编写的工厂类纳入容器管理
<bean id="peopleFactoryBean" class="edu.nf.ch01.factory.PeopleServiceFactory"/> <!-- factory-bean引用上面自定义工厂的id factory-method指定工厂中方法名 --> <bean id="peopleService" factory-bean="peopleFactoryBean" factory-method="create"/>
scope属性
默认值:singleton
- singleton(单例):容器会为每一个Bean创建唯一的一个实例,并放在容器中管理,直到容器销毁,Bean才会跟着销毁
<bean id="userService" class="edu.nf.ch02.service.UserService" scope="singleton"/>
- prototype(原型):容器一开始并不会创建实例,而是当调用了getBean方法时才会根据class创建一个新的实例,这个实例并不会纳入容器中,使用完之后直接丢弃,因此每次调用getBean方法时都会新建一个实例
<bean id="userService" class="edu.nf.ch02.service.UserService" scope="prototype"/>
- request请求作用域(需要集成在web环境中):与servlet中的请求作用域保持一致,bean会在一次请求响应后就销毁
<bean id="userService" class="edu.nf.ch02.service.UserService" scope="request"/>
- session会话作用域(需要集成在web环境中):与servlet中的会话作用域保持一致,当客户端关闭浏览器后,销毁了会话id,服务端后台就会自动销毁这个Bean
<bean id="userService" class="edu.nf.ch02.service.UserService" scope="session"/>
name属性
id属性表示Bean在容器中的唯一标识,是不可重复发,但同时还有另外一个name属性,用于bean容器中的别名,这个笔名是可以有多个(别名之间可以使用逗号或者空格隔开) ,所以在获取bean的时候可以根据id也可以根据别名来获取
注意:在指定name以后,可以不需要指定id,但是name的第一个名字就会自动作为id使用,其他的仍是别名
<bean class="edu.nf.ch03.service.OrderService" name="o1,o2,o3"/>
初始化方法
bean的初始化方法有两种方式(可以二选一):
- 实现InitializingBean接口,接口包含一个afterPropertiesSet方法
- 自定义初始化方法,并通过init-method属性来指定自定义的方法名即可。注意:如果两种初始化方法同时存在,自定义方法是最后被执行
销毁方法
bean的销毁方法也有两种实现方式:
- 实现DisposableBean接口,接口包含一个destroy方法
- 自定义销毁方法,并通过destroy-method属性指定方法名即可
重点:spring管理bean的生命周期是针对单例的bean,通过原型创建的bean不会纳入spring容器中,因此不会执行生命周期的方法
<bean id="userService"class="edu.nf.ch04.service.UserService"init-method="init"destroy-method="myDestroy"scope="singleton"/>
总结:bean的生命周期流程
-> 执行对象的构造方法
-> 后置处理器的postProcessBeforeInitialization方法
-> InitializingBean接口的afterPropertiesSet方法
-> 自定义的init-method
-> 后置处理器的postProcessAfterInitialization方法
-> DisposableBean接口的destroy方法
处理器的postProcessBeforeInitialization方法
-> InitializingBean接口的afterPropertiesSet方法
-> 自定义的init-method
-> 后置处理器的postProcessAfterInitialization方法
-> DisposableBean接口的destroy方法
-> 自定义的destroy-method方法
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/146301.html