JAVAEE——spring02:使用註解配置spring、sts外掛程式、junit整合測試和aop示範,javaeespring02

來源:互聯網
上載者:User

JAVAEE——spring02:使用註解配置spring、sts外掛程式、junit整合測試和aop示範,javaeespring02
一、使用註解配置spring  1.步驟

   1.1 導包4+2+spring-aop

   4代表:

    

   2代表:

   日誌包:com.springsource.org.apache.commons.logging-1.1.1.jar

   可選:com.springsource.org.apache.log4j-1.2.15.jar(老版本要匯入的,匯入可以保證一定能運行)

   1.2 為主設定檔引入新的命名空間(約束)

   1.3 開啟使用註解代替設定檔

   1.4 在類中使用註解完成配置

  2.將對象註冊到容器
//<bean name="user" class="cn.itcast.bean.User"  />//@Component("user")//    @Service("user") // service層//    @Controller("user") // web層    @Repository("user")// dao層
  3.修改對象的作用範圍
//指定對象的作用範圍@Scope(scopeName="singleton")
  4.實值型別注入

   通過反射的Field賦值,破壞了封裝性:

    @Value("tom")        private String name;

   通過set方法賦值,推薦使用.:

    @Value("tom")        public void setName(String name) {        this.name = name;    }
  5.參考型別注入
    //@Autowired //自動裝配    //問題:如果匹配多個類型一致的對象.將無法選擇具體注入哪一個對象.    //@Qualifier("car2")//使用@Qualifier註解告訴spring容器自動裝配哪個名稱的對象    private Car car;

   推薦方式:

    @Resource(name="car")//手動注入,指定注入哪個名稱的對象    private Car car;
  6.初始化|銷毀方法
    @PostConstruct //在對象被建立後調用.init-method    public void init(){        System.out.println("我是初始化方法!");    }    @PreDestroy //在銷毀之前調用.destory-method    public void destory(){        System.out.println("我是銷毀方法!");    }
 二、STS外掛程式  1.手動安裝外掛程式(成功率低)

  

   步驟1:

  

   步驟2:

  

   步驟3:

  

  2.直接使用spring裝好外掛程式的eclipse

   

 

三、spring與junit整合測試  1.導包4+2+aop+test  2.配置註解
//幫我們建立容器@RunWith(SpringJUnit4ClassRunner.class)//指定建立容器時使用哪個設定檔@ContextConfiguration("classpath:applicationContext.xml")public class Demo {    //將名為user的對象注入到u變數中    @Resource(name="user")    private User u;
  3.測試
    @Test    public void fun1(){                System.out.println(u);            }

 

四、spring中的aop  1.aop思想介紹

  2.spring中的aop概念

  3.spring實現aop的原理

    3.1 動態代理(優先)

   被代理對象必須要實現介面,才能產生代理對象.如果沒有介面將不能使用動態代理技術

   3.2 cglib代理(沒有介面)

   第三方代理技術,cglib代理.可以對任何類組建代理程式.代理的原理是對目標對象進行繼承代理. 如果目標對象被final修飾.那麼該類無法被cglib代理.

  4.aop名詞學習

  

 

五、spring中的aop示範  1.步驟(xml配置)   1.1 導包4+2

   spring的aop包:

    spring-aspects-4.2.4.RELEASE.jar

    spring-aop-4.2.4.RELEASE.jar

   spring需要第三方aop包:

    com.springsource.org.aopalliance-1.0.0.jar

    com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

   1.2 準備目標對象
