The configuration, implementation applicationcontextaware and implementation applicationlistener__spring of spring slice

Source: Internet
Author: User
Tags aop
1. Configuration section (AOP) in the project, horizontal monitoring

Can look here: http://ntzrj513.blog.163.com/blog/static/27945612201362232315/

① Instance code (annotation-based approach):

@Component
@Aspect
@Order (1) Public
class Serviceloggingaspect {
    private final Logger Logger = Loggerfactory.getlogger (serviceloggingaspect.class);

    @Around ("Execution" (Public * com.raipeng.micro. service.*service.* (..)) ")
    Public Object Logaround (Proceedingjoinpoint joinpoint) throws Throwable {
        long starttime = System.currenttimemillis ();
        Object result = Joinpoint.proceed ();
        Long time = System.currenttimemillis ()-StartTime;
        Logger.info ("The method {}.{} () time Consuming {}ms ", Joinpoint.gettarget (). GetClass (). Getsimplename (), Joinpoint.getsignature (). GetName (), time) ;
        return result;
    }

Joinpoint can get to the current execution of methods, parameters, and so on, by the following methods:

Can look here: http://my.oschina.net/itblog/blog/211693

Joinpoint.getargs ()//gets
the parameter joinpoint.gettarget ()//Gets the target object that is being woven   
joinpoint.gettarget (). GetClass (). Getsimplename ()//Get the full name of the woven class
joinpoint.proceed ()//Get the execution result of the Weaving object and return
joinpoint.getsignature ()//Return the signature of the target method
joinpont.getsignature (). GetName ()//return the name of the target method





Configuration files in the ②spring

Scan Package

<context:component-scan base-package= "Com.raipeng.micro.enterprise.**.service"/>
<context: Component-scan base-package= "Com.raipeng.micro.core.support.aspect"/>

<aop:aspectj-autoproxy/>


2. Implement Applicationcontextaware interface, cover its Setapplicationcontext method

Can look here: http://blog.csdn.net/kaiwii/article/details/6872642

Objective: To get the beans in the spring container at random

Specific code:

Package com.raipeng.micro.core.utils;

Import org.springframework.beans.BeansException;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.ApplicationContextAware;

/**
 * Created on 2015/7/16.
 * *
 @author 
 * @version 1.0/Public
class Apputils implements Applicationcontextaware {
    private Static ApplicationContext ApplicationContext;

    @Override public
    void Setapplicationcontext (ApplicationContext applicationcontext) throws Beansexception {
        apputils.applicationcontext = ApplicationContext;
    }

    public static Object Getbean (String name) {return
        Applicationcontext.getbean (name);
    }

    public static <T> T Getbean (String name, class<t> clazz) {return
        Applicationcontext.getbean (name, clazz);
    }

Configuration:

Xml

Spring's own listener for loading the profile you need for yourself
<listener>
        <listener-class> org.springframework.web.context.contextloaderlistener</listener-class>
</listener>/
/ Specify the configuration file (read applicationcontext.xml files under Default path if not configured)

 <context-param>
     <param-name> Contextconfiglocation</param-name> 
       <param-value>classpath:applicationcontext.xml</ Param-value>
 </context-param>


Applicationcontext.xml

Registers the class used to get the bean (configurable in the SPRING-MVC configuration file)

<bean class= "Com.raipeng.micro.core.utils.AppUtils"/>

3. Implement Applicationlistener interface, overwriting its onapplicationevent (Contextrefreshedevent event) method

Can look here: http://yangyongbyjava.iteye.com/blog/1600581

Load two times problem: http://zhaoshijie.iteye.com/blog/1974682 or add a Boolean parameter, after execution becomes false/true


Instance code: Configuring the default path for static and dynamic resources

Package Com.raipeng.micro.core.web.listener;
Import Com.raipeng.micro.core.web.conf.WebConfig;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.ApplicationListener;
Import org.springframework.context.event.ContextRefreshedEvent;
Import org.springframework.stereotype.Component;

Import Org.springframework.web.context.WebApplicationContext;
Import Javax.annotation.Resource;

Import Javax.servlet.ServletContext;
 /** * Created on 2015/6/16. * * @author * @version 1.0/@Component public class Webapplicationlistener implements Applicationlistener<context
    refreshedevent> {private final Logger Logger = Loggerfactory.getlogger (Webapplicationlistener.class);

    Private Webconfig webconfig;
    @Resource public void Setwebconfig (Webconfig webconfig) {this.webconfig = Webconfig;
   @Override public void Onapplicationevent (Contextrefreshedevent event) {     ApplicationContext ApplicationContext = Event.getapplicationcontext ();
        Webapplicationcontext Webapplicationcontext = (webapplicationcontext) ApplicationContext;
        ServletContext ServletContext = Webapplicationcontext.getservletcontext ();
        Webconfig.setcontext (Servletcontext.getcontextpath ());
        Webconfig.setresource (Webconfig.getcontext () + "/public");
        Servletcontext.setattribute ("Webconfig", webconfig);
        Servletcontext.setattribute ("Context", webconfig.getcontext ());
        Servletcontext.setattribute ("Resource", Webconfig.getresource ());
        Servletcontext.setattribute ("revision", Webconfig.getrevision ()); Logger.debug ("context:{}, resource:{}, revision:{}", Webconfig.getcontext (), Webconfig.getresource (),
    Webconfig.getrevision ());
 }

Webconfig.java

Package com.raipeng.micro.core.web.conf;

Import Org.springframework.beans.factory.annotation.Value;
Import org.springframework.stereotype.Component;

/**
 * Created on 2015/6/16.
 * *
 @author 
 * @version 1.0
 /
@Component public
class Webconfig {
    private String context;// Web application Context
    private string resource;//static file
    private string revision;//css, JS version number public

    string GetContext ( ) {return context
        ;
    }

    public void SetContext (String context) {
        This.context = context;
    }

    Public String GetResource () {return
        resource;
    }

    public void Setresource (String Resource) {
        this.resource = resource;
    }

    Public String getrevision () {return
        revision;
    }

    @Value ("${config.git.revision}") Public
    void Setrevision (String revision) {
        this.revision = revision;
    }
}







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.