国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
spring2.5 注解
spring2.5 注解
1.準(zhǔn)備工作
(1)導(dǎo)入common-annotations.jar
(2)導(dǎo)入schema文件 文件名為spring-context-2.5.xsd
(3)在xml的beans節(jié)點(diǎn)中配置
<?xml version="1.0" encoding="UTF-8"?>
<beans 
       .......
       xmlns:context="http:/
1.準(zhǔn)備工作
(1)導(dǎo)入common-annotations.jar
(2)導(dǎo)入schema文件 文件名為spring-context-2.5.xsd
(3)在xml的beans節(jié)點(diǎn)中配置
<?xml version="1.0" encoding="UTF-8"?>
<beans 
       .......
       xmlns:context="
       xsi:schemaLocation="
           .......
          
http://www.springframework.org/schema/context
          
.....
   <!--將針對注解的處理器配置好  -->
  <context:annotation-config />
.....
<beans>
2.在java代碼中使用@Autowired或@Resource注解方式進(jìn)行裝配 ,這兩個(gè)注解的區(qū)別是:
@Autowired默認(rèn)按類型裝配,@Resource默認(rèn)按名稱裝配,當(dāng)找不到名稱匹配的bean才會按類型裝配。
默認(rèn)注解
@Resource  private PersonDao persondao;
<bean id="personDao" class="com.hf.dao.impl.PersonDaoBean"></bean>
首先是判斷persondao是否與xml里的personDao名字相同,相同則注入,
不同則判斷persondao是否是com.hf.dao.impl.PersonDaoBean類型,是則注入不是則返回null.
@Resource(name="personDao")  private PersonDao dao;
<bean id="personDao" class="com.hf.dao.impl.PersonDaoBean"></bean>
判斷name名稱是否與bean中id相同不同則返回null
Spring自帶注解方式
@Autowired @Qualifier("personDao") private PersonDao persondao;
默認(rèn)是按類型注入  加上@Qualifier("personDao")則按名稱注入
3.通過在classpath 自動掃描方式把組件納入spring容器中管理
spring2.5為我們引入了組件自動掃描機(jī)制,它可以在類路徑底下尋找標(biāo)注了@Component @Service @Controller @Repository
注解的類,并將這些類納入進(jìn)spring容器中管理。它們的作用和xml文件中使用bean 節(jié)點(diǎn)配飾組件是一樣的。
(1)使用到了注解的功能(需要注解準(zhǔn)備工作的內(nèi)容)
(2)在xml中加入
<context:component-scan base-package="com.hf" />
其中base-package 為需要掃描的包(包含子包)
(3)
@Service用于標(biāo)注業(yè)務(wù)層組件
@Controller用于標(biāo)注控制層組件(如struts中的action)
@Repository用于標(biāo)注數(shù)據(jù)訪問組件 ,即DAO 組件
@Component泛指組件,當(dāng)組件不好歸類的時(shí)候,我們可以使用這個(gè)注解進(jìn)行標(biāo)注。
(4)
業(yè)務(wù)類
@Service
public class PersonServiceBean implements PersonService {.....}
輸出類
AbstractApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService= (PersonService)cxt.getBean("personServiceBean");
System.out.println(personService);
cxt.close();
使用注解中bean的id默認(rèn)名稱為類名稱的首字母小寫名稱
--------------------------------------------------
自己指定名稱
@Service("aa") //默認(rèn)作用域范圍 是單例范圍
public class PersonServiceBean implements PersonService {.....}
輸出類
AbstractApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService= (PersonService)cxt.getBean("aa");
System.out.println(personService);
cxt.close();
--------------------------------------------------
@Service("aa") @Scope("prototype")//修改bean的作用域
public class PersonServiceBean implements PersonService {....}
-----------------------------------------------------------
   @PostConstruct
   public void init(){ 
    System.out.println("初始化");
   }
   @PreDestroy
   public void destory(){  
    System.out.println("釋放資源");
   }
4.AOP注解方式
(1)準(zhǔn)備工作:
.導(dǎo)入common-annotations.jar  aspectjrt.jar aspectweaver.jar cglib-nodep-2.13.jar
.導(dǎo)入schema文件 文件名為spring-aop-2.0.xsd
.在xml的beans節(jié)點(diǎn)中配置
<?xml version="1.0" encoding="UTF-8"?>
<beans 
       .......
       xmlns:aop="
       xsi:schemaLocation="
           .......
          
http://www.springframework.org/schema/aop
          
.....
    <!-- 配置解釋處理器 為@AspectJ注解提供支持  -->
  <aop:aspectj-autoproxy />
.....
<beans>
(2)
<bean id="myInterceptor" class="com.hf.service.impl.MyInterceptor"></bean>
<bean id="personService" class="com.hf.service.impl.PersonServiceBean" ></bean>
將切面和被攔截的類交給spring管理
(3)切面類
@Aspect //定義切面類
public class MyInterceptor {
   /**
    *  @Pointcut("execution(* com.hf.service..*.*(..))")表達(dá)式含義
    * 第一個(gè)* 表示返回值類型為任意類型
    * com.hf.service..  兩個(gè)點(diǎn)表示包路徑下的子包的類也要攔截
    * com.hf.service..*.* 子包的所有類中的所有方法 第一個(gè)*是方法第二個(gè)*是類
    * (..)代表方法參數(shù)隨意 可有可無可多可少
    * **/
   @Pointcut("execution (* com.hf.service.impl.PersonServiceBean.*(..))")// 定義切入點(diǎn)
   private void andMethod()//聲明一個(gè)切入點(diǎn)
   {}
  
/*   @Before("andMethod()")
   public void doAccessCheck(){
    System.out.println("前置通知");   
   }
   */
  
   @Before("andMethod() && args(name)") //帶參數(shù) 只攔截符合參數(shù)類型的方法
   public void doAccessCheck(String name){
    System.out.println("前置通知"+name);   
   }
  
/*   @AfterReturning("andMethod()")
   public void doFaterReturning(){   
    System.out.println("后置通知");
   }*/
  
   @AfterReturning(pointcut="andMethod()",returning="result")//帶返回值的 無返回值的方法 result為null
   public void doFaterReturning(String result){//攔截方法執(zhí)行后 獲取返回值對象
    System.out.println("后置通知:"+result);
   }
  
  @After("andMethod()")
   public void doAfter(){
    System.out.println("最終通知"); 
   }
  
/*   @AfterThrowing("andMethod()")
   public void doAfterThrowing(){   
    System.out.println("例外通知");
   }*/
  
   @AfterThrowing(pointcut="andMethod()" , throwing="e") //獲取例外并打印
   public void doAfterThrowing(Exception e){  
    System.out.println("例外通知:"+e);
   }
   @Around("andMethod()")//環(huán)繞通知
   public Object doBasecProfiling(ProceedingJoinPoint pjp )throws Throwable{
   //if(){//判斷是否與權(quán)限
    System.out.println("進(jìn)入通知");
    Object result = pjp.proceed();
    System.out.println("離開 通知");
   //}
    return result;
   }
}
(4)業(yè)務(wù)類 PersonServiceBean
public class PersonServiceBean implements PersonService {
 public void save(String name){
      throw new RuntimeException("純屬例外");
      // System.out.println("我是Save方法"+name);
    }
 public String update() { 
  return "我是update方法";
 }
}
 
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服