使用AOP时出现空指针异常

时间:2013-08-28 11:28:13

标签: spring-mvc aspectj resteasy

应用程序上下文文件

 
    <!-- Aspect -->
    <!-- It allow @Component, @Service, @Controller, etc.. annotations -->
    <context:component-scan base-package="com.piyush.cns.*" />
    <aop:aspectj-autoproxy/>


    <!-- It allow us to use @Autowire, @Required and @Qualifier annotations -->
    <context:annotation-config />

    <!-- Logger bean -->
    <bean id="logger" class="org.slf4j.LoggerFactory" factory-method="getLogger">
        <constructor-arg type="java.lang.String" value="com.piyush.cns" />
    </bean>

    <!-- Aspect -->
    <bean id="logAspect" class="com.piyush.cns.customer.resource.AllAspects" /> 
</beans>

上面的代码是包含所有bean声明的应用程序上下文文件。

控制器类CustomersResource。该课程最初将收到请求。基本要求是从服务器端验证客户对象。

@Controller
@Path("/customers")
public class CustomersResource implements Serializable {

    private static final long serialVersionUID = 1L;

    @Autowired
    ICustomerService customerService;

    @Autowired
    IPartnerService partnerService;

    @Autowired
    ResourceHelper resourceHelper;

    @Autowired
    private Logger logger;

    /**
     * @Request POST
     * @param uriInfo - To get Absolute path
     * @param Customer - JSON Request of Customer to add
     * @return Response with created customer Payload.  Also includes location header
     * @throws TCException - If customer Name already exist
     * @Description REST service to add Customer sent as JSON
     */
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response addCustomer(@Context UriInfo uriInfo, Customer customer) throws TCException {
        logger.info("Entering addCustomer for customer " + customer.getCustomerName());
        logger.debug("customer Desc \t " + customer.getDescription());

        MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
        String partnerId = queryParams.getFirst("partnerId");
        logger.info("Partner ID from Query Param : " + partnerId);
        // If partnerId : Null, Customer type is direct ie, not associated with any partner. implies customer type = 0
        // [Direct]
        if (null == partnerId) {
            // Add DIRECT Customer
            customer.setCustomerType(0);
            logger.debug("Customer type set to : DIRECT");
        } else {
            // ADD INDIRECT Customer
            // Set customer type, partner
            customer.setCustomerType(1);
            logger.debug("Customer type set to : INDIRECT");
            // Check if partner exist
            Partner partner = partnerService.getPartnerById(Long.parseLong(partnerId));
            if (null == partner) {
                logger.error("EntityResourceNotFoundException. Partner Resource not found for ID : " + partnerId);
                throw new EntityResourceNotFoundException("", "Partner Resource with ID : " + partnerId
                    + " not found to add customer");
            }
            customer.setPartner(partner);
            logger.debug("Customer set to Partner : " + partnerId);
        }
        // Save Customer
        customer = customerService.addCustomer(customer);

        // Creating location header
        UriBuilder builder = uriInfo.getAbsolutePathBuilder();
        URI uri = builder.path(String.valueOf(customer.getId())).build();
        logger.info("Exiting addCustomer for customer " + customer.getCustomerName());
        return Response
            .created(URI.create(uri.toString()))
            .entity(
                resourceHelper.buildResponse(customer, Status.CREATED.toString(),
                    "Customer Created: " + customer.getCustomerName())).build();
    }



Aspect class AllAspects

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

@Aspect
public class AllAspects {

    @Before("execution(* com.piyush.cns.customer.resource.CustomersResource.addCustomer(..))")
    public void logBefore(JoinPoint joinPoint) {

        System.out.println("logBefore() is running!");
        System.out.println("******");
    }
}




My problem is the control flow is not going inside the method "logBefore(...)"of class AllAspects and also in class CustomersResource  at the method addCustomers(...) logger is coming as null.


Please help me get out of this issue.

0 个答案:

没有答案
相关问题