spring框架

體系結(jié)構(gòu):

1、Spring core 是spring框架的核心,提供了IOC和依賴注入特性
2、Spring Context 提供了一種框架風(fēng)格的方式來訪問對象,繼承了beans包的功能,同時增加了國際化、事件傳播、資源裝載、以及透明創(chuàng)建上下文
3、Spring AOP 通過配置管理,直接將面向方面編程集成到了框架之中。
4、Spring DAO 提供了JDBC的抽象層。可以消除冗長的JDBC編碼和數(shù)據(jù)庫廠商特有的錯誤代碼

開發(fā)步驟:
1、引入spring jar包
2、添加applicationContext.xml
3、創(chuàng)建一個javabean里面屬性有set get方法即可
4、配置applicationContext.xml
<bean id="he" class="entity.Hello">
<property name="hello" value="我愛中國!!!"></property>
</bean>
5、開發(fā)測試
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Hello bean1 = (Hello) ac.getBean("he");//第1種獲取bean方式,需要強(qiáng)轉(zhuǎn)
Hello bean2 = ac.getBean("he", Hello.class);//第2種獲取bean方式
System.out.println(bean1.getHello());

控制反轉(zhuǎn):
控制反轉(zhuǎn)(IOC--Inversion of control)所謂控制反轉(zhuǎn)就是應(yīng)用本身不負(fù)責(zé)依賴對象的創(chuàng)建及維護(hù),依賴對象的創(chuàng)建及維護(hù)是由外部容器負(fù)責(zé)的。這樣控制權(quán)就由應(yīng)用轉(zhuǎn)移到了外部容器,控制權(quán)的轉(zhuǎn)移就是所謂反轉(zhuǎn)

依賴注入:
注入方式:
1、set 注入

<bean id="he" class="entity.Hello"><!--  第1種  set注入 -->
<property name="hello" value="我愛北京!!!"></property>
</bean>

2、構(gòu)造方法注入

<bean id="stu" class="entity.Student"><!--  第2種  構(gòu)造方法注入 -->
<constructor-arg index="0" value="張三豐"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
</bean>

3、參考注入(接口注入)

<bean id="girl" class="entity.Girl">

<property name="boy" ref="girl"></property><!--  第3種參考注入 -->
</bean>

數(shù)組注入:

1 、public class Teacher {
private String[]info;//數(shù)組
private List list;//List集合
private Set set;//Set集合
private Map map;//Map鍵值對

//所以對象都有set get方法。。。。
2、

<bean id="tea" class="entity.Teacher">
<property name="info"><!-- 注入數(shù)組 -->
<array>
<value>教授</value>
<value>副教授</value>
<value>高工</value>
</array>
</property>
<property name="list"><!-- 注入List集合 -->
<list>
<value>博士</value>
<value>碩士</value>
<value>學(xué)士</value>
</list>
</property>
<property name="set"><!-- 注入Set集合 -->
<set>
<value>北京</value>
<value>上海</value>
<value>南京</value>
</set>
</property>
<property name="map"><!-- 注入Map集合 -->
<map>
<entry key="姓名" value="李小龍"></entry>
<entry key="工資" value="8888"></entry>
<entry key="性別" value="男"></entry>
</map>
</property>
</bean>

3、

ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
    Teacher tea = ac.getBean("tea",Teacher.class);
    String[] info = tea.getInfo();//取出數(shù)組
    for(String ss:info){
        System.out.println(ss);}
    List list = tea.getList();//取出List集合
    for(int i=0;i<list.size();i++){
        System.out.println(list.get(i));}
    Set set = tea.getSet();//取出Set集合
    Iterator iterator = set.iterator();
    while(iterator.hasNext()){
        System.out.println(iterator.next());
    }
      Map map = tea.getMap();//取出Map中的鍵和值
      Iterator iterator2 = map.keySet().iterator();
      while(iterator2.hasNext()){
          Object next = iterator2.next();//獲取鍵
          System.out.println(next+":"+map.get(next));//get獲取值
      }
      System.out.println(map);

五種類型通知:
1、前置通知[Before advice]:在連接點(diǎn)前面執(zhí)行,前置通知不會影響連接點(diǎn)的執(zhí)行,除非此處拋出異常。
3、正常返回通知[After returning advice]:在連接點(diǎn)正常執(zhí)行完成后執(zhí)行,如果連接點(diǎn)拋出異常,則不會執(zhí)行。
3、異常返回通知[After throwing advice]:在連接點(diǎn)拋出異常后執(zhí)行。
4、返回通知[After (finally) advice]:在連接點(diǎn)執(zhí)行完成后執(zhí)行,不管是正常執(zhí)行完成,還是拋出異常,都會執(zhí)行返回通知中的內(nèi)容。
5、環(huán)繞通知[Around advice]:環(huán)繞通知圍繞在連接點(diǎn)前后,比如一個方法調(diào)用的前后。這是最強(qiáng)大的通知類型,能在方法調(diào)用前后自定義一些操作。環(huán)繞通知還需要負(fù)責(zé)決定是繼續(xù)處理join point(調(diào)用ProceedingJoinPoint的proceed方法)還是中斷執(zhí)行。

aop和oop區(qū)別:
1、oop是java在面向?qū)ο缶幊?br> aop是面向切面編程,AOP主要應(yīng)用于日志記錄,性能統(tǒng)計,安全控制,事務(wù)處理等方面,它是為程序員解耦而生.