public class UserServiceImpl implements UserService {    @Override    public void save() {        System.out.println("儲存使用者!");        //int i = 1/0;    }    @Override    public void delete() {        System.out.println("刪除使用者!");    }    @Override    public void update() {        System.out.println("更新使用者!");    }    @Override    public void find() {        System.out.println("尋找使用者!");    }}
   1.3 準備通知
//通知類public class MyAdvice {        //前置通知    //        |-目標方法運行之前調用    //後置通知(如果出現異常不會調用)//        |-在目標方法運行之後調用    //環繞通知//        |-在目標方法之前和之後都調用    //異常攔截通知//        |-如果出現異常,就會調用    //後置通知(無論是否出現 異常都會調用)//        |-在目標方法運行之後調用//----------------------------------------------------------------    //前置通知    public void before(){        System.out.println("這是前置通知!!");    }    //後置通知    public void afterReturning(){        System.out.println("這是後置通知(如果出現異常不會調用)!!");    }    //環繞通知    public Object around(ProceedingJoinPoint pjp) throws Throwable {        System.out.println("這是環繞通知之前的部分!!");        Object proceed = pjp.proceed();//調用目標方法        System.out.println("這是環繞通知之後的部分!!");        return proceed;    }    //異常通知    public void afterException(){        System.out.println("出事啦!出現異常了!!");    }    //後置通知    public void after(){        System.out.println("這是後置通知(出現異常也會調用)!!");    }}
   1.4 配置進行織入,將通知織入目標對象中
<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "><!-- 準備工作: 匯入aop(約束)命名空間 --><!-- 1.配置目標對象 -->    <bean name="userService" class="cn.itcast.service.UserServiceImpl" ></bean><!-- 2.配置通知對象 -->    <bean name="myAdvice" class="cn.itcast.d_springaop.MyAdvice" ></bean><!-- 3.配置將通知織入目標對象 -->    <aop:config>        <!-- 配置切入點             public void cn.itcast.service.UserServiceImpl.save()             void cn.itcast.service.UserServiceImpl.save()            * cn.itcast.service.UserServiceImpl.save()            * cn.itcast.service.UserServiceImpl.*()                        * cn.itcast.service.*ServiceImpl.*(..)            * cn.itcast.service..*ServiceImpl.*(..)        -->        <aop:pointcut expression="execution(* cn.itcast.service.*ServiceImpl.*(..))" id="pc"/>        <aop:aspect ref="myAdvice" >            <!-- 指定名為before方法作為前置通知 -->            <aop:before method="before" pointcut-ref="pc" />            <!-- 後置 -->            <aop:after-returning method="afterReturning" pointcut-ref="pc" />            <!-- 環繞通知 -->            <aop:around method="around" pointcut-ref="pc" />            <!-- 異常攔截通知 -->            <aop:after-throwing method="afterException" pointcut-ref="pc"/>            <!-- 後置 -->            <aop:after method="after" pointcut-ref="pc"/>        </aop:aspect>    </aop:config></beans>
  2.步驟(註解配置)   前面的1,2,3步和xml配置一樣   2.4 配置進行織入,將通知織入目標對象中

   applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "><!-- 準備工作: 匯入aop(約束)命名空間 --><!-- 1.配置目標對象 -->    <bean name="userService" class="cn.itcast.service.UserServiceImpl" ></bean><!-- 2.配置通知對象 -->    <bean name="myAdvice" class="cn.itcast.e_annotationaop.MyAdvice" ></bean><!-- 3.開啟使用註解完成織入 -->    <aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>

   通知類:

//通知類@Aspect//表示該類是一個通知類public class MyAdvice {    @Pointcut("execution(* cn.itcast.service.*ServiceImpl.*(..))")    public void pc(){}    //前置通知    //指定該方法是前置通知,並制定切入點    @Before("MyAdvice.pc()")    public void before(){        System.out.println("這是前置通知!!");    }    //後置通知    @AfterReturning("execution(* cn.itcast.service.*ServiceImpl.*(..))")    public void afterReturning(){        System.out.println("這是後置通知(如果出現異常不會調用)!!");    }    //環繞通知    @Around("execution(* cn.itcast.service.*ServiceImpl.*(..))")    public Object around(ProceedingJoinPoint pjp) throws Throwable {        System.out.println("這是環繞通知之前的部分!!");        Object proceed = pjp.proceed();//調用目標方法        System.out.println("這是環繞通知之後的部分!!");        return proceed;    }    //異常通知    @AfterThrowing("execution(* cn.itcast.service.*ServiceImpl.*(..))")    public void afterException(){        System.out.println("出事啦!出現異常了!!");    }    //後置通知    @After("execution(* cn.itcast.service.*ServiceImpl.*(..))")    public void after(){        System.out.println("這是後置通知(出現異常也會調用)!!");    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.