Spring
控制反轉(zhuǎn)IOC
IoC Inverse of Control 反轉(zhuǎn)控制的概念,就是將原本在程序中手動(dòng)創(chuàng)建HelloService對(duì)象的控制權(quán),交由Spring框架管理,簡(jiǎn)單說,就是創(chuàng)建HelloService對(duì)象控制權(quán)被反轉(zhuǎn)到了Spring框架
BeanFactory,延遲實(shí)例化bean,第一次調(diào)用getBean
ApplicationContext 一般常用,功能更強(qiáng)
ClassPathXmlApplicationContext 加載classpath xml文件
FileSystemXmlApplicationContext 加載指定盤符文件 , ServletContext.getRealPath()
加載Spring容器
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("cn/ccj/a/hello/beans.xml");
ClassPathXmlApplicationContext(String)加載一個(gè)spring容器
ClassPathXmlApplicationContext(String[])加載多個(gè)spring容器
Spring DI 設(shè)置字段內(nèi)容
DI:Dependency Injection 依賴注入,在Spring框架負(fù)責(zé)創(chuàng)建Bean對(duì)象時(shí)(控制反轉(zhuǎn)),動(dòng)態(tài)的將依賴對(duì)象注入到Bean組件。
getBean("helloService")從spring容器中獲得指定名稱對(duì)象的實(shí)例時(shí),通過此設(shè)置<property name="info" value="haha"></property>
相當(dāng)于執(zhí)行 servcie.setInfo("傳智播客");
<?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="helloService" class="cn.ccj.a.hello.HelloService">
<!—通過spring容器,給HelloService類的info屬性注入“傳智播客”-->
<property name="info" value="haha"></property>
</bean>
</beans>
從spring直接獲得注入的內(nèi)容
package cn.ccj.a.hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestHello {
public static void main(String[] args) {
//使用spring 控制反轉(zhuǎn),交予spring創(chuàng)建對(duì)象
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("cn/ccj/a/hello/beans.xml");
HelloService servcie =
(HelloService) applicationContext.getBean("helloService");
//servcie.setInfo("hello");
servcie.sayInfo();
}
}
2.Bean.xml的配置
類別說明
類別 | 說明 |
---|---|
singleton | 在Spring IoC容器中僅存在一個(gè)Bean實(shí)例,Bean以單例方式存在,默認(rèn)值 |
prototype | 每次從容器中調(diào)用Bean時(shí),都返回一個(gè)新的實(shí)例,即每次調(diào)用getBean()時(shí) ,相當(dāng)于執(zhí)行new XxxBean() |
request | 每次HTTP請(qǐng)求都會(huì)創(chuàng)建一個(gè)新的Bean,該作用域僅適用于WebApplicationContext環(huán)境 |
session | 同一個(gè)HTTP Session 共享一個(gè)Bean,不同Session使用不同Bean,僅適用于WebApplicationContext 環(huán)境 |
globalSession | 一般用于Portlet應(yīng)用環(huán)境,該作用域僅適用于WebApplicationContext 環(huán)境 |
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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">
<!-- 4 bean 作用域
* singleton 單例,只創(chuàng)建一個(gè)實(shí)例。默認(rèn)值
* prototype 多例,每一次都創(chuàng)建實(shí)例
* request,request范圍,request.setAttribute
* session,session范圍,session.setAttribute
* globalSession , prolet 分布式門戶,sso(單點(diǎn)登錄)將不同的應(yīng)用的數(shù)據(jù)保存到globalSession中,達(dá)到數(shù)據(jù)共享
-->
<bean id="person" class="cn.ccj.c_beanscope.Person" scope="prototype"></bean>
</beans>
bean的生命周期
- instantiate bean對(duì)象實(shí)例化
- populate properties 封裝屬性
- 如果Bean實(shí)現(xiàn)BeanNameAware 執(zhí)行 setBeanName
- 如果Bean實(shí)現(xiàn)BeanFactoryAware 或者 ApplicationContextAware 設(shè)置工廠 setBeanFactory 或者上下文對(duì)象 setApplicationContext
- 如果存在類實(shí)現(xiàn) BeanPostProcessor(后處理Bean) ,執(zhí)行postProcessBeforeInitialization
- 如果Bean實(shí)現(xiàn)InitializingBean 執(zhí)行 afterPropertiesSet
- 調(diào)用<bean init-method="init"> 指定初始化方法 init
- 如果存在類實(shí)現(xiàn) BeanPostProcessor(處理Bean) ,執(zhí)行postProcessAfterInitialization
- 執(zhí)行業(yè)務(wù)處理
- 如果Bean實(shí)現(xiàn) DisposableBean 執(zhí)行 destroy
- 調(diào)用<bean destroy-method="customerDestroy"> 指定銷毀方法 customerDestroy
Spring 注解
注解裝配bean
- @Component 普通組件bean注解
- @Controller 用于配置表示層web
- @Service 用于配置業(yè)務(wù)層邏輯service層
- @Repository 用于配置數(shù)據(jù)訪問層Dao
舉例子
//表現(xiàn)層 web
@Controller("myweb")
public class Web {
@Autowired
private Service service;
public void save(){
service.save();
System.out.println("web save");
}
}
//業(yè)務(wù)邏輯service
@org.springframework.stereotype.Service
public class Service {
@Autowired
@Qualifier("myDao")
private Dao dao;
public void save(){
dao.save();
System.out.println("service save");
}
}
//數(shù)據(jù)持久層 Dao層
@Repository("myDao")
public class Dao {
public void save(){
System.out.println("dao save");
}
}
//bean.xml配置
//Spring容器
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 確定使用注解 -->
<context:annotation-config></context:annotation-config>
<!-- 自動(dòng)掃描指定的包 -->
<context:component-scan base-package="cn.ccj.spring"></context:component-scan>
</beans>
spring里面的注解
@Qualifier
@Autowired注解來指定自動(dòng)裝配,@Autowired可以修飾setter方法、普通方法、實(shí)例變量和構(gòu)造器等。當(dāng)使用@Autowired標(biāo)注setter方法時(shí),默認(rèn)采用byType自動(dòng)裝配策略。在這種策略下,符合自動(dòng)裝配類型的候選Bean實(shí)例常常有多個(gè),這個(gè)時(shí)候就可能引起異常,為了實(shí)現(xiàn)精確的自動(dòng)裝配,Spring提供了@Qualifier注解,通過使用@Qualifier,允許根據(jù)Bean的id來執(zhí)行自動(dòng)裝配。
使用@PostConstruct和@PreDestroy定制生命周期行為
@PostConstruct和@PreDestroy同樣位于javax.annotation包下,也是來自JavaEE規(guī)范的兩個(gè)Annotation,Spring直接借鑒了它們,用于定制Spring容器中Bean的生命周期行為。它們都用于修飾方法,無須任何屬性。其中前者修飾的方法時(shí)Bean的初始化方法;而后者修飾的方法時(shí)Bean銷毀之前的方法。
@Component("book")
public class Book {
@PostConstruct
public void init(){
System.out.println("初始化");
}
@PreDestroy
public void destory(){
System.out.println("銷毀");
}
}
@Scope("singleton")
- @Scope("singleton") 給當(dāng)前bean配置范圍,取值:singleton、prototype等
Spring基礎(chǔ)知識(shí)匯總 Java開發(fā)必看
Spring的AOP
,AOP為Aspect Oriented Programming的縮寫,意為:面向切面編程,通過預(yù)編譯方式和運(yùn)行期動(dòng)態(tài)代理實(shí)現(xiàn)程序功能的統(tǒng)一維護(hù)的一種技術(shù)。AOP是OOP(面向?qū)ο缶幊蹋┑难永m(xù),是軟件開發(fā)中的一個(gè)熱點(diǎn),也是Spring框架中的一個(gè)重要內(nèi)容,是函數(shù)式編程的一種衍生范型。利用AOP可以對(duì)業(yè)務(wù)邏輯的各個(gè)部分進(jìn)行隔離,從而使得業(yè)務(wù)邏輯各部分之間的耦合度降低,提高程序的可重用性,同時(shí)提高了開發(fā)的效率。
經(jīng)典應(yīng)用:事務(wù)管理、性能監(jiān)視、安全檢查、緩存 、日志等
AOP核心概念
1、橫切關(guān)注點(diǎn)
對(duì)哪些方法進(jìn)行攔截,攔截后怎么處理,這些關(guān)注點(diǎn)稱之為橫切關(guān)注點(diǎn)
2、切面(aspect)
類是對(duì)物體特征的抽象,切面就是對(duì)橫切關(guān)注點(diǎn)的抽象
3、連接點(diǎn)(joinpoint)
被攔截到的點(diǎn),因?yàn)镾pring只支持方法類型的連接點(diǎn),所以在Spring中連接點(diǎn)指的就是被攔截到的方法,實(shí)際上連接點(diǎn)還可以是字段或者構(gòu)造器
4、切入點(diǎn)(pointcut)
對(duì)連接點(diǎn)進(jìn)行攔截的定義
5、通知(advice)
所謂通知指的就是指攔截到連接點(diǎn)之后要執(zhí)行的代碼,通知分為前置、后置、異常、最終、環(huán)繞通知五類
6、目標(biāo)對(duì)象
代理的目標(biāo)對(duì)象
7、織入(weave)
將切面應(yīng)用到目標(biāo)對(duì)象并導(dǎo)致代理對(duì)象創(chuàng)建的過程
8、引入(introduction)
在不修改代碼的前提下,引入可以在運(yùn)行期為類動(dòng)態(tài)地添加一些方法或字段