JavaWeb---Spring的學習筆記(1)
阿新 • • 發佈:2018-12-19
1. 什麼是spring,它能夠做什麼?
Spring是一個開源框架,它由Rod Johnson建立。它是為了解決企業應用開發的複雜性而建立的。 Spring使用基本的JavaBean來完成以前只可能由EJB完成的事情。 然而,Spring的用途不僅限於伺服器端的開發。從簡單性、可測試性和鬆耦合的角度而言,任何Java應用都可以從Spring中受益。 目的:解決企業應用開發的複雜性 功能:使用基本的JavaBean代替EJB,並提供了更多的企業應用功能 範圍:任何Java應用 簡單來說,Spring是一個輕量級的控制反轉(IoC)和麵向切面(AOP)的容器框架。
2.簡介ioc
2.1建立介面和類
package com.zking.ioc.biz;
public interface UserDao {
public void doSomething();
}
package com.zking.ioc.biz.impl; import com.zking.ioc.biz.UserDao; /** * 模擬做專案實現某一功能 * @author Theshy * */ public class UserBizImpl implements UserDao{ public void doSomething() { // TODO Auto-generated method stub System.out.println("系統升級:效能更優!"); } }
問題: 如果底層的實現切換了,需要修改原始碼,能不能不修改程式原始碼對程式進行擴充套件?
2.2將實現類交給Spring管理 在spring的解壓路徑下spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" default-autowire="byName" 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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <bean class="com.zking.ioc.biz.impl.UserBizImpl" id="userBiz"></bean> </beans>
3.ioc的依賴注入 3.1實體類
package com.zking.ioc.web;
import java.util.List;
public class StudentAction {
private Integer sid;
private String sname;
private List<String> hidden;
/*public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}*/
public List<String> getHidden() {
return hidden;
}
public void setHidden(List<String> hidden) {
this.hidden = hidden;
}
public void aaa() {
System.out.println(this.sid+" "+this.sname+" "+this.hidden);
}
public StudentAction() {
}
public StudentAction(Integer sid, String sname) {
super();
this.sid = sid;
this.sname = sname;
}
}
set注入
<!-- ioc注入的型別以及方式 -->
<bean class="com.zking.ioc.web.StudentAction" id="studentAction">
<!-- set/get方式的注入 -->
<property name="sid" value="22"></property>
<property name="sname" value="包子"></property>
<property name="hidden" >
<list>
<value>的看守的</value>
<value>大蘇打</value>
</list>
</property>
</bean>
構造注入
<!-- ioc注入的型別以及方式 -->
<bean class="com.zking.ioc.web.StudentAction" id="studentAction">
<!-- 構造方法注入 --!>
<constructor-arg name="sid" value="22"></constructor-arg>
<constructor-arg name="sname" value="燉冬瓜"></constructor-arg>
<property name="hidden" >
<list>
<value>的看守的</value>
<value>大蘇打</value>
</list>
</property>
</bean>
2.3自動裝配
<!--
byType:根據管理javabean的介面屬性,在spring的上下文自動尋找實現類去注入,
當找到兩個以及以上的時會報錯。與上下文的id無關
byName:根據管理javabean的介面名,在spring上下文中尋找同名的介面
進行注入
-->