目录
1.IOC(Inversion of Control)控制反转
2.DI(Dependency Injection)依赖注入
Spring
1.spring Framework
-
spring生态圈的最基础的项目,是其他项目的根基。
-
spring Framework 系统架构
-
首先是Core Container(核心容器)包含了Beans Core Context SpEL
-
Date Access/Integration (数据访问/集成) 包含 JDBC ORM OXM JMS Transactions
-
Web 包含 WebSocket Servlet Web Portlet
-
AOP :面向切面编程
-
Aspects :AOP 思想实现
-
Instrumentation
-
Messaging
-
Test
-
1.1 核心概念
1.IOC(Inversion of Control)控制反转
使用对象时,由主动new产生对象转换为由外部提供对象,此过程中对象创建控制权由程序转移到外部,此思想称为控制反转
IOC 容器 负责对象的创建、初始化等一系列工作,其中包含了数据层和业务层的类对象 被创建或被管理的对象在IOC容器中统称为Bean IOC容器中放的就是一个个的Bean对象
2.DI(Dependency Injection)依赖注入
在容器中建立bean与bean之间的依赖关系的整个过程,称为依赖注入
3.1和2都是为了充分解耦
使用IOC容器管理bean(IOC) 在IOC容器内将有依赖关系的bean进行关系绑定(DI) 最终结果为:使用对象时不仅可以直接从IOC容器中获取,并且获取到的bean已经绑定了所有的依赖关系
配置
applicationContext.xml <bean id="bookdao" class="com.scidag.dao.impl.BookDaoImpl"/> <bean id="bookservice" class="com.scidag.service.impl.BookServiceImpl"> <property name="bd" ref="bookdao"/> </bean> App.java //获取ioc容器 ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); //BookDao bookdao = (BookDao) ac.getBean("bookdao"); //bookdao.save(); BookService bookservice = (BookService)ac.getBean("bookservice"); bookservice.outBook();
4.bean基础配置
-
基础配置
<bean id="" class=""/>
-
bean 起多个名称 name ='name1, name2 name3; name4'中间用, ’ ‘ ;都可以
<bean id="bookService" name="service service4 bookEbi" <-- Ebi全称Enterprise Business Interface,翻译为企业业务接口 -->
-
scope 配置
默认值为singleton
singleton 默认为单例 prototype 为非单例
4.1bean 实例化
对象交给Ioc容器来创建,实例化bean过程
bean是如何创建的,实例化bean 的三种方式,构造方法,静态工厂,实例工厂
-
构造方法
app.java ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); BookDao bookdao = (BookDao) ac.getBean("bookdao"); bookdao.save(); ApplicationContext.xml <bean id="bookdao" class="com.scidag.dao.impl.BookDaoImpl"/> <bean id="bookservice" name="service" class="com.scidag.service.impl.BookServiceImpl"> <property name="bd" ref="bookdao"/> </bean>-->
-
静态工厂
App.java ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userdao =(UserDao) ac.getBean("userdao"); userdao.save(); ApplicationContext.xml <bean id="userdao" class="com.scidag.factory.UserDaoFactory" factory-method="getUserDao"></bean>
-
实例工厂
ApplicationContext.xml <bean id="studao" class="com.scidag.factory.StuDaoFactory"/> <bean id="stu" factory-bean="studao" factory-method="getStu"/> app.java StuDaoFactory s=new StuDaoFactory(); StuDao stuDao=s.getStu(); stuDao.save();
-
FactoryBean
public class AppForInstanceUser { public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); StuDao stu = (StuDao) ctx.getBean("stu"); stu.save(); } } ApplicationCopntext.xml <bean id="stu" class="com.scidag.factory.StuDaoFactoryBean"/> public class StuDaoFactoryBean implements FactoryBean { @Override public StuDao getObject() throws Exception { return new StuDaoImpl(); } @Override public Class<?> getObjectType() { return StuDao.class; } }
4.2bean的生命周期
5.DI注入
setter注入 简单类型
<!-- 简单类型注入--> <bean id="bookDao" class="com.scidag.dao.impl.BookDaoImpl"> <property name="dasename" value="mysql"/> <property name="age" value="18"/> </bean> <!-- set注入--> <!-- <bean id="bookDao" class="com.scidag.dao.impl.BookDaoImpl" />--> <bean id="userDao" class="com.scidag.dao.impl.UserDaoImpl" /> <bean id="bookservice" class="com.scidag.service.impl.BookServiceImpl"> <property name="bookDao" ref="bookDao"/> <property name="userDao" ref="userDao"/> </bean> public class AppForDISet { public static void main(String[] args) { ApplicationContext ap=new ClassPathXmlApplicationContext("applicationContext.xml"); BookService bookService =(BookService) ap.getBean("bookservice"); bookService.save(); } } public class BookDaoImpl implements BookDao { private String dasename; private int age; public void setDasename(String dasename) { this.dasename = dasename; } public void setAge(int age) { this.age = age; } public void save() { System.out.println("book dao save"+dasename+age); }
构造器注入
<bean id="bookDao" class="com.scidag.dao.impl.BookDaoImpl"/> <bean id="bookService" class="com.scidag.service.impl.BookServiceImpl"> <constructor-arg name="bookDao" ref="bookDao"/> </bean> public class BookServiceImpl implements BookService { private BookDao bookDao; public BookServiceImpl(BookDao bookDao) { this.bookDao = bookDao; } @Override public void save() { System.out.println("book service save"); bookDao.save(); } } public class App { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); BookService bookService = (BookService) ctx.getBean("bookService"); bookService.save(); } }
5.1自动配置
5.1.1 自动装配概念
IoC容器根据bean所依赖的资源在容器中自动查找并注入到bean中的过程称为自动装配
5.1.2自动装配类型
-
==按类型(常用)== autowire="byType"
- 需要注入属性的类中对应属性的setter方法不能省略 - 被注入的对象必须要被Spring的IOC容器管理 - 按照类型在Spring的IOC容器中如果找到多个对象,会报`NoUniqueBeanDefinitionException` 一个类型在IOC中有多个对象,还想要注入成功,这个时候就需要按照名称注入,配置方式为:
-
按名称 autowire="byName"
-
按构造方法
-
不启用自动装配
5.1.3集合类型注入
<bean id="bookDao" class="com.scidag.dao.impl.BookDaoImpl"> <property name="arr"> <array> <value>100</value> <value>110</value> <value>120</value> </array> </property> <property name="list"> <list> <value>daishio</value> <value>daishiaadao</value> <value>daishaaio</value> </list> </property> <property name="map"> <map> <entry key="12" value="大苏打"></entry> <entry key="11" value="苏打"></entry> <entry key="14" value="大的"></entry> </map> </property> <property name="set"> <set> <value>idss</value> <value>idss</value> <value>idsas</value> </set> </property> <property name="properties"> <props> <prop key="1">oka</prop> <prop key="3">oa</prop> <prop key="2">ok</prop> </props> </property> </bean>