Spring(17)Springブロッキング


この例はSpring(十六)という文章を結びつけると、実際にSpring(十六)が書いているのもブロックで、AOPのカットプログラミング向けの知識点だと思います.


   SSH   spring   ,        spring                    ,

           ,            .

Purview  .

package aop; 

public interface Purview { 
    void checkLogin(); 
}
PurviesImpl ,Purview      .

package aop; 

public class PurviewImpl implements Purview { 

    public void checkLogin() { 
        System.out.println("This is checkLogin method!"); 
    } 
} 

/**
* PurviewAdvice ,    ,    MethodBeforeAdvice, *AfterReturningAdvice, ThrowsAdvice  ,     MethodBeforeAdvice  
*/
package aop; 

import java.lang.reflect.Method; 
import org.springframework.aop.MethodBeforeAdvice; 

public class PurviewAdvice implements MethodBeforeAdvice { 
    public void before(Method arg0, Object[] arg1, Object arg2) 
            throws Throwable { 
        System.out.println("This is before method!"); 
    } 
} 

//Test ,   .

package aop; 

import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class Test { 
    public static void main(String[] args) { 
        // TODO          
        ApplicationContext ctx = new ClassPathXmlApplicationContext( 
                "applicationContext.xml"); 
        PurviewImpl purviewImpl = (PurviewImpl) ctx.getBean("purviewImpl"); 
        purviewImpl.checkLogin(); 

    } 

} 

applicationContext.xml    .

    <bean id="purviewImpl" class="aop.PurviewImpl"></bean> 
    <bean id="purviewAdvice" class="aop.PurviewAdvice"></bean> 
    <bean id="purviewAdvisor" 
        class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> 
        <property name="advice"> 
            <ref local="purviewAdvice" /> 
        </property> 
        <property name="patterns"> 
            <list> 
                <value>.*checkLogin.*</value> 
            </list> 
        </property> 
    </bean> 

    <bean id="autoproxyaop" 
        class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> 
        <property name="beanNames"> 
           <value>purviewImpl</value> //     javaBean

        </property> 
        <property name="interceptorNames"> 
            <list> 
                  <value>purviewAdvisor</value>//     ,         

            </list> 
        </property> 
    </bean>
      ,     :

Exception in thread "main" java.lang.ClassCastException: $Proxy1
 at aop.Test.main(Test.java:34)

 Test  

PurviewImpl purviewImpl = (PurviewImpl) ctx.getBean("purviewImpl");

   

Purview purviewImpl = (Purview) ctx.getBean("purviewImpl");

       :

This is before method!
This is checkLogin method!

         !

  : spring  bean   ,        ,           ,

                  ,        .