先綜述一下,看到的不少資料總結出來的東西,若是有錯或者不全,還但願提出寶貴意見。web
Spring是一個開源框架。Spring的核心是控制反轉(IoC)和麪向切面(AOP)。簡單來講,Spring是一個分層的JavaSE/EE full-stack(一站式) 輕量級開源框架。spring
輕量級:與EJB對比,依賴資源少,銷燬的資源少。mybatis
分層: 一站式,每個層都提供的解決方案app
web層:struts,spring-MVC框架
service層:spring學習
dao層:hibernate,mybatis , jdbcTemplate --> spring-dataspa
Spring的核心是控制反轉(IoC)和面向切面(AOP)hibernate
提供UserService接口和實現類,得到UserService實現類的實例.以前開發中,直接new一個對象便可。code
學習spring以後,將由Spring建立對象實例--> IoC 控制反轉(Inverse of Control)。以後須要實例對象時,從spring工廠(容器)中得到,須要將實現類的全限定名稱配置到xml文件中xml
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置service <bean> 配置須要建立的對象 id :用於以後從spring容器得到實例時使用的 class :須要建立實例的全限定類名 --> <bean id="userServiceId" class="com.itheima.a_ioc.UserServiceImpl"></bean> </beans>
public void demo02(){ //從spring容器得到 //1 得到容器 String xmlPath = "com/itheima/a_ioc/beans.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); //2得到內容 --不須要本身new,都是從spring容器得到 UserService userService = (UserService) applicationContext.getBean("userServiceId"); userService.addUser(); }
DI
DI Dependency Injection ,依賴注入
is a :是一個,繼承。
has a:有一個,成員變量,依賴。
class B {
private A a; //B類依賴A類
}
依賴:一個對象須要使用另外一個對象
注入:經過setter方法進行另外一個對象實例設置。
例如:
class BookServiceImpl{
//以前開發:接口 = 實現類 (service和dao耦合)
//private BookDao bookDao = new BookDaoImpl();
//spring以後 (解耦:service實現類使用dao接口,不知道具體的實現類)
private BookDao bookDao;
setter方法
}
模擬spring執行過程
建立service實例:BookService bookService = new BookServiceImpl() -->IoC <bean>
建立dao實例:BookDao bookDao = new BookDaoImple() -->IoC
將dao設置給service:bookService.setBookDao(bookDao); -->DI <property>