aop全注解開發(fā):
注解名 說明
@Controller 注解控制層組件,(如struts中的action)
@Service 注解業(yè)務(wù)層組件,service層組件
@Repository 注解數(shù)據(jù)訪問層組件,DAO層組件
@Component 泛指組件,當(dāng)組件不好歸類的時候,我們可以使用這個注解進(jìn)行標(biāo)注
@Autowired 默認(rèn)是按照類型裝配注入(spring)
@Resource 默認(rèn)是按照名稱來裝配注入(j2ee)
@Scope 注解用于指定scope作用域的(用在類上)
@Transactional 添加事務(wù)

注解配置AOP,為三步
1)使用注解@Aspect來定義一個切面,在切面中定義切入點(diǎn)(@Pointcut),通知類型(@Before、 @After 、@AfterReturning 、@AfterThrowing 、@Around )
2)開發(fā)需要被攔截的類
3)將切面配置到xml中,也可以使用自動掃描bean方式
案例:
@Aspect// 使用spring 全注解定義一個切面
@Component("aop")
// 泛指組件當(dāng)組件不好歸類的時候,我們可以使用這個注解進(jìn)行標(biāo)注

public class AspInterceptor {
    @Before("execution(* com.hw.service..*.add*(com.hw.entity.Student))")
    public void before() {// 前置通知
        System.out.println("before....使用本程序先交9美金!!!");
    }

    @AfterReturning("execution(* com.hw.service.impl.*.add*(String))")
    public void after() {// 正常返回通知
        System.out.println("after....程序使用正常9美金很值!!!");
    }

    @AfterThrowing("execution(* com.hw.service.impl.*.*(..))")
    public void afterthrowing() {// 異常返回通知
        System.out.println("throwing....程序使用有異常9美金上當(dāng)了!!!");
    }

    @After("execution(* com.hw.service.impl.*.add*(..))")
    public void afterfinally() {// 返回最終通知
        System.out.println("afterfinally....別想著程序了,9美金已交了!!!");
    }

    @Around("execution(* com.hw.service.impl.*.update*(..))")
    public void around(ProceedingJoinPoint pj) throws Throwable {// 環(huán)繞通知
        System.out.println("環(huán)繞通知,要工作了");
        pj.proceed();
        System.out.println("環(huán)繞通知,要發(fā)工資了");
        /*
         * 環(huán)繞通知:能在方法調(diào)用前后自定義一些操作。環(huán)繞通知還需要負(fù) 
         * 責(zé)決定是繼續(xù)處理join point(調(diào)用ProceedingJoinPoint的
         * proceed方法)還是中斷執(zhí)行
         */
    }

spring中配置數(shù)據(jù)源的4中形式:
spring自帶的數(shù)據(jù)源(DriverManagerDataSource),DBCP數(shù)據(jù)源,C3P0數(shù)據(jù)源,JNDI數(shù)據(jù)源。

實(shí)際開發(fā)中一般使用
1 DBCP數(shù)據(jù)源(http://www.cnblogs.com/adolfmc/archive/2013/01/22/2872298.html)
DBCP的配置依賴于2個jar包c(diǎn)ommons-dbcp.jar,commons-pool.jar

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url"
            value="jdbc:mysql://localhost:3306/cc6?useUnicode=true&characterEncoding=utf8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root" />
        <!-- 連接池啟動時的初始值 -->
        <property name="initialSize" value="3" />
        <!-- 連接池的最大值 -->
        <property name="maxActive" value="300" />
        <!-- 最大空閑值.當(dāng)經(jīng)過一個高峰時間后,連接池可以慢慢將已經(jīng)用不到的連接慢慢釋放一部分,一直減少到maxIdle為止 -->
        <property name="maxIdle" value="2" />
        <!-- 最小空閑值.當(dāng)空閑的連接數(shù)少于閥值時,連接池就會預(yù)申請去一些連接,以免洪峰來時來不及申請 -->
        <property name="minIdle" value="1" />
    </bean>

2 C3P0是一個開放源代碼的JDBC數(shù)據(jù)源實(shí)現(xiàn)項目,C3P0依賴于jar包c(diǎn)3p0.jar

<bean id="dataSource" class=""com.mchange.v2.c3p0.ComboPooledDataSource">
       <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
       <property name="url" value="jdbc:mysql:/localhost:3306/cc2?useUnicode=true&characterEncoding=utf8"></property>
       <property name="username" value="root"></property>
       <property name="password" value="root"></property>
   </bean>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容