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

深圳坪山区地图济南网站建设优化公司

深圳坪山区地图,济南网站建设优化公司,重庆渝北论坛,wordpress 隐私设置文章目录1、Bean的配置1.1、配置方式2、Bean的实例化2.1、构造器实例化2.2、静态工厂方式实例化2.3、实例工厂方式实例化3、Bean的作用域3.1、作用域的种类4、Bean的生命周期5、Bean的装配方式5.1、基于XML的装配5.2、基于Annotation的装配5.3、自动装配1、Bean的配置 1.1、配… 文章目录1、Bean的配置1.1、配置方式2、Bean的实例化2.1、构造器实例化2.2、静态工厂方式实例化2.3、实例工厂方式实例化3、Bean的作用域3.1、作用域的种类4、Bean的生命周期5、Bean的装配方式5.1、基于XML的装配5.2、基于Annotation的装配5.3、自动装配1、Bean的配置 1.1、配置方式 基于xml文件Properties文件 在实际开发中最常使用的是XMl格式的配置方式 2、Bean的实例化 实例化Bean的方式有三种 构造器实例化静态工厂实例化实例工厂实例化 2.1、构造器实例化 构造器实例化是指Spring容器通过Bean对应类中默认的无参构造方法来实例化Bean。 默认的无参构造方法来实例化Bean package com.nynu.qdy.instance.constructor; public class Bean1 { } ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdbean idbean1 classcom.nynu.qdy.instance.constructor.Bean1 //beans2.2、静态工厂方式实例化 使用静态工厂要求创建一个静态工厂的方法来创建Bean的实例其Bean配置中的class属性所指定的不再是Bean实例的实现类而是静态工厂类同时还需要使用factory-method属性来指定所创建的静态工厂方法。 建一个静态工厂的方法来创建Bean的实例 package com.nynu.qdy.instance.static_factory;public class MyBean2Factory {// 使用自己的工厂建立Bean2实例public static Bean2 createBean() {return new Bean2();} } ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd!-- 静态工厂方式实例化Beanclass指向静态工厂的全类名factory-method需要调用的静态工厂方法名--bean idbean2classcom.nynu.qdy.instance.static_factory.MyBean2Factoryfactory-methodcreateBean /bean/beans2.3、实例工厂方式实例化 此种方式的工厂类中不再使用静态方法创建Bean实例而是采用直接创建Bean实例的方式。同时在配置文件中需要实例化的Bean也不是通过class属性直接指向的实例化类而是通过factory-bean属性指向配置的实例化工厂然后使用factory-bean属性确定使用工厂中的哪个方法。 通过factory-bean属性指向配置的实例化工厂 package com.nynu.qdy.instance.factory;public class MyBean3Factory {public MyBean3Factory() {System.out.println(bean3工厂实例化);}// 创建Bean3实例的方法public Bean3 createBean() {return new Bean3();} }?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd!-- 配置工厂 --bean idmyBean3Factoryclasscom.nynu.qdy.instance.factory.MyBean3Factory/bean!-- 使用factory-bean属性指向配置的实例工厂 使用factory-method属性确定使用工厂中的哪个方法 --bean idbean3 factory-beanmyBean3Factoryfactory-methodcreateBean/bean/beans3、Bean的作用域 3.1、作用域的种类 通过Spring容器创建一个Bean的实例时不仅可以完成Bean的实例化还可以为Bean指定特定的作用域。bean的作用域默认都是单例的但是可以通过scope[作用域]来设置。 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd !-- scope:作用域--bean idscope classcom.nynu.qdy.scope.Scopescopesingleton/beanbean idscope1 classcom.nynu.qdy.scope.Scopescopeprototype/bean/beans4、Bean的生命周期 Spring IOC容器对bean的生命周期的管理过程是 通过构造器或者工厂方法创建Bean实例设置Bean的属性 调用Bean的初始化方法使用Bean关闭容器调用Bean的销毁方法 5、Bean的装配方式 Bean的装配方式 基于XML的装配、基于注解的装配(Annotation)、自动装配、 5.1、基于XML的装配 基于xml的装配方式 设值注入Setter Injection构造注入Constructor Injection 在Spring实例化Bean的过程中Spring首先会调用Bean的默认构造方法来实例化Bean对象 然后通过反射的方式调用setter方法来注入属性值。因此设值注入要求一一个 Bean必须满足以下两点要求。 Bean类必须提供一个默认的无参构造方法。Bean类必须为需要注入的属性提供对应的setter方法 使用设值注入时 在Spring配置文件中需要使用元素的子元素来为每个属性注入值;而使用构造注入时在配置文件里需要使用元素的子元素 来定义构造方法的参数可以使用其value 属性或子元素来设置该参数的值。 下面通过一个案例来演示基于XML方式的Bean的装配 1.编写JavaBean package com.nynu.qdy.assemble;import java.util.List;public class User {private String username;private Integer password;private ListString list;/* *由于使用构造注入需要有其有参和无参的构造方法。同时为了输出时能够看到结果还重写了其属性的toString方法 *//** 1.使用构造注入 1.1提供带所有参数的有参构造方法*/public User(String username, Integer password, ListString list) {super();this.username username;this.password password;this.list list;}/** 2.使用设值注入 2.1提供默认构造参数 2.2为所有属性提供setter方法*/public User() {super();}public void setUsername(String username) {this.username username;}public void setPassword(Integer password) {this.password password;}public void setList(ListString list) {this.list list;}Overridepublic String toString() {return User [username username , password password , list list ];} }2.编写配置文件 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd!-- 1.使用构造注入方式装配User实例constructor-arg:用于定义构造方法的参数index属性表示索引从0开始value属性用于设置注入的值list子元素用于User来中对应的list集合属性注入值--bean iduser1 classcom.nynu.qdy.assemble.Userconstructor-arg index0 valuetom/constructor-argconstructor-arg index1 value123456/constructor-argconstructor-arg index2listvalueconstructorvalue1/valuevalueconstructorvalue2/value/list/constructor-arg/bean!-- 2.使用设值注入方式装配User实例property元素用于调用Bean实例中的setter方法完成属性赋值从而完成依赖注入lsit子元素用于User来中对应的list集合属性注入值--bean iduser2 classcom.nynu.qdy.assemble.Userproperty nameusername value张三/propertyproperty namepassword value654321/property!-- 注入list集合 --property namelistlistvaluesetlistvalue1/valuevaluesetlistvalue2/value/list/property/bean /beans3.测试 package com.nynu.qdy.assemble;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class XmlBeanAssembleTest {SuppressWarnings(resource)public static void main(String[] args) {// 定义配置文件路径String xmlPath com/nynu/qdy/assemble/beans5.xml;// ApplicationContext在加载配置文件时对Bean进行实例化ApplicationContext applicationContext new ClassPathXmlApplicationContext(xmlPath);// 构造方式输出结果System.out.println(applicationContext.getBean(user1));// 设值方式输出结果System.out.println(applicationContext.getBean(user2));} }4.结果 User [usernametom, password123456, list[constructorvalue1, constructorvalue2]] User [username张三, password654321, list[setlistvalue1, setlistvalue2]]5.2、基于Annotation的装配 Spring中定义了一系列的注解常用的注解如下所示。 Component: 可以使用此注解描述Sping中的Ban.但它是个泛化的概念仅仅表示一个组件Bean并且可以作用在任何层次。使用时只需将该注解标注在相应类上即可 。Repository:用于将数据访问层Dao层的类表识为Spring中的Bean其功能与Component相同。Service通常作用在业务层( Semice层)用于将业务层的类标识为Spring中的Bean,其功能与Component相同。 Controller:通常作用在控制层(如Spring MVC的Controller).用于将控制层的类标识为Spring中的Bean,其功能与Component相同。Autowired:用于对Bean的属性变量、属性的stter方法及构造方法进行标注配合对应的注解处理器完成Been的自动配置工作。默认按照Bean的类型进行装配。Resource:其作用与Autowited-一样。 其区别在于Autowired默认按照Bean类型装配而Resource默认按照Bean实例名称进行装配。Resource 中有两个重要属性: name和type。Spring将name属性解析为Bean实例名称type属性解析为Bean实例类型。如果指定name属性则按实例名称进行装配;如果指定type属性则按Bean类型进行装配;如果都不指定则先按Bean实例名称装配如果不能匹配再按照Bean类型进行装配;如果都无法匹配则抛出NoSuchBeanDefintionException异常。Qulifier: 与Autowired注解配合使用会将默认的按Bean类型装配修改为按Bean 的实例名称装配Bean的实例名称由Qualifer注解的参数指定。 下面通一个案例来演示如何通过这些注解来装配Bean 1.UserDao 接口 package com.nynu.qdy.annotation;public interface UserDao {public void save(); }2.接口实现类 package com.nynu.qdy.annotation;import org.springframework.stereotype.Repository;/** Reposrtory:将UserDaoImpl类表示为Spring中的Bean* 相当于bean iduserDao classcom.nynu.qdy.annotation.UserDaoImpl/*/ Repository(userDao) public class UserDaolmpl implements UserDao {public void save() {System.out.println(userdao...save....);} } 3.UserService 接口 package com.nynu.qdy.annotation;public interface UserService {public void save(); }4.UserServiceImpl实现类 package com.nynu.qdy.annotation;import javax.annotation.Resource; import org.springframework.stereotype.Service;/** Reposrtory:将UserServiceImpl类表示为Spring中的Bean* 相当于bean idUserService * classcom.nynu.qdy.annotation.UserDaoServiceImpl/* Resource:相当于配置文件中property nameuserDao refuserDao/*/ Service(userService) public class UserServicelmpl implements UserService {Resource(name userDao) private UserDao userDao;public void save() {// 调用userDao中的save方法this.userDao.save();System.out.println(userservice....save....);} }5.控制器类 package com.nynu.qdy.annotation;import javax.annotation.Resource; import org.springframework.stereotype.Controller;/** Controller:相当于在配置文件中编写bean iduserController* classcom.nynu.qdy.annotation.UserController/* Resource:相当于配置文件中编写property nameuserService refuserService/*/ Controller(userController) public class UserController {Resource(name userService)private UserService userService;public void save() {this.userService.save();System.out.println(UserService...save....);}}6.配置文件beans.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context-4.3.xsd!-- 使用context命名空间在配置文件中开启相应的注解处理器 --context:annotation-config /!-- 分别定义3个Bean实例 --bean iduserDao classcom.nynu.qdy.annotation.UserDaolmpl /bean iduserServiceclasscom.nynu.qdy.annotation.UserServicelmpl /bean iduserControllerclasscom.nynu.qdy.annotation.UserController /!-- 使用context命名空间通知Spring扫描指定包下的所有Bean类进行注解解析 --context:component-scanbase-packagecom.nynu.qdy.annotation / /beans7.测试类 package com.nynu.qdy.annotation;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class AnnotationAssembleTest {SuppressWarnings(resource)public static void main(String[] args) {// 定义配置文件路径String xmlPath com/nynu/qdy/annotation/beans6.xml;// 加载配置文件ApplicationContext applicationContext new ClassPathXmlApplicationContext(xmlPath);// 获取UserControllerUserController userController (UserController) applicationContext.getBean(userController);// 调用UserController中save()方法userController.save();}}8.结果 userdao...save.... userController....save.... UserService...save....9.提示 上述案例中如果使用Autowire注解替换Resource注解也可以达到同样的效果。 5.3、自动装配 Spring可以通过设置aotowrie的属性来自动装配Bean所谓自动装配就是将一个Bean自动地装配到其他Bean的Property中。 名称说明byName根据 Property 的 name 自动装配如果一个 Bean 的 name 和另一个 Bean 中的 Property 的 name 相同则自动装配这个 Bean 到 Property 中。byType根据 Property 的数据类型Type自动装配如果一个 Bean 的数据类型兼容另一个 Bean 中 Property 的数据类型则自动装配。constructor根据构造方法的参数的数据类型进行 byType 模式的自动装配。autodetect如果发现默认的构造方法则用 constructor 模式否则用 byType 模式。no默认情况下不使用自动装配Bean 依赖必须通过 ref 元素定义。 下面通过修改案例来演示使用自动装配 1.修改UserServiceImpl文件和UserController文件分别在文件中增加类属性的setter方法。 2.修改beans.xml文件添加autowire属性 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context-4.3.xsd!-- 使用context命名空间在配置文件中开启相应的注解处理器 --context:annotation-config /!-- 分别定义3个Bean实例 --bean iduserDao classcom.nynu.qdy.annotation.UserDaolmpl /bean iduserServiceclasscom.nynu.qdy.annotation.UserServicelmpl autowirebyName/bean iduserControllerclasscom.nynu.qdy.annotation.UserController autowirebyName/!-- 使用context命名空间通知Spring扫描指定包下的所有Bean类进行注解解析 --context:component-scanbase-packagecom.nynu.qdy.annotation / /beans3.结果 userdao...save.... userController....save.... UserService...save....4.提示 使用 Autowired 注解自动装配Bean使用Autowired注解自动装配具有类型兼容的单个Bean属性。可以在构造器、普通字段、一切具有参数的方法上使用 Autowired 注解。 Autowired也可以用在数组上Spring会将所有匹配的Bean自动装配进数组。Autowired也可以用在集合上Spring会判断该集合的类型,然后自动装配所有类型兼容的Bean。Autowired也可以用在Map上若key为String类型Spring将Bean的名称作为keyBean本身作为值自动装配所有类型兼容的的Bean。 也可以使用 Resource 或 Inject 自动装配Bean功能与 Autowired 类似建议使用Autowired注解。
http://www.zqtcl.cn/news/777782/

相关文章:

  • 网站seo优化效果智能营销系统开发
  • 国外做储物的网站个人网站建设在哪里
  • 北京高端网站设计外包公司不用代码做网站的工具
  • 网站开发交付资料广告设计公司经营范围
  • 如何建立一个好的网站wordpress 看不到主题
  • 古典网站织梦模板云南app软件开发
  • 网页设计与网站建设期末考试wordpress文章页面图片自动适应
  • 网站建设费要交印花税吗国内ui网站
  • wordpress安装在本地专业seo网络推广
  • 农庄网站模板网络文化经营许可证图片
  • 微信做模板下载网站有哪些内容江苏省常州建设高等职业技术学校网站
  • 网站开发补充合同范本docker 部署wordpress
  • 学会了php的语法怎么做网站海外推广媒体
  • 东莞网站建设排行企业网站开发公司大全
  • wordpress商城必备软件重庆seo优化推广
  • 蚌埠百度做网站山东省无障碍网站建设标准
  • 平乡企业做网站流量精灵官网
  • 厦门做网站优化公司wordpress cx-udy
  • 做外汇门户网站WordPress推广返佣插件
  • c语言在线编程网站学生个人网页设计作品
  • 南阳网站排名优化报价wordpress视频付费
  • 政务新网站建设ipv6改造wordpress
  • 店招免费设计在线生成网站seo优化关键词快速排名上首页
  • 毕设做系统与网站答辩wordpress个人模板
  • 农家乐网站建设wordpress改变访问目录结构
  • 单位网站建设的重要性盐城城南建设局一局网站
  • 网站登录验证码显示不出来刘强东当年做网站读的什么书
  • 网站seo优化步骤动态ip可以做网站
  • 用自己电脑怎么做网站广州公司建站
  • 购物网站前端浮动特效怎么做常用开发工具