苏州工业园区网站,广东seo推广工具,中国美食网页设计,wordpress 媒体库IoC,Inversion of Control 控制反转#xff0c;是一个过程。仅通过构造函数、工厂方法或在对象实例化后在对象实例上设置属性来定义其依赖关系。容器负责这些工作#xff0c;这个过程从本质上来说是bean本身的反向#xff0c;因此称为反向控制。
1 容器
负责实例化、配置及… IoC,Inversion of Control 控制反转是一个过程。仅通过构造函数、工厂方法或在对象实例化后在对象实例上设置属性来定义其依赖关系。容器负责这些工作这个过程从本质上来说是bean本身的反向因此称为反向控制。
1 容器
负责实例化、配置及装配bean。容器从配置元数据那获取该怎么实例化、配置及装配bean。而配置来源主要有三个1XML2java注释3java代码。xml比较常用。
Configuration // 将一个普通类标志为IoC容器
public class CustomConfig {Bean // 定义一个beanpublic User customUser() {return new User();}
}private static void test1() {AnnotationConfigApplicationContext context new AnnotationConfigApplicationContext(CustomConfig.class);User user context.getBean(User.class);System.out.println(user);
}
上面代码展示了java代码配置bean的方式。通过AnnotationConfigApplicationContext来获取容器。 图 ApplicationContext的方法及实现类
BeanFactory 接口提供了管理Bean的方法而ApplicationContext继承了该接口并有以下扩展
1更容易与Spring 的aop集成。
2消息资源处理。
3事件发布。
4针对web项目提供了特定的子类。 图 ApplicationContext的方法及实现类
2 Bean
构成程序主干并由IoC容器管理的对象称为bean。
在模块化开发中不同模块的容器可能存在依赖了同一bean的bean。有时我们考虑到扩展或者在某个模块中有特定的命名规范所依赖的这个bean的命名可能会不同。 比如模块A、B依赖同一个数据源配置bean。 在模块A中该bean命名为datasourceA在模块B中命名为datasourceB。这时候需要用到别名。
!--数据源common.xml--
bean iddatasource classdao.BaseDao/!--模块A a.xml--
import resourcedao.xml/!—别名--
alias namedatasource aliasdatasourceA/
bean idteacherService classservice.TeacherServiceproperty namebaseDao refbaseDao/
/bean
2.1 实例化
xml配置中实例化bean有三种方式1构造函数2静态工厂方法3bean的工厂方法。
public class GoodsDao {
}public class GoodsService {public GoodsDao makeGoodsDao() {return new GoodsDao();}
}public class StaticFactory {public static GoodsService makeGoodsService() {return new GoodsService();}
}beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd default-lazy-inittrue!--构造函数实例化--bean idgoodsService classinstantiating.GoodsService/!--静态工厂实例化class为该工厂的类--bean idgoodsService2 classinstantiating.StaticFactory factory-methodmakeGoodsService/!--bean的工厂实例化--bean idgoodsDao factory-beangoodsService factory-methodmakeGoodsDao/
/beans
3 依赖
依赖是指对象在运行中需要用到的其他对象。在IoC中由容器负责注入。注入方式有两种1构造函数2set方法。
public class OtherDao {
}public class ReportDao {
}public class ReportService {private ReportDao reportDao;private String name;private Double num;public ReportService(ReportDao reportDao, String name, Double num) {this.reportDao reportDao;this.name name;this.num num;}private Properties dataProperties;private MapString, String stockInfoMap;private ListString links;private OtherDao otherDao;public void setDataProperties(Properties dataProperties) {this.dataProperties dataProperties;}public void setStockInfoMap(MapString, String stockInfoMap) {this.stockInfoMap stockInfoMap;}public void setLinks(ListString links) {this.links links;}public void setOtherDao(OtherDao otherDao) {this.otherDao otherDao;}Overridepublic String toString() {return ReportService{ reportDao reportDao , name name \ , num num , dataProperties dataProperties , stockInfoMap stockInfoMap , links links , otherDao otherDao };}public static void main(String[] args) {ApplicationContext applicationContext new ClassPathXmlApplicationContext(dependency.xml);ReportService reportService applicationContext.getBean(ReportService.class);System.out.println(reportService);}
}beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd default-lazy-inittruebean idreportDao classdependency.ReportDao/bean idreportService classdependency.ReportService
!-- constructor-arg 为构造函数参数参数可以依赖其他bean,也可以是基本类似--constructor-arg namereportDao refreportDao/constructor-arg namename valuereportService层/constructor-arg namenum value103.4/
!-- set方法来注入参数--
!-- Properties类型--property namedataPropertiespropsprop keyhostlocalhost/propprop keyusernameadmin/propprop keypassword123/prop/props/property
!-- Map类型--property namestockInfoMapmapentry keyname value中国平安/entry keystock value601318.sh//map/property
!-- List类型--property namelinks
!-- 空值--null //propertyproperty nameotherDao
!-- 内部bean可以不要id或者name,不会被其他bean依赖--bean classdependency.OtherDao//property/bean
/beans
3.1 depends-on与懒加载
容器创建一个bean时会先创建起依赖的bean。但是有时两个bean直接没有直接依赖但是希望在创建这个bean之前先创建其他的bean。可用depends-on来完成这个需求
bean idteacherDao classdao.TeacherDao depends-onuserDao,baseDao/
在创建teacherDao这个bean之前会先创建userDao及baseDao这两个bean。
在容器中默认会在项目加载时把所有的bean都创建完成这样做的好处是某个bean的配置错误能在运行时被发现。但是有时不希望创建所有的bean希望当要使用这个bean时再来创建default-lazy-init 懒加载属性可以作用于全局的beans,也可以作用于单个的beans。当值为true时bean在第一次使用时才会被创建。
3.2 方法注入
假如bean1的某个方法在每次调用时都需要一个特定的bean2不是bean1的直接依赖即非bean1的字段。传统方法是可以在该方法中通过ApplicationContext获取bean2。这是这样加大了耦合度容器提供了Lookup标签来实现此类需求
public class Bean2 {
}public class Bean1 {private final static ApplicationContext applicationContext new ClassPathXmlApplicationContext(method.xml);/*** 传统方式*/public void showBean2OfTradition() {Object bean2 applicationContext.getBean(bean2);System.out.println(bean2);}/*** Lookup 方法*/public Bean2 getBean2() {return null;}public static void main(String[] args) {Bean1 bean1 applicationContext.getBean(Bean1.class);bean1.showBean2OfTradition();System.out.println(bean1.getBean2());}
}beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd default-lazy-inittruebean idbean1 classmethod.Bean1
!-- 会覆盖原getBean2方法直接返回bean2--lookup-method namegetBean2 beanbean2//beanbean idbean2 classmethod.Bean2/
/beans
4 作用域
bean 也可以指定作用域生命周期Spring支持六种作用域。bean的scope属性来指定该bean的作用域。 singleton 默认作用域。不同容器生成的bean不同。 prototype 在同一容器中不同bean依赖的bean被创建的实例不同。 request 每次请求都会创建不同的bean。 session 每个session都会创建不同 bean。 application 每个servletContext生成不同的bean。 websocket 每个websocket连接生成不同的bean。
表 spring 的六种bean的作用域