Explore Spring 4 Features - Part 2
Just Starting from the point where I left in the last POST
The content of aspect-config.xml is:
The noteworthy thing here is that the Aspect is being injected with Spring defined bean of type DataInjectObject which
is being configured in SpringWebApplicationConfig Configuration class (Quite Interesting Huh!!!!).
In SpringWebApplicationConfig
1) Custom WebArgumentResolvers
2) Custom Formatters (One for Address Type and another for Formatting Date Type) is being introduced.
a) Date Formatter is Annotation Based. The Custom Annotation used here is: CustomDateFormat.
Its definition is:
Annotation type CustomDateFormat
Its Definition is:
CustomDateFormatter implementation which is merely another Custom Formatter implementation.
Its definition is:
3) viewResolver is being registered.
4) multipartResolver is being registered for form-data/multipart type.
The SpringSecurityApplicationConfig file which corresponds to spring-security.xml configuration
file has some basic spring security configurations.
1) Configuration of In-Memory Authentication (For Users).
2) Configuring http basic authentication for URL's ,configuring default SPRING-SECURITY login page,
along with Custom AccessDeniedHandler for ExceptionHandling.
3) A Custom Filter CustomExceptionTranslationFilter is configured before Spring ExceptionTranslationFilter
in Spring Security Filter Chain. The Definition of CustomExceptionTranslationFilter is:
This is some idea in short about Spring Java Config.
The next post is going to be regarding some interesting features of Spring 4.
The content of aspect-config.xml is:
Here MyAspect is a AspectJ class:
public aspect MyAspect { private DataInjectObject injectedData; public DataInjectObject getInjectedData() { return injectedData; } public void setInjectedData(DataInjectObject injectedData) { this.injectedData = injectedData; } pointcut incept1() : execution(* com.springjavaconfig.controller.BaseControllerNew.secureMethodNew(..,DataInjectObject)); pointcut incept2() : execution(* com.springjavaconfig.controller.BaseController.getEmployeeForm()); Object around() :incept2() { System.out.println("################################## \n\n"); System.out.println("####### ASPECTS IN ACTION ####### \n\n"); System.out.println("################################## \n\n"); System.out.println(" ****** Injected DATA:"+injectedData); return proceed(); } Object around(DataInjectObject dataInjectObject) : incept1() && args(..,dataInjectObject) { System.out.println("################################## \n\n"); System.out.println("####### ASPECTS ####### \n\n"); System.out.println("################################## \n\n"); System.out.println(" @@@ Inspecting This and Target DataInjectObject: "+dataInjectObject+"\n injectedData:"+injectedData); System.out.println("####\n\n Injected Data:"+injectedData); if("Subhankar".equals(injectedData)) { System.out.println("####### PROCEEDING WITH METHOD EXECUTION ####### \n\n"); return proceed(dataInjectObject); } else { System.out.println("####### SUSPENDING METHOD EXECUTION ####### \n\n"); return proceed(dataInjectObject); } } }Like other AspectJ functional classes, It too contains pointcuts,advice (Around advice) and Method Joinpoint is being used.
The noteworthy thing here is that the Aspect is being injected with Spring defined bean of type DataInjectObject which
is being configured in SpringWebApplicationConfig Configuration class (Quite Interesting Huh!!!!).
In SpringWebApplicationConfig
1) Custom WebArgumentResolvers
2) Custom Formatters (One for Address Type and another for Formatting Date Type) is being introduced.
a) Date Formatter is Annotation Based. The Custom Annotation used here is: CustomDateFormat.
Its definition is:
@Target(value=ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface CustomDateFormat { }So AnnotationFormatterFactory implementation is being used for formatting the Date Type annotated with Custom
Annotation type CustomDateFormat
Its Definition is:
public class CustomDateAnnotationFormatterFactory implements AnnotationFormatterFactoryFrom the above definition we can see that the AnnotationFormatterFactory implementation uses{ public Set > getFieldTypes() { // TODO Auto-generated method stub System.out.println("$$$$ In GetFields Method FOR DATE FORMATTER $$$$ "); Set > set = new HashSet >(); set.add(Date.class); return set; } public Parser getParser(CustomDateFormat customDateFormat, Class class1) { System.out.println("$$$$ In getParser Method $$$$ \n"+class1); // TODO Auto-generated method stub return new CustomDateFormatter(); } public Printer getPrinter(CustomDateFormat customDateFormat, Class class1) { System.out.println("$$$$ In getPrinter Method $$$$ \n"+class1); // TODO Auto-generated method stub return new CustomDateFormatter(); } }
CustomDateFormatter implementation which is merely another Custom Formatter implementation.
Its definition is:
public class CustomDateFormatter implements Formatterb) The AddressFormatter is merely another formatter just like CustomDateFormatter{ private String pattern; public CustomDateFormatter() { this.pattern = "dd/MM/yyyy"; } public String print(Date date, Locale locale) { System.out.println(" ***** In Print Method **** "); if (date == null) { return ""; } this.pattern = "dd/MM/yyyy"; return getDateFormat().format(date); } public Date parse(String formatted, Locale locale) throws ParseException { System.out.println(" ***** In Parse Method **** "); System.out.println("formatted:"+formatted+"\n locale:"+locale.getCountry()); if (formatted.length() == 0) { return null; } return getDateFormat().parse(formatted); } protected DateFormat getDateFormat() { System.out.println(" ***** In getDateFormat Method **** "); DateFormat dateFormat = new SimpleDateFormat(this.pattern); dateFormat.setLenient(false); return dateFormat; } }
3) viewResolver is being registered.
4) multipartResolver is being registered for form-data/multipart type.
The SpringSecurityApplicationConfig file which corresponds to spring-security.xml configuration
file has some basic spring security configurations.
1) Configuration of In-Memory Authentication (For Users).
2) Configuring http basic authentication for URL's ,configuring default SPRING-SECURITY login page,
along with Custom AccessDeniedHandler for ExceptionHandling.
3) A Custom Filter CustomExceptionTranslationFilter is configured before Spring ExceptionTranslationFilter
in Spring Security Filter Chain. The Definition of CustomExceptionTranslationFilter is:
public class CustomExceptionTranslationFilter extends ExceptionTranslationFilter { private AuthenticationTrustResolver authenticationTrustResolver = new AuthenticationTrustResolverImpl(); private AccessDeniedHandler accessDeniedHandler = new AccessDeniedHandlerImpl(); public CustomExceptionTranslationFilter(AuthenticationEntryPoint authenticationEntryPoint) { super(authenticationEntryPoint); } private void handleException(HttpServletRequest request, HttpServletResponse response, FilterChain chain, RuntimeException exception) throws IOException, ServletException { if ((exception instanceof AuthenticationException)) { System.out.println("Authentication exception occurred; redirecting to authentication entry point"+ exception); sendStartAuthentication(request, response, chain, (AuthenticationException)exception); } else if ((exception instanceof AccessDeniedException)) { System.out.println(" **** In AccessDeniedException **** "); if (this.authenticationTrustResolver.isAnonymous(SecurityContextHolder.getContext().getAuthentication())) { System.out.println("Access is denied (user is anonymous); redirecting to authentication entry point"+ exception); sendStartAuthentication(request, response, chain, new InsufficientAuthenticationException("Full authentication is required to access this resource")); } else { System.out.println("Access is denied (user is not anonymous); delegating to AccessDeniedHandler"+ exception); } this.accessDeniedHandler.handle(request, response, (AccessDeniedException)exception); } } }The CustomExceptionTranslationFilter used Spring LoginUrlAuthenticationEntryPoint
This is some idea in short about Spring Java Config.
The next post is going to be regarding some interesting features of Spring 4.
Comments
Post a Comment