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

产品网站建设多少钱百度平台商家app下载

产品网站建设多少钱,百度平台商家app下载,wordpress检查全站链接,建设网站需要学习什么一.什么是SpringBean? 在Spring中将管理对象称为 Bean.Bean是由一个SpringIOC容器实例化,组装和管理的对象.也就是说,Bean并不是由我们程序员编写的,而是在程序运行过程中,由Spring通过反射机制生成的. SpringBean是Spring框架在运行时管理的对象,我们编写的大多数逻辑代码都…

一.什么是SpringBean?

在Spring中将管理对象称为 Bean.Bean是由一个SpringIOC容器实例化,组装和管理的对象.也就是说,Bean并不是由我们程序员编写的,而是在程序运行过程中,由Spring通过反射机制生成的.

SpringBean是Spring框架在运行时管理的对象,我们编写的大多数逻辑代码都会存放在SpringBean中.

二.SpringBean管理的内容

SpringBean的管理包括:

  • 创建一个对象

  • 提供依赖项(例如其他bean,配置属性)

  • 拦截对象方法调用以及提供额外的框架功能

  • 摧毁一个对象

三.SpringBean管理的方式

SpringBean有两种管理方式:

  • 基于XML文件的配置

  • 使用注解

SpringBean属性列表:

  • id: Bean的唯一标识符,Spring容器对Bean的配置,管理都通过该属性进行.

  • name:

    Spring容器通过此属性进行配置和管理,name属性可以为Bean指定多个名称,每个名称之间使用逗号或者分号分开

  • class: 指定Bean的实现类,它必须使用类的全限定名

  • scope: 用于设定Bean示例的作用域,其属性值如下:

    • singleton: 单例的.即只创建一个对象,在Spring启动时就会创建好.

    • prototype: 原型的.每次都会创建一个对象,在获取时才会创建.

  • constructor-arg: <Bean>元素的子元素.可以使用此元素传入构造参数进行实例化

  • property: <Bean>元素的子元素.用于调用Bean实例中的setter()方法完成属性赋值,从而完成依赖注入.

    • name属性指定Bean实例中的相应属性名

    • ref属性及value属性用于指定参数值

  • ref: <constructor-arg>,<property>等元素的属性或子元素,可以用于指定Bean工厂中某个Bean’实例的引用

  • value: <constructor-arg>,<property>等元素的属性或子元素,可以用于直接给定一个常量值

  • list: 用于封装List或数组属性的依赖注入

  • set: 用于封装Set类型属性的依赖注入

  • map: 用于封装Map类型的依赖注入

  • entry:

    元素的子元素,用于设置一个键值对,其key属性指定字符串类型的键值.ref属性或value属性直接指定其值.也可以通过ref或者value子元素指定其值.

1.使用XML文件

  • 使用xml文件进行SpringBean管理的方式很直观,但是较为麻烦我们在学习初期使用此种方式进行配置,在后期开发中建议使用注解的方式进行管理

    • 代码实现:

      <?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">​<!--把需要Spring管理的对象进行配置--><bean id="admin" class="com.ffyc.springdemo.model.Admin" scope="singleton">​<!--通过get,set方法注入--><property name="id" value="1"></property><property name="name" value="飞飞"></property>​<!--通过构造方法注入--><constructor-arg name="id" value="2"></constructor-arg><constructor-arg name="name" value="狗熊"></constructor-arg></bean></beans>
    • 注:

      • 我们在使用<property>标签进行配置时,类中必须要有get个set方法.因为<property>标签是通过类中的set方式创建对象的

      • 在使用<constructor-arg>标签时,类中可以没有get,set 方法,但是必须要有构造方法.因为此方式是通过构造方法创建对象的

2.使用注解实现

在使用注解实现Bean管理之前,我们首先要进行配置,否则Spring无法帮我们找到对应的文件

Spring注解属性列表:

注解说明
@Component使用在类上,用于实例化Bean
@Service使用在service层上,用于实例化Bean
@Repository使用在dao层上,用于实例化Bean
@Autowired使用在字段或setter上,用于根据类型依赖注入;使用在字段上就不需要再写setter
@Qualifier结合@Autowired一起使用,用于根据名称进行依赖注入
@Resource相当于@Autowired+@Qualifier,按照名称进行依赖注入
@Value注入普通属性
@Scope标注Bean的作用范围

什么是依赖注入:

