アプリケーション コンテキスト ファイル
<!-- 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.