首页 > 其他分享 >Spring入门案例(二)

Spring入门案例(二)

时间:2022-09-18 13:34:15浏览次数:86  
标签:www 调用 入门 Spring 案例 bean beans 创建 org

1、bean的作用域scope,  singleton:单例(默认)先创建 / prototype:非单例,用的时候在创建

<?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 https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="userService" scope="singleton" class="com.qzerp.service.impl.UserServiceImpl"/>
</beans>

2、bean的生命周期

先在实现类里面创建两个方法,分别是init()创建时调用   和   destroy()销毁时调用

<bean id="userService" init-method="init" destroy-method="destroy" class="com.qzerp.service.impl.UserServiceImpl"/>

3、bean的工厂创建,一般是用来调用第三方

<!--静态工厂创建bean-->

<bean id="userService" class="com.qzerp.service.impl.UserServiceFactory" factory-method="getService"/>

<!--实例工厂创建bean-->

<bean id="factoryBean"  class="com.qzerp.service.impl.UserServiceImpl"/>

<!--实例工厂创建bean,依赖工厂对象的bean-->

<bean id="userService"  factory-bean="factoryBean"  factory-method="getService"/>

 

标签:www,调用,入门,Spring,案例,bean,beans,创建,org
From: https://www.cnblogs.com/lili520/p/16704672.html

相关文章