spring框架介紹
為什么要出現spring?
業界追求軟件高內聚、低耦合、性能好。可維護性好,可擴展性好。
如何做到高內聚?
讓代碼分層,每一層的分工明確,每一層都各司其職,利於代碼的維護。
如何做到低耦合?
將每層的代碼依賴降到最低,不會因為改變某層而影響到其他層,有利於我們的擴展。
如何做到性能好?
代碼的優化。
什么是spring?
spring是一個輕量級的一站式的框架。支持插拔式的開發,只需要四個架包就可以啟動起來。其他的架包只需要按需配置即可,還可以插入其他的框架。與早期的EJB的框架都屬於javaEE企業級開發,EJB是重量級的。
spring的介紹
spring的啟動
只需要四個jar包,還有一個是日志的依賴包。
applicationContext.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">
<!-- 創建一個對象 -->
<bean id="userService" class="cn.zj.spring.service.UserService"/>
</beans>
測試
@Test
public void testBean() {
//傳統做法
// UserService service = new UserService();
// service.say();
//讀取配置文件
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//創建bean對象
UserService userService = context.getBean("userService",UserService.class);
userService.say();
}
IOC控制反轉
IOC是控制反轉,所謂控制反轉就是將創建對象的事情交給spring容器,外界使用的時候只需要在spring容器中拿就行了。從而達到了對象不直接依賴在類中,達到了解耦。(不直接通過new的方式)
好萊塢法則,只有你跟我說你需要對象我就給你。
四種實例化bean的方式
-
構造器實例化
<!-- 構造器實例化 (無參構造函數) --> <!-- 默認情況下使用的方式,會自動調用無參構造函數 原理:反射 Class clz = Class.forName(""); clz.newInstance(); --> <bean id="userService" class="cn.zj.spring.service.UserService"/>
-
靜態工廠
<!-- 靜態工廠實例化 --> <bean id="userService" class="cn.zj.spring.service.UserServiceFactory" factory-method="getObject" />
/** * 靜態工廠 * @author lgx * */ public class UserServiceFactory { public static UserService getObject() { System.out.println("靜態工廠創建了UserService對象"); return new UserService(); } }
-
實例化工廠
<!-- 實體工廠實例化 --> <bean id="userService2Factory" class="cn.zj.spring.service.UserService2Factory"/> <bean id="userService" factory-bean="userService2Factory" factory-method="getObject"/>
/** * 實體工廠 * @author lgx * */ public class UserService2Factory { public UserService getObject() { System.out.println("實體工廠創建了UserService對象"); return new UserService(); }
-
實現FactoryBean接口實例化:實例工廠變種
<!-- 實現FactoryBean接口實例化:實例工廠變種 --> <bean id="userService" class="cn.zj.spring.service.UserService3Factory" />
public class UserService3Factory implements FactoryBean<UserService>{ //返回一個對象 @Override public UserService getObject() throws Exception { System.out.println("實現FactoryBean實例化"); return new UserService(); } //返回對象的類型 @Override public Class<?> getObjectType() { return null; } //是否單例,true是,false不是 @Override public boolean isSingleton() { return false; }
Ioc細節配置
可以配置別名,在bean中配置name,還有創建對象的scope。
<?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"
default-init-method=""
default-destroy-method=""
>
<!-- 細節配置 -->
<!-- 配置別名 name:就是bean的id alias:就是獲取bean對象時的名稱 -->
<alias name="userService" alias="us" />
<!-- 創建一個對象 -->
<!-- 配置name屬性
name: 可以配置多個name,中間用空格隔開
id: 只能配置一個,如果中間有空格也會被當成一個整體
-->
<!-- <bean name="uService service" id="userService" class="cn.zj.spring.service.UserService" /> -->
<!-- 配置scope
singleton:默認情況下創建的對象是單例的
prototype: 多例的
request:一次請求有效,在web項目
session:一次會話有效,在web項目
-->
<!-- <bean scope="prototype" id="userService" class="cn.zj.spring.service.UserService" />
-->
<!--
init-method:在創建對象的時候就調用,每創建一次就調用,寫方法名
destroy-method:當spring容器銷毀時,只有單例模式有效,,多例需手動調用,寫方法名
-->
<bean init-method="init"
destroy-method="destory"
id="userService" class="cn.zj.spring.service.UserService" />
</beans>
DI依賴注入
當我們創建的對象交給了spring容器去做,那么對象與對象之間的關系怎么辦呢?
此時我們就有了一個新的概念叫依賴注入,依賴注入就是處理對象與對象之間的關系。
依賴注入的四種方式
實體類:
User.java
package cn.zj.spring.pojo;
public class User {
private Integer id;
private String name;
private Department dept;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", dept=" + dept + "]";
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(Integer id, String name, Department dept) {
super();
this.id = id;
this.name = name;
this.dept = dept;
}
}
Department.java
package cn.zj.spring.pojo;
public class Department {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Department [id=" + id + ", name=" + name + "]";
}
public Department() {
super();
// TODO Auto-generated constructor stub
}
public Department(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
}
Collection.java
package cn.zj.spring.pojo;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Collection {
private String[] arr;
private List list;
private Set set;
private Map map;
private Properties props;
public String[] getArr() {
return arr;
}
public void setArr(String[] arr) {
this.arr = arr;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Properties getProps() {
return props;
}
public void setProps(Properties props) {
this.props = props;
}
public Collection() {
super();
// TODO Auto-generated constructor stub
}
public Collection(String[] arr, List list, Set set, Map map, Properties props) {
super();
this.arr = arr;
this.list = list;
this.set = set;
this.map = map;
this.props = props;
}
@Override
public String toString() {
return "Collection [arr=" + Arrays.toString(arr) + ", list=" + list + ", set=" + set + ", map=" + map
+ ", props=" + props + "]";
}
}
方式:
-
setting 注入
<!--setting 注入 用得最多的 --> <!-- 員工 --> <bean id="user" class="cn.zj.spring.pojo.User"> <!-- name:屬性的值 value:初始化的值,是值類型比如String,Integer等基本數據類型 ref:引用數據類型,引用spring中bean的id --> <property name="id" value="12" /> <property name="name" value="旺財" /> <property name="dept" ref="dept" /> </bean> <!-- 部門 --> <bean id="dept" class="cn.zj.spring.pojo.Department"> <property name="id"value="1" /> <property name="name" value="人才部" /> </bean>
-
構造器注入
<!-- 構造器注入 --> <bean id="user" class="cn.zj.spring.pojo.User"> <!-- constructor-arg: argument參數的縮寫 index:根據構造器的索引,不建議 name:根據屬性的名字,推薦 type:屬性的數據類 value: 值類型,比如String,Integer等基本數據類型 ref: 引用數據類型 --> <constructor-arg name="id" type="Integer" value="10"/> <constructor-arg name="name" type="String" value="旺財s"/> <constructor-arg ref="dept"/>關聯另外的實體類 </bean> <bean id="dept" class="cn.zj.spring.pojo.Department"> <constructor-arg name="id" type="Integer" value="1"/> <constructor-arg name="name" type="String" value="銷售部"/> </bean>
-
p命名空間注入
-
需要導入p命名空間約束
xmlns:p="http://www.springframework.org/schema/p"
-
<!-- p命名空間注入 --> <!-- xmlns:p="http://www.springframework.org/schema/p" --> <!-- p:屬性= "值" p:屬性-ref = bean.id --> <bean id="user" class="cn.zj.spring.pojo.User" p:id = "2" p:name="蛋黃" p:dept-ref="dept"/> <bean id="dept" class="cn.zj.spring.pojo.Department" p:id="10" p:name="網絡部"/>
-
-
集合注入
<!-- 集合注入 --> <bean id="collection" class="cn.zj.spring.pojo.Collection"> <!-- 數組注入 --> <property name="arr"> <array> <value>aa</value> <value>bb</value> <value>cc</value> </array> </property> <!--list 集合注入 --> <property name="list"> <list> <value>111</value> <value>111</value> <value>222</value> </list> </property> <!--set集合注入 --> <property name="set"> <set> <value>111</value> <value>111</value> <value>222</value> </set> </property> <!-- map集合的注入 --> <property name="map"> <map> <entry key="key1" value="123" /> <entry key="key1" value="456" /> <entry key="key2" value="123" /> </map> </property> <!--Properties注入 --> <property name="props"> <props> <prop key="key1">aa</prop> <prop key="key1">vv</prop> <prop key="key2">cc</prop> </props> </property> </bean>
Properteis文件的配置
spring管理數據庫的操作,比如事務的處理,Connection
對象的管理。現在配置druid-1.1.9.jar
的連接池。
druid-1.1.9.jar:是阿里巴巴開發的,目前最快,最流行的連接池。
db.properties
配置文件
useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
mysql8.0以上需要配置此
#數據庫連接四要素
jdbc.driverClassName = com.mysql.cj.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
jdbc.username = root
jdbc.password = root
#最大連接數
jdbc.maxActive = 10
applicationContext.xml
配置
需要先導入context約束
xmlns:context="http://www.springframework.org/schema/context"
需要添加到xsi:schemaLocation中
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd