当前位置: 首页 > news >正文

华容网站定制北京seo公司助力网络营销

华容网站定制,北京seo公司助力网络营销,网站品牌推广,万网备案域名购买学习目标 上一篇文章我们介绍了什么是Spring,以及Spring的一些核心概念,并且快速快发一个Spring项目,实现IOC和DI,今天具体来讲解IOC 能够说出IOC的基础配置和Bean作用域 了解Bean的生命周期 能够说出Bean的实例化方式 一、Bean的基础配置 …

学习目标

上一篇文章我们介绍了什么是Spring,以及Spring的一些核心概念,并且快速快发一个Spring项目,实现IOC和DI,今天具体来讲解IOC

能够说出IOC的基础配置和Bean作用域

了解Bean的生命周期

能够说出Bean的实例化方式

一、Bean的基础配置

问题导入

问题1:在<bean>标签上如何配置别名?

问题2:Bean的默认作用范围是什么?如何修改?

1 Bean基础配置【重点】

配置说明

2 Bean别名配置

配置说明

注意事项:

获取bean无论是通过id还是name获取,如果无法获取到,将抛出异常NoSuchBeanDefinitionException
NoSuchBeanDefinitionException: No bean named 'studentDaoImpl' available

代码演示

【第0步】创建项目名称为10_2_IOC_Bean的maven项目

【第一步】导入Spring坐标

  <dependencies><!--导入spring的坐标spring-context,对应版本是5.2.10.RELEASE--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.15</version></dependency><!-- 导入junit的测试包 --><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>5.8.2</version><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.28</version></dependency></dependencies>

【第二步】导入Student实体类

@Data
@ToString
@AllArgsConstructor
public class Student {private String name;private String address;private Integer age;private Integer status;
}

【第三步】定义Spring管理的类(接口)

  • StudentDao接口和StudentDaoImpl实现类

package com.zbbmeta.dao;public interface StudentDao {/*** 添加学生*/void save();
}
package com.zbbmeta.dao.impl;import com.zbbmeta.dao.StudentDao;public class StudentDaoImpl implements StudentDao {@Overridepublic void save() {System.out.println("DAO: 添加学生信息到数据库...");}
}
  • StudentService接口和StudentServiceImpl实现类

package com.zbbmeta.service;public interface StudentService {/*** 添加学生*/void save();
}
package com.zbbmeta.service.impl;import com.zbbmeta.dao.StudentDao;
import com.zbbmeta.dao.impl.StudentDaoImpl;
import com.zbbmeta.service.StudentService;public class StudentServiceImpl implements StudentService {//创建成员对象private StudentDao studentDao = new StudentDaoImpl();@Overridepublic void save() {}
}

【第四步】创建Spring配置文件在resources目录下,配置对应类作为Spring管理的bean对象

  • 定义application.xml配置文件并配置StudentDaoImpl

<?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"><!--目标:熟悉bean标签详细的属性应用--><!--name属性:可以设置多个别名,别名之间使用逗号,空格,分号等分隔--><bean name="studentDao2,abc studentDao3" class="com.zbbmeta.dao.impl.StudentDaoImpl" id="studentDao"></bean></beans>

注意事项:bean定义时id属性和name中名称不能有重复的在同一个上下文中(IOC容器中)不能重复

【第四步】根据容器别名获取Bean对象

package com.zbbmeta;import com.zbbmeta.dao.StudentDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class NameApplication {public static void main(String[] args) {/*** 从IOC容器里面根据别名获取对象执行*///1.根据配置文件application.xml创建IOC容器ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");//2.从IOC容器里面获取id="abc"对象StudentDao studentDao = (StudentDao) ac.getBean("abc");//3.执行对象方法studentDao.save();//4.关闭容器ac.close();}
}

打印结果

3 Bean作用范围配置【重点】

配置说明

扩展: scope的取值不仅仅只有singleton和prototype,还有request、session、application、 websocket ,表示创建出的对象放置在web容器(tomcat)对应的位置。比如:request表示保存到request域中。

代码演示

在application.xml中配置prototype格式

  • 定义application.xml配置文件并配置StudentDaoImpl

<?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"><!--目标:熟悉bean标签详细的属性应用--><!--scope属性:定义bean的作用范围,一共有5个singleton: 设置单例创建对象(推荐,也是默认值),好处:节省资源prototype: 设置多例创建对象,每次从IOC容器获取的时候都会创建对象,获取多次创建多次。request: 在web开发环境中,IOC容器会将对象放到request请求域中,对象存活的范围在一次请求内。请求开始创建对象,请求结束销毁对象session: 在web开发环境中,IOC容器会将对象放到session会话域中,对象存活的范围在一次会话内。会话开始开始创建对象,会话销毁对象销毁。global-session: 是多台服务器共享同一个会话存储的数据。--><bean class="com.zbbmeta.dao.impl.StudentDaoImpl" id="studentDao4" scope="prototype"></bean></beans>

根据容器别名获取Bean对象

package com.zbbmeta;import com.zbbmeta.dao.StudentDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class ScopeApplication {public static void main(String[] args) {/*** Bean的作用域范围演示*///1.根据配置文件application.xml创建IOC容器ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");//2.从IOC容器里面获取id="studentService"对象System.out.println("=========singleton(单例)模式=========");StudentDao studentDao = (StudentDao) ac.getBean("studentDao");StudentDao studentDao1 = (StudentDao) ac.getBean("studentDao");System.out.println("studentDao = " + studentDao);System.out.println("studentDao1 = " + studentDao1);System.out.println("=========prototype模式=========");StudentDao studentDao2 = (StudentDao) ac.getBean("studentDao4");StudentDao studentDao3 = (StudentDao) ac.getBean("studentDao4");System.out.println("studentDao2 = " + studentDao2);System.out.println("studentDao3 = " + studentDao3);//4.关闭容器ac.close();}
}

打印结果

注意:在我们的实际开发当中,绝大部分的Bean是单例的,也就是说绝大部分Bean不需要配置scope属性

二、Bean的实例化

思考:Bean的实例化方式有几种?

2 实例化Bean的三种方式

2.1 构造方法方式【重点】

  • BookDaoImpl实现类

public class StudentDaoImpl implements StudentDao {public StudentDaoImpl() {System.out.println("Student dao constructor is running ....");}@Overridepublic void save() {System.out.println("DAO: 添加学生信息到数据库...");}
}
  • application.xml配置

    <!--目标:讲解bean创建方式--><!--创建StudentDaoImpl对象方式1:默认调用类的无参构造函数创建--><bean id="studentDao" class="com.zbbmeta.dao.impl.StudentDaoImpl"/>
  • AppForInstanceBook测试类

public class OneApplication {public static void main(String[] args) {/*** 无参构造方式创建Bean*///1.根据配置文件application.xml创建IOC容器ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");//2.从IOC容器里面获取id="studentService"对象StudentDao studentDao = (StudentDao) ac.getBean("studentDao");//3.执行对象方法studentDao.save();//4.关闭容器ac.close();}
}
  • 运行结果

注意:无参构造方法如果不存在,将抛出异常BeanCreationException

2.2 静态工厂方式

  • StudentDaoFactory工厂类

package com.zbbmeta.factory;import com.zbbmeta.dao.StudentDao;
import com.zbbmeta.dao.impl.StudentDaoImpl;public class StudentDaoFactory {
//    静态工厂创建对象public static StudentDao getStudentDao(){System.out.println("Student static factory setup....");return new StudentDaoImpl();}
}
  • applicationContext.xml配置

<?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"><!--目标:讲解bean创建方式--><!--创建StudentDaoImpl对象方式1:默认调用类的无参构造函数创建-->
<!--    <bean id="studentDao" class="com.zbbmeta.dao.impl.StudentDaoImpl"/>--><!--创建StudentDaoImpl对象方式2:调用静态工厂方法创建对象加入IOC容器class="com.zbbmeta.factory.StudentDaoFactory" 设置工厂类全名factory-method="getStudentDao" 调用工厂的静态方法
--><bean class="com.zbbmeta.factory.StudentDaoFactory" factory-method="getStudentDao" id="studentDao2"></bean></beans>

注意:测试前最好把之前使用Bean标签创建的对象进行注释

  • TwoApplication测试类

public class TwoApplication {public static void main(String[] args) {/*** 无参构造方式创建Bean*///1.根据配置文件application.xml创建IOC容器ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");//2.从IOC容器里面获取id="studentService"对象StudentDao studentDao = (StudentDao) ac.getBean("studentDao2");//3.执行对象方法studentDao.save();//4.关闭容器ac.close();}
}
  • 运行结果

2.3 实例工厂方式

  • UserDao接口和UserDaoImpl实现类

    //利用实例方法创建StudentDao对象public StudentDao getStudentDao2(){System.out.println("调用了实例工厂方法");return new StudentDaoImpl();}
  • StudentDaoFactory工厂类添加方法

//实例工厂创建对象
public class UserDaoFactory {public UserDao getUserDao(){return new UserDaoImpl();}
}
  • applicationContext.xml配置

<?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"><!--目标:讲解bean创建方式--><!--创建StudentDaoImpl对象方式1:默认调用类的无参构造函数创建-->
<!--    <bean id="studentDao" class="com.zbbmeta.dao.impl.StudentDaoImpl"/>--><!--创建StudentDaoImpl对象方式2:调用静态工厂方法创建对象加入IOC容器class="com.zbbmeta.factory.StudentDaoFactory" 设置工厂类全名factory-method="getStudentDao" 调用工厂的静态方法
-->
<!--    <bean class="com.zbbmeta.factory.StudentDaoFactory" factory-method="getStudentDao" id="studentDao2"></bean>--><!--创建BookDaoImpl对象方式3:调用实例工厂方法创建对象加入IOC容器class="com.itheima.factory.BookDaoFactory" 设置工厂类全名factory-method="getBookDao" 调用工厂的静态方法
--><!--第一步:创建工厂StudentDaoFactory对象--><bean class="com.zbbmeta.factory.StudentDaoFactory" id="studentDaoFactory"></bean><!--第一步:调用工厂对象的getStudentDao2()实例方法创建StudentDaoImpl对象加入IOC容器factory-bean="studentDaoFactory" 获取IOC容器中指定id值的对象factory-method="getStudentDao2" 如果配置了factory-bean,那么这里设置的就是实例方法名--><bean factory-bean="studentDaoFactory" factory-method="getStudentDao2" id="studentDao3"></bean></beans>
  • ThreeApplication测试类

package com.zbbmeta;import com.zbbmeta.dao.StudentDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class ThreeApplication {public static void main(String[] args) {/*** 无参构造方式创建Bean*///1.根据配置文件application.xml创建IOC容器ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");//2.从IOC容器里面获取id="studentService"对象StudentDao studentDao = (StudentDao) ac.getBean("studentDao3");//3.执行对象方法studentDao.save();//4.关闭容器ac.close();}
}
  • 运行结果

三、Bean的生命周期【了解】

问题导入

问题1:多例的Bean能够配置并执行销毁的方法?

问题2:如何做才执行Bean销毁的方法?

1 生命周期相关概念介绍

  • 生命周期:从创建到消亡的完整过程

  • bean生命周期:bean从创建到销毁的整体过程

  • bean生命周期控制:在bean创建后到销毁前做一些事情

1.1生命周期过程

  • 初始化容器
    • 创建对象(内存分配)

    • 执行构造方法

    • 执行属性注入(set操作)

    • 执行bean初始化方法

  • 使用bean
    • 执行业务操作

  • 关闭/销毁容器
    • 执行bean销毁方法

2 代码演示

2.1 Bean生命周期控制

【第0步】创建项目名称为10_4_IOC_BeanLifeCycle的maven项目

【第一步】导入Spring坐标

  <dependencies><!--导入spring的坐标spring-context,对应版本是5.2.10.RELEASE--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.15</version></dependency><!-- 导入junit的测试包 --><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>5.8.2</version><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.28</version></dependency></dependencies>

【第二步】导入Student实体类

@Data
@ToString
@AllArgsConstructor
public class Student {private String name;private String address;private Integer age;private Integer status;
}

【第三步】定义Spring管理的类(接口)

  • StudentDao接口和StudentDaoImpl实现类

package com.zbbmeta.dao;public interface StudentDao {/*** 添加学生*/void save();
}
package com.zbbmeta.dao.impl;import com.zbbmeta.dao.StudentDao;public class StudentDaoImpl implements StudentDao {public StudentDaoImpl(){System.out.println("Student Dao 的无参构造");}@Overridepublic void save() {System.out.println("DAO: 添加学生信息到数据库...");}public void init(){System.out.println("Student Dao 的初始化方法");}public void destroy(){System.out.println("Student Dao 的销毁方法");}
}

【第四步】创建Spring配置文件在resources目录下,配置对应类作为Spring管理的bean对象

  • 定义application.xml配置文件并配置StudentDaoImpl

<?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"><!--目标:创建StudentDaoImpl对象:设置生命周期方法init-method="init" 在对象创建后立即调用初始化方法destroy-method="destroy":在容器执行销毁前立即调用销毁的方法注意:只有单例对象才会运行销毁生命周期方法
--><bean class="com.zbbmeta.dao.impl.StudentDaoImpl" id="studentDao" init-method="init" destroy-method="destroy"></bean>
</beans>

【第四步】根据容器别名获取Bean对象

package com.zbbmeta;import com.zbbmeta.dao.StudentDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class LifeCycleApplication {public static void main(String[] args) {//1.根据配置文件application.xml创建IOC容器ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");//2.从IOC容器里面获取id="studentService"对象StudentDao studentDao = (StudentDao) ac.getBean("studentDao");//3.执行对象方法studentDao.save();//4.关闭容器ac.close();}
}

打印结果

3 Bean销毁时机

  • 容器关闭前触发bean的销毁

  • 关闭容器方式:
    • 手工关闭容器 调用容器的close()操作

    • 注册关闭钩子(类似于注册一个事件),在虚拟机退出前先关闭容器再退出虚拟机 调用容器的registerShutdownHook()操作

public class LifeCycleApplication {public static void main(String[] args) {//1.根据配置文件application.xml创建IOC容器ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");//2.从IOC容器里面获取id="studentService"对象StudentDao studentDao = (StudentDao) ac.getBean("studentDao");//3.执行对象方法studentDao.save();//4.关闭容器
//            ac.close();//注册关闭钩子函数,在虚拟机退出之前回调此函数,关闭容器ac.registerShutdownHook();}
}

文章转载自:
http://wanjiabezant.kjrp.cn
http://wanjiagigsman.kjrp.cn
http://wanjiasunbird.kjrp.cn
http://wanjiawhiskers.kjrp.cn
http://wanjiabetrayer.kjrp.cn
http://wanjiaunmerciful.kjrp.cn
http://wanjialifeboat.kjrp.cn
http://wanjiacassiterite.kjrp.cn
http://wanjiakoel.kjrp.cn
http://wanjiamicromechanism.kjrp.cn
http://wanjiaunrest.kjrp.cn
http://wanjiaindian.kjrp.cn
http://wanjiacofacter.kjrp.cn
http://wanjiacomate.kjrp.cn
http://wanjiaclamworm.kjrp.cn
http://wanjiaophiuran.kjrp.cn
http://wanjiawingover.kjrp.cn
http://wanjiacered.kjrp.cn
http://wanjiabillingual.kjrp.cn
http://wanjiaascetically.kjrp.cn
http://wanjiaeighteenmo.kjrp.cn
http://wanjiaprecautious.kjrp.cn
http://wanjiaoctaploid.kjrp.cn
http://wanjiaalt.kjrp.cn
http://wanjiadarter.kjrp.cn
http://wanjiazahidan.kjrp.cn
http://wanjiasep.kjrp.cn
http://wanjiaseniti.kjrp.cn
http://wanjiaclown.kjrp.cn
http://wanjiacountrypeople.kjrp.cn
http://wanjiaanticharm.kjrp.cn
http://wanjiagolfer.kjrp.cn
http://wanjiamemorial.kjrp.cn
http://wanjiacarcinomatous.kjrp.cn
http://wanjianonattendance.kjrp.cn
http://wanjiasemifinalist.kjrp.cn
http://wanjiaconglutinate.kjrp.cn
http://wanjiahematin.kjrp.cn
http://wanjiaunsplinterable.kjrp.cn
http://wanjiaonlay.kjrp.cn
http://wanjiaprevision.kjrp.cn
http://wanjiadermapteran.kjrp.cn
http://wanjiakumquat.kjrp.cn
http://wanjiashaef.kjrp.cn
http://wanjiaintellectually.kjrp.cn
http://wanjiadft.kjrp.cn
http://wanjiamycelia.kjrp.cn
http://wanjialegislatrix.kjrp.cn
http://wanjiamisdemeanant.kjrp.cn
http://wanjiaexpiscate.kjrp.cn
http://wanjiadedicator.kjrp.cn
http://wanjiaeinar.kjrp.cn
http://wanjiacarven.kjrp.cn
http://wanjiaecstatically.kjrp.cn
http://wanjiadocumentarian.kjrp.cn
http://wanjialibau.kjrp.cn
http://wanjialoadstone.kjrp.cn
http://wanjiaimplementation.kjrp.cn
http://wanjiaephyra.kjrp.cn
http://wanjiabretagne.kjrp.cn
http://wanjiamenace.kjrp.cn
http://wanjiasidetrack.kjrp.cn
http://wanjiakris.kjrp.cn
http://wanjiacampaign.kjrp.cn
http://wanjialatticed.kjrp.cn
http://wanjiainebriate.kjrp.cn
http://wanjiaeighthly.kjrp.cn
http://wanjianecromimesis.kjrp.cn
http://wanjiaairpark.kjrp.cn
http://wanjiablanch.kjrp.cn
http://wanjiatheogonist.kjrp.cn
http://wanjiaautograph.kjrp.cn
http://wanjiacomponential.kjrp.cn
http://wanjiaincasement.kjrp.cn
http://wanjiakinda.kjrp.cn
http://wanjiainterlocutory.kjrp.cn
http://wanjiaweariness.kjrp.cn
http://wanjiaquetta.kjrp.cn
http://wanjiaverbose.kjrp.cn
http://wanjiazillion.kjrp.cn
http://www.15wanjia.com/news/115835.html

相关文章:

  • wordpress爬取豆瓣电影简介河源网站seo
  • 携程网站建设的基本特点网络营销的主要特点有哪些
  • 做代妈的网站网络促销的方法有哪些
  • 唐山石家庄做网站哪家好餐饮营销方案100例
  • 济南网站建设工资网站建设制作过程
  • 辽宁沈阳今天消息seo站内优化公司
  • 建设局网站简介seo攻略
  • jsp做电影网站软文广告例子
  • 模拟登录wordpress廊坊优化外包
  • dedecms做网站最新病毒感染
  • 凡科网站做网站可靠吗北京网站优化推广方案
  • 福田做商城网站建设哪家便宜技能培训有哪些科目
  • 网站制作怎么做让点击高免费的外链网站
  • 中邮通建设咨询有限公司官方网站公司建网站需要多少钱
  • 学校 网站源码seo推广骗局
  • 蒙古网站做奶食百度人工优化
  • 房产网站方案网站技术制作
  • 做自己的网站如何赚钱的宁德网站建设制作
  • b2b电子商务网站的类型不包括最近发生的重大新闻事件
  • web程序设计asp.net网站开发课后答案google chrome 网络浏览器
  • 怎么购买国外的域名seo外链怎么做能看到效果
  • 亲子网站源码种子搜索神器下载
  • 聊城网站建设潍坊揭阳seo推广公司
  • 网站做镜像检查漏洞虎扑体育网体育
  • 做软件的网站建设百度客服人工
  • 建设网站的企业是什么百度公司电话热线电话
  • 微信公众号红包网站开发qq营销软件
  • 网站开发立项做游戏推广一个月能拿多少钱
  • 大庆 网站建设免费入驻的电商平台
  • 拉趣网站是谁做的重庆seo排名外包