1:Spring 是什麼
spring 是一個開源的開控制反轉(IOC)和麪向切面(AOP)的容器框架。
它的主要目的是簡化企業開發。
2:IOC:
public class PersonServiceBean{
private PersonDao personDao=new PersonDao();
public void save(Person person){
personDao.save(person);
}
}
如上:PersonDao 是在"應用內部"創建及維護的,
所謂的IOC就是應用本身不負責依賴對象的創建及維護,
對象的創建及維護有外部容器負責,這樣控制權就由"應用"轉移到
"外部容器",控制權的轉移就是所謂的反轉;
3:依賴注入:
當我們把依賴對象交給外部容器負責創建,那上面的程序該改成如下:
public class PersonServiceBean{
private PersonDao personDao;
//通過構造器參數,讓容器創建好的依賴對象注入進PersonServiceBean
//當然也可以通過setter方法進行注入
public PersonServiceBean(PersonDao personDao){
this.personDao=personDao;
}
public void save(Person person){
personDao.save(person);
}
}
所謂依賴注入就是指:在運行期,由外部容器動態的將依賴對象注入到組件中。
4:spring的好處:
(1)降低了action-->service-->dao層之間的耦合度,
以前action層需要調用service層(及在action中的new service層),
service層需要調用dao層;
如上面"2:IOC"中的程序一樣(private PersonDao personDao=new PersonDao();)
如果不提供PersonDao的話改程序編譯都不會成功的,這樣就以看成各層之間是緊密
耦合的;
(2)提供了很多的服務:如事務控制等
(3)容器提供了單例模式
(4)提供了aop:權限攔截等
(5)提供了衆多的輔助類:如jdbcTemplate等
(6)對主流框架提供了集成支持
5:spring 管理bean(也就是類)
只需在spring的配置文件中配置一個<bean>,spring就會自動的去實例化該類
如:<bean id="personService" class="com.lid.service.PersonServiceBean"/>
6:bean的作用域:
默認是單利模式,及只創建一次bean
如果想沒調用一次就創建一個新的bean對象的話,只需指定一下作用域即可如下:
<bean id="personService" class="com.lid.service.PersonServiceBean"
scope="prototype"/>
7:bean的創建時:
(1)如果作用域的範圍是默認的:單例模式時 bean是在spring加載時創建的;
如果改成延時實例化<bean id="personService" class="com.lid.service.PersonServiceBean"
lazy-init="true"/>
則bean是在spring容器實例化以後,我們調用spring的getBean方法是實例化的
(2)如果作用域prototype時,bean是在spring容器實例化以後,我們調用spring的getBean方法時實例化的;
如果想讓spring管理的所有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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" default-lazy-init="true">
</beans>
就是在beans標籤中添加default-lazy-init="true";
8:爲spring指定創建bean實例是執行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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" default-lazy-init="true" default-init-method="">
<!-- 這時候該bean由spring來創建 -->
<bean id="personService" class="com.lid.service.PersonServiceBean" init-method="" destroy-method=""/>
</beans>
9:spring的依賴注入:
(1)通過setter方法
類:
public class PersonServiceBean {
private PersonDao personDao;
public void save(){
System.out.println("-----spring test----");
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
}
spring配置文件:
<bean id="personDao" class="com.lid.dao.PersonDao"/>
<bean id="personService" class="com.lid.service.PersonServiceBean">
<property name="personDao" ref="personDao"></property>
</bean>
解析:通過ref後面的名稱在spring中找到該對象,然後注入給PersonServiceBean對象中的
屬性名爲personDao的這個屬性;
當然也可如下:
<bean id="personService" class="com.lid.service.PersonServiceBean">
<property name="personDao">
<bean class="com.lid.dao.PersonDao"></bean>
</property>
</bean>
這兩種的優缺點:
第一種 使用ref這樣<bean id="personDao" class="com.lid.dao.PersonDao"/>
只在外面定義的,可以被公用;
第二種 在在內部定義個,只供自己用;
(2)通過構造方法
public class PersonServiceBean {
private PersonDao personDao;
private String name;
public PersonServiceBean(PersonDao personDao,String name){
this.personDao=personDao;
this.name=name;
}
public void save(){
System.out.println("-----spring test----");
}
}
spring配置文件:
<bean id="personDao" class="com.lid.dao.PersonDao"/>
<bean id="personService" class="com.lid.service.PersonServiceBean">
<constructor-arg index="0" type="com.lid.dao.PersonDao" ref="personDao"/>
<constructor-arg index="1" value="zhangsan"/>
</bean>
解析:
index:只的是構造函數中的第一個參數
type:指的是參數的類型,基本類型不用指定
ref,value:指的是將什麼類型(值)注入;
(3)通過註解注入:
使用@Autowire或者@Resource註解進行裝配
@Autowire 按類型進行裝配------去配置文件中找與標註的類型相同的bean,然後注入;
@Resource 按照名稱進行裝備,當找不到與名稱匹配的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">
<context:annotation-config/>
</beans>
類:
public class PersonServiceBean {
@Resource
private PersonDao personDao;
private String name="xxx";
public PersonServiceBean(PersonDao personDao,String name){
this.personDao=personDao;
this.name=name;
}
public void save(){
System.out.println("-----spring test----");
}
}
配置文件:
<?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">
<context:annotation-config/>
<bean id="personDao" class="com.lid.dao.PersonDao"/>
<bean id="personService" class="com.lid.service.PersonServiceBean"/>
</beans>
10:spring自動掃描和管理bean
spring在類路徑下尋找標註了@Component、@Service、@Controller
、@Repository註解的類,並把這些類納入進spring容器中進行管理;
@Service用於標註業務層組件
@Controller用於標註控制層組件(Strust的action層)
@Repository用於標註數據庫訪問組件,即DAO組件
@Component泛指組件,當不好歸類時用其進行標註
配置文件:
<?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">
<!--spring 掃描的範圍-->
<!--該配置項包含很多註解處理器,
其中就包括<context:annotation-config/>配置項的處理器,
-->
<context:component-scan base-package="com.lid"/>
</beans>
類:
//@Service----不指定名稱,則是類名稱第一個字符小寫後即(personServiceBean)
@Service("personService")//因爲默認是單例模式
@Scope("prototype")//改成非單利模式
public class PersonServiceBean {
private PersonDao personDao;
private String name="xxx";
public PersonServiceBean(PersonDao personDao,String name){
this.personDao=personDao;
this.name=name;
}
@PostConstruct
public void init(){
System.out.println("---類被初始化之後就執行該方法");
}
@PreDestroy
public void destory(){
System.out.println("---類被銷燬之前就執行該方法");
}
public void save(){
System.out.println("-----spring test----");
}
}
spring學習筆記(上)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.