网站备案人有什么责任,青岛网站建设与设计制作,汽车网站源码,网站权重问题Bean的配置
Spring容器支持XML(常用)和Properties两种格式的配置文件
Spring中XML配置文件的根元素是,中包含了多个子元素#xff0c;每个子元素定义了一个Bean,并描述了该Bean如何装配到Spring容器中
元素包含了多个属性以及子元素#xff0c;常用属性及子元素如下所示 i…Bean的配置
Spring容器支持XML(常用)和Properties两种格式的配置文件
Spring中XML配置文件的根元素是,中包含了多个子元素每个子元素定义了一个Bean,并描述了该Bean如何装配到Spring容器中
元素包含了多个属性以及子元素常用属性及子元素如下所示 id 是一个Bean的唯一标识Spring容器对Bean的配置、管理都有这个属性完成 name Spring容器同样可以通过此属性对容器中的Bean进行配置和管理name属性可以为Bean指定多个名称、每个名称之间用逗号或分号隔开 class 该属性制定了Bean的具体实现类它必须是一个完整的类名使用类的全限定名 scope 用来设定Bean实例的作用域 constructor-arg 元素的子元素可以使用此元素传入构造参数进行实例化。 该元素的index属性指定构造参数的序号从0开始 type属性指定构造参数的类型 参数值可以通过ref属性或value属性直接指定也可以通过ref或value子元素指定 property 元素的子元素用于调用Bean实例中的setter方法完成属性赋值从而完成依赖注入。该元素的name属性指定Bean实例中的相应属性名。 ref属性或value属性指定参数值 ref 、等元素的属性或子元素可以用于指定Bean工厂中某个Bean的实例引用 value 、等元素的属性或子元素。可以用于直接指定一个常量值 list 用于封装List或数组类型的依赖注入 set 用于封装Set类型属性的依赖注入 map 用于封装Map类型属性的依赖注入 entry 元素的子元素用于设置一个键值对
注意如果Bean中未指定id和nameSpring会将class值当作id
例子
?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 iduserDao classcom.aqiuo.UserDaoImp /bean iduserService classcom.aqiuo.UserServiceImpproperty nameuserDao refuserDao/property/bean/beansBean的实例化
构造器实例化
构造器实例化是指Spring容器通过Bean对应类中默认的的无参构造函数来实例化Bean。
静态工厂方式实例化
开发者创建一个静态工厂方法来创建Bean的实例其Bean配置的class属性所指定的不再是Bean实例的实现类而是静态工厂类。同时需要使用factory-method属性来指定所创建的静态工厂方法。
package com.aqiuo.Static_Factory;public class Bean2 {}//静态工厂
public class MyBean2Factory {public static Bean2 createBean2() {return new Bean2();}
}
//xml配置文件
?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 //id还是bean的id class指定对应的工厂实现类 factory-method指定工厂方法bean idbean2 classcom.aqiuo.Static_Factory.MyBean2Factory factory-methodcreateBean2/bean
/beans
//测试实例工厂方式实例化 采用直接创建Bean的实例同时在配置文件中需要的实例化Bean也不是通过class属性直接指向实例化类而是通过factory-bean书性指向配置的实例工厂然后使用factory-method属性指定使用工厂的哪个方法 public class MyBean3Factory {public MyBean3Factory() {System.out.println(Bean3工厂实例化...);}public Bean3 createBean3() {return new Bean3();}
}!--实例工厂--bean idmyBean3Factory classcom.aqiuo.Instance_factory.MyBean3Factory scopeprototype/bean!-- factory-bean指定配置的实例工厂 factory-method指定工厂的哪个方法 --bean idbean3 factory-beanmyBean3Factory factory-methodcreateBean3/bean
Bean的作用域 singleton(默认) 使用ta定义的Bean在Spring容器中将只有一个实例无论多少个bean引用它都指向同一个对象 prototype 每次通过Spring容器获取的prototype定义的bean时容器都创建一个新的Bean实例 request 在一次HTTP请求中容器会返回Bena的同一个实例不同Http请求会产生新的Bean,该Bean仅在当前HTTP Request内有效 session 在一次HTTPSession请求中容器会返回Bena的同一个实例不同Http请求会产生新的Bean。该Bean仅在当前HTTP Session内有效 globalSession application websocket
Bean的生命周期 Bean的装配方式
xml配置
setter配置先调用默 认无参构造函数实力化对象然后通过反射方式调用setter方法注入属性
要求
Bean类必须有一个默认无参构造方法
Bean类必须为需要注入的属性提供对应的setter方法
构造器配置
要求
Bean类必须有一个有参构造方法 //setter注入bean iduser1 classcom.aqiuo.User.Userproperty nameusername value冉雨珊/propertyproperty namepassword value张宇/propertyproperty namelistlistvalue1/valuevalue2/valuevalue3/value/list/property/bean//构造器注入bean iduser2 classcom.aqiuo.User.Userconstructor-arg index0 valuerys/constructor-argconstructor-arg index1 valuezy/constructor-argconstructor-arg index2listvalueqq/valuevaluebb/valuevaluemm/value/list/constructor-arg/beanbean idstudent classcom.orz.spring.bean.Studentproperty namename value李华/property namemapmapentry keykey1 value数据库原理/entry keykey2 valuejava编程//map /property/bean
自动装配
元素的autowire属性 default byName byType constructor no
注解配置
注解 Component 此注解可以描述Spring中的bean泛化的概念仅仅表示一个组件Bean Repository 用于将数据访问层DAO层的类标识为Spring中的bean Service 通常作用在业务层Service层用于将业务层的类标识为Spring中的bean Controller 通常作用在控制层用于将控制层的类标识为Spring中的Bean Autowired 用于对bean的属性变量、属性的setter方法及构造方法进行标注配合对应的注解处理器完成Bean的自动配置工作 Resource 默认按照Bean实例名称进行装配name解析为bean名称type属性解析为Bean实例类型 Qualifier 与Autowired注解配合使用会将默认按Bean类型装配修改为按Bean实例名称装配。
注意:Spring 4.0以上版本使用上面的代码对指定包中注解进行扫描前需要先向项目中导入SpringAOP的jar包否则会爆出错误
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:contexthttp://www.springframework.org/schema/contextxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd 扫描包下的注解context:component-scan base-packagecom.aqiuo.annotation.* / /beans