目錄
Spring架構的介紹... 1
1、spring架構的作用... 1
2、spring的優點... 2
Spring架構的搭建... 2
1.導包... 2
2.建立一個對象... 4
3.書寫配置檔案... 5
4.Spring架構的思想... 6
5.Spring工廠... 7
Spring架構的介紹
1、spring架構的作用
Spring架構是一個一棧式架構(javaee三層開發(web service dao)spring架構全部解決)。Spring本身就有這三層架構對應的功能web:spring MVC springBoot service:spring dao:spring整合了JDBC。Spring本身是一個容器,放了什麼對象就擁有了對象的功能。Hibernate和struts2都可以放到這個容器裡面。
2、spring的優點
能夠簡化軟體的開發的複雜性:
(1).IOC(Inversion of Control)控制反轉
(2).AOP思維(面向切面程式設計)
(3).不僅不排斥其他的架構,并且幫助其他架構管理對象
(4).整合了jdbc spring
(5).AOP事務
(6).Junit單元測試
Spring架構的搭建
1.導包
require:是宋老師幫忙整理好的
Spring-3.0.2 spring架構整合的其他jar包,目前市面上所有的jar包
Spring-5.0.2 spring架構本身的包
打開spring-5.0.2之後:
libs:jar包
schema:限制檔案,spring架構限制檔案有很多
Spring 架構将功能進行了分層。
Date Access/Integration:dao層的功能
Web:對應的是web層的功能
中間的部分是使用上面兩個層支援的jar包。
下面是spring的核心包:
還需要導入日志檔案,市面上非常好用的日志:Apache-logging log4j 去spring當面整合的檔案中找。
2.建立一個對象
package cn.hd.test;
public class User {
private String name;
private String age;
private String address;
public User() {
}
public User(String name, String age,String address) {
this.name = name;
this.age = age;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(Stringaddress) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"name='" + name+ '\'' +
", age='" + age+ '\'' +
", address='" +address + '\'' +
'}';
}
}
3.書寫配置檔案
1.配置檔案名字和位置都沒有要求,但是建議放在src路徑下,名字為applicationContext.xml
2.導入限制
3.配置bean類
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean name="user" class="cn.hd.test.User"></bean>
</beans>
4.書寫測試類
package cn.hd.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo {
public static void main(String[]args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) ac.getBean("user");
System.out.println(user);
}
}
4.Spring架構的思想
(1)、IOC(控制反轉):由spring架構來幫助我們建立對象。
(2)、di(依賴注入):為了實作IOC思想。
對象屬性的注入
5.Spring工廠
BeanFactory:是spring架構最原始的接口,用來建立bean類。
BeanFactory建立對象的方式是什麼時候使用什麼時候建立。适用于資源比較貧瘠。
ApplicationContext:現在用的。
建立對象的方式是加載完配置檔案後就建立。
FileSystemXmlApplicationContext:從指定路徑讀取配置檔案。
ClassPathXmlApplicationContext:從類的路徑讀取配置檔案。