spring(千鋒逆戰(zhàn))

手動添加webapp包,包下建立WEB-INF,及建立web.xml文件即可正常使用;添加tomcat的依賴,找到tomcat的run方法即可運行web項目;


添加spring的依賴

spring-context:? ? ? 添加完成后自動導入6個相關的依賴,在pom.xml中 添加如下圖的依賴

如圖

<bean id="girl "? class="(類名,自動導入全包名)"? scope=" 默認值 singlrton ">

<property? ?name="? "? value="? ?"? ? />使用無參的構(gòu)造器

<constructor-arg name="? " value=" " />使用有參的構(gòu)造器

<property? ?name="? "? ref="? ?"? ? />對象的導入用

有參構(gòu)造方法name不寫,默認重上倒下的順序,也可以使用index="? "比較 0開始,? type="? "類型標記

</bean>

ApplicationContext ac =new ClassPathXmlApplicationContext("bean.xml");讀取這個文件

ac.getBean("b1");得到項目的數(shù)據(jù)

spring 添加 P 屬性? 點擊 namespace 勾選 p 屬性,勾選上之后再xml中就會多一條p屬性的連接;

<bean? ?id="? "? class="? "? p:成員變量="? "? ></bean> 添加變量;

<bean? ?id="? "? class="? "? p:成員變量-ref="? "? ></bean> 添加對象;

ac.close;最后要關閉資源


java代理

代理對象存在的價值主要用于攔截對真實業(yè)務對象的訪問.代理對象應該具有和目標對象相同的方法(真實業(yè)務對象).


靜態(tài)代理實現(xiàn)原理


第一種創(chuàng)建方式

public class UserFactory {

public static IUserServicegetUserService(){

IUserService us =new UserservcieImpl();

? ? ? ? MyAspect ma =new MyAspect();

? ? ? ? IUserService ius = (IUserService) Proxy.newProxyInstance(UserFactory.class.getClassLoader(), us.getClass().getInterfaces(), new InvocationHandler() {

@Override

? ? ? ? ? ? public Objectinvoke(Object proxy, Method method, Object[] args)throws Throwable {

ma.before();

? ? ? ? ? ? ? ? Object obj = method.invoke(us, args);

? ? ? ? ? ? ? ? ma.after();

? ? ? ? ? ? ? ? return obj;

? ? ? ? ? ? }

});

? ? ? ? return ius;

? ? }

}

第二種創(chuàng)建方式

使用Spring中的一個增強類來實現(xiàn)aop方式

? ? *? ? ? 1.? 創(chuàng)建Enhancer對象

? ? *? ? ? 2.? 設置增強類Enhancer的superClass

? ? *? ? ? 3.? 設置Enhancer對象的回調(diào)

? ? *? ? ? 4.? 通過eh對象的create()方法來得到指定的對象

public class UserFactory {

public static IUserServicegetUserService() {

Enhancer? en =new Enhancer();??//創(chuàng)建Enhancer對象

? ? ? ? MyAspect my =new MyAspect();

? ? ? ? en.setSuperclass(lianxi2.IUserService.class);//設置增強類Enhancer的superClass

? ? ? ? en.setCallback(new MethodInterceptor() {//設置Enhancer對象的回調(diào)

public Objectintercept(Object o, Method method, Object[] objects, MethodProxy methodProxy)throws Throwable {

my.before();

? ? ? ? ? ? ? Object obj = method.invoke(objects);

? ? ? ? ? ? ? my.after();

return null;

? ? ? ? ? ? }

});

? ? ? ? IUserService ius= (IUserService) en.create();//通過eh對象的create()方法來得到指定的對象

? ? ? ? return? ius;

? ? }

第三種實現(xiàn)方式

beans.xml配置 (要放在resources資源下):

<?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="us" class="com.qfedu.aop03.UserServiceImpl" />

? ? <bean id="my" class="com.qfedu.aop03.MyAspect" />

? ? <!--

? ? ? ? ProxyFactoryBean代理的FactoryBean對象,我們現(xiàn)在要代理的是us

? ? ? ? ? ? 包含四個屬性注入:

? ? ? ? ? ? ? ? 1.? interfaces: 接口對象們

? ? ? ? ? ? ? ? ? ? <list>

? ? ? ? ? ? ? ? ? ? ? ? <value>com.qfedu.aop03.IUserService</value>

? ? ? ? ? ? ? ? ? ? ? ? <value>com.qfedu.aop03.IUserService</value>

? ? ? ? ? ? ? ? ? ? ? ? <value>com.qfedu.aop03.IUserService</value>

? ? ? ? ? ? ? ? ? ? </list>

? ? ? ? ? ? ? ? 2.? target:目標對象,哪個對象將被以代理的方式創(chuàng)建

? ? ? ? ? ? ? ? 3.? interceptorNames:攔截對象的名稱,自定義的MethodInterceptor對象,注意它的包結(jié)構(gòu)組成

? ? ? ? ? ? ? ? 4.? optimize:boolean類型的值:

? ? ? ? ? ? ? ? ? ? ? ? true:強制使用cglib的動態(tài)代理方式

? ? ? ? ? ? ? ? ? ? ? ? false:使用jdk自帶的動態(tài)代理

? ? ? ? ? ? ? ? ? ? ? ? cglib:code generation library,代碼生成庫,性能更高

? ? -->

? ? <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">

? ? ? ? <property name="interfaces" value="com.qfedu.aop03.IUserService" />

? ? ? ? <property name="target" ref="us" />

? ? ? ? <property name="interceptorNames" value="my" />

? ? ? ? <property name="optimize" value="true" />

? ? </bean>

</beans>

Myaspect


import org.aopalliance.intercept.MethodInterceptor;

import org.aopalliance.intercept.MethodInvocation;

public class MyAspect implements MethodInterceptor {

? ? private void before(){

? ? ? ? System.out.println("---------before----------");

? ? }

? ? private void after(){

? ? ? ? System.out.println("---------after----------");

? ? }

? ? @Override

? ? public Object invoke(MethodInvocation invocation) throws Throwable {

? ? ? ? before();

? ? ? ? //? 業(yè)務處理方法的調(diào)用

? ? ? ? Object obj = invocation.proceed();

? ? ? ? after();

? ? ? ? return obj;

? ? }

}

測試類:

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAOP03 {

? ? @Test

? ? public void testAOP03(){

? ? ? ? ApplicationContext ac = new ClassPathXmlApplicationContext("com/qfedu/aop03/beans.xml");

? ? ? ? IUserService us = ac.getBean("proxy", IUserService.class);

? ? ? ? Object o = new Object();

? ? ? ? System.out.println(us.getAllUser());

? ? ? ? System.out.println(us.deleteUser(1));

? ? ? ? System.out.println(us.saveUser(o));

? ? ? ? System.out.println(us.updateUser(o));

? ? }

}

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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