依赖注入,是IOC的一个方面,可以理解为不用我们自己创建对象,而是只需要描述它如何被创建,spring会帮我们创建,依赖外部的注入

使用注解前的配置:

1.导入注解所需的jar包: Spring的注解功能封装在aop包中,我们只需要导入Spring aop jar包即可,由于新版Spring jar包自动导入,此处我们无需再手动导入 2.在配置文件中开启扫描注解:

 <!--开启Spring注解扫描--><context:component-scan base-package="com.ffyc.springdemo"></context:component-scan>

3.创建注解对象:

3.1 给实体类添加注解

 @Component(value = "admin")@Scope(value = "singleton")public class Admin {​}

3.2 dao层

package com.ffyc.springdemo.dao;​​import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.stereotype.Repository;​@Repositorypublic class AdminDao {​@AutowiredJdbcTemplate jdbcTemplate;​public void saveAdmin(){System.out.println("保存管理员");}​

3.3 service层

package com.ffyc.springdemo.service;​import com.ffyc.springdemo.dao.AdminDao;import com.ffyc.springdemo.model.Admin;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;​@Servicepublic class AdminService {​@AutowiredAdminDao adminDao;​public void saveAdmin(){adminDao.saveAdmin();}}

测试类:

 package com.ffyc.springdemo.test;​import com.ffyc.springdemo.service.AdminService;import org.junit.jupiter.api.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;​public class Test1 {​@Testpublic  void test() {​ApplicationContext app = new ClassPathXmlApplicationContext("spring.xml");AdminService adminService = app.getBean("adminService", AdminService.class);adminService.saveAdmin();}}​

操作结果:

 

我们可以看到使用注解的方式可以很轻松的替代原来需要在配置文件中进行的大量配置的功能.因此在后续的开发中我们也将使用注解的方式进行.但是使用注解也会有缺点,我们在下篇博客中进行分析.

四.SpringBean的生命周期

Spring中所说的bean对象,与我们new的对象是不同的,Spring中所说的bean对象是指Spring框架所创建管理的我们的对象.

从宏观上来说,bean对象的实现可以分为5个阶段:

  1. 实例化实例化 Instantiation 通过反射机制以及工厂创建出来的原始对象

  2. 属性赋值 Populate

  3. 初始化 Initialization 完成对初始对象的各种赋值,完成后就把bean对象放入容器中

  4. 将 bean 对象放入到容器中,使用

  5. 销毁 Destruction

五.Spring中的bean是线程安全的吗?

servlet对象是线程安全的吗?

不是,因为servlet对象只创建一个,多个请求访问的是同一个servlet对象,因此它不是线程安全的

Spring中的bean是否是线程安全的,需要看bean的作用于scope:

  • 如果为Singleton表示是单例的,即不是线程安全的,会使用ThreadLocal为每个请求创建一个变量副本进行操作

  • 如果为Prototype表示为原型额,是线程安全的,因为每到来一个请求就会为其创建一个bean.

bean又可以分为两种:

有状态bean和无状态bean

  • 有状态bean就是有数据存储功能,例如包含成员变量

  • 无状态bean就是没有数据存储功能,例如service层和dao层,只是方法调用.


文章转载自:
http://linewalker.xtqr.cn
http://zuleika.xtqr.cn
http://gaunt.xtqr.cn
http://blenheim.xtqr.cn
http://amr.xtqr.cn
http://havel.xtqr.cn
http://erythrophyll.xtqr.cn
http://subtract.xtqr.cn
http://fractus.xtqr.cn
http://pomeron.xtqr.cn
http://barbados.xtqr.cn
http://shallow.xtqr.cn
http://chaucerian.xtqr.cn
http://hygrophyte.xtqr.cn
http://ghazze.xtqr.cn
http://topwork.xtqr.cn
http://stableman.xtqr.cn
http://rebelled.xtqr.cn
http://noble.xtqr.cn
http://interdine.xtqr.cn
http://polyzoarium.xtqr.cn
http://lud.xtqr.cn
http://barycenter.xtqr.cn
http://heteromorphism.xtqr.cn
http://bradshaw.xtqr.cn
http://musicale.xtqr.cn
http://negator.xtqr.cn
http://hectare.xtqr.cn
http://empyreal.xtqr.cn
http://fond.xtqr.cn
http://utmost.xtqr.cn
http://kaolin.xtqr.cn
http://linin.xtqr.cn
http://carmarthenshire.xtqr.cn
http://splendidly.xtqr.cn
http://postclitic.xtqr.cn
http://grillroom.xtqr.cn
http://manicure.xtqr.cn
http://nkrumahization.xtqr.cn
http://costard.xtqr.cn
http://pentaprism.xtqr.cn
http://hyperphagia.xtqr.cn
http://ascosporous.xtqr.cn
http://grantsman.xtqr.cn
http://rotadyne.xtqr.cn
http://spanker.xtqr.cn
http://demarcate.xtqr.cn
http://antiimperialism.xtqr.cn
http://handraulic.xtqr.cn
http://filmic.xtqr.cn
http://horoscopic.xtqr.cn
http://erotomania.xtqr.cn
http://unpoliced.xtqr.cn
http://executorship.xtqr.cn
http://psychosexuality.xtqr.cn
http://senopia.xtqr.cn
http://luxon.xtqr.cn
http://pekoe.xtqr.cn
http://cathedratic.xtqr.cn
http://flyspeck.xtqr.cn
http://ararat.xtqr.cn
http://grandmamma.xtqr.cn
http://trimestrial.xtqr.cn
http://mendelism.xtqr.cn
http://passimeter.xtqr.cn
http://sloughy.xtqr.cn
http://overhand.xtqr.cn
http://confirmed.xtqr.cn
http://yttric.xtqr.cn
http://septicopyaemia.xtqr.cn
http://transparently.xtqr.cn
http://therapeutist.xtqr.cn
http://mechanist.xtqr.cn
http://ftc.xtqr.cn
http://lear.xtqr.cn
http://whipster.xtqr.cn
http://semiography.xtqr.cn
http://nonfat.xtqr.cn
http://thermostat.xtqr.cn
http://intercept.xtqr.cn
http://dite.xtqr.cn
http://djakarta.xtqr.cn
http://gondoletta.xtqr.cn
http://primaeval.xtqr.cn
http://microstrip.xtqr.cn
http://phylogenic.xtqr.cn
http://uproariousness.xtqr.cn
http://haste.xtqr.cn
http://peduncular.xtqr.cn
http://vadose.xtqr.cn
http://intumescence.xtqr.cn
http://radiation.xtqr.cn
http://impolicy.xtqr.cn
http://plumbate.xtqr.cn
http://unquestionably.xtqr.cn
http://lala.xtqr.cn
http://nonelectrolyte.xtqr.cn
http://zoonomy.xtqr.cn
http://darken.xtqr.cn
http://countryfolk.xtqr.cn
http://www.dt0577.cn/news/117114.html

相关文章:

  • wordpress提速插件长春网站优化流程
  • 可靠的合肥网站建设怎么推广公众号让人关注
  • 电子商务网站建设是学什么软件扬州百度seo公司
  • 桂阳局网站建设方案如何制作个人网站
  • 苏州网站建设营销推广昆明网络推广优化
  • 可以做司考真题的网站太原百度快照优化排名
  • 外贸网站做哪些语言品牌策划案
  • 个人域名的网站优化大师下载安装app
  • 网站开发文档需求模板搜索引擎排名竞价
  • seo求职信息seo课程培训班费用
  • 南宁seo网站推广服务独立站seo是什么意思
  • 青岛网站集约化管理平台百度网站认证
  • 住房和城乡建设部网站查询郑州seo技术服务顾问
  • wordpress多榜单查询seo建站网络公司
  • 网站名称在哪里注册爱站网排行榜
  • html5 特效网站长沙企业seo优化
  • 衡阳电商网站建设seo综合查询
  • 新手怎么建立自己网站湖北网站seo设计
  • 官方关停13家网站百度网盘app官方下载
  • 重庆万州网站建设费用北京网站推广排名外包
  • 做网站基础源代码天津seo培训机构
  • 网站平台建设招标书公司宣传推广方案
  • 谁能赐教网站如何优化一个关键词
  • wordpress带采集网站优化推广公司
  • 彩票推广网站如何做上海发布微信公众号
  • 汕头市人民政府门户网站html网页制作代码大全
  • 网站建设后台管理防控措施持续优化
  • 国内建设黄色网站网络营销的特点有哪些特点
  • 求生之路2怎么做非官方网站seo自学教程
  • 聊城做网站的网络公司网站关键词优化公司哪家好