Annotation Based After Returning Advice ”; Previous Next @AfterReturning is an advice type, which ensures that an advice runs after the method executes successfully. Following is the syntax of @AfterReturning advice. Syntax @AfterReturning(Pointcut = “execution(* com.tutorialspoint.Student.*(..))”, returning = “retVal”) public void afterReturningAdvice(JoinPoint jp, Object retVal){ System.out.println(“Method Signature: ” + jp.getSignature()); System.out.println(“Returning:” + retVal.toString() ); } Where, @AfterReturning − Mark a function as an advice to be executed before method(s) covered by Pointcut, if the method returns successfully. Pointcut − Provides an expression to select a function execution( expression ) − Expression covering methods on which advice is to be applied. returning − Name of the variable to be returned. To understand the above-mentioned concepts related to @AfterReturning Advice, let us write an example, which will implement @AfterReturning Advice. To write our example with few advices, let us have a working Eclipse IDE in place and use the following steps to create a Spring application. Step Description 1 Update the project Student created under chapter Spring AOP – Application. 2 Update the bean configuration and run the application as explained below. Following is the content of Logging.java file. This is actually a sample of aspect module, which defines the methods to be called at various points. package com.tutorialspoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; @Aspect public class Logging { /** * This is the method which I would like to execute * after a selected method execution. */ @AfterReturning(Pointcut = “execution(* com.tutorialspoint.Student.*(..))”, returning = “retVal”) public void afterReturningAdvice(JoinPoint jp, Object retVal){ System.out.println(“Method Signature: ” + jp.getSignature()); System.out.println(“Returning:” + retVal.toString() ); } } Following is the content of the Student.java file. package com.tutorialspoint; public class Student { private Integer age; private String name; public void setAge(Integer age) { this.age = age; } public Integer getAge() { System.out.println(“Age : ” + age ); return age; } public void setName(String name) { this.name = name; } public String getName() { System.out.println(“Name : ” + name ); return name; } public void printThrowException(){ System.out.println(“Exception raised”); throw new IllegalArgumentException(); } } Following is the content of the MainApp.java file. package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”); Student student = (Student) context.getBean(“student”); student.getAge(); } } Following is the configuration file Beans.xml. <?xml version = “1.0” encoding = “UTF-8”?> <beans xmlns = “http://www.springframework.org/schema/beans” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xmlns:aop = “http://www.springframework.org/schema/aop” xsi:schemaLocation = “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd “> <aop:aspectj-autoproxy/> <!– Definition for student bean –> <bean id = “student” class = “com.tutorialspoint.Student”> <property name = “name” value = “Zara” /> <property name = “age” value = “11”/> </bean> <!– Definition for logging aspect –> <bean id = “logging” class = “com.tutorialspoint.Logging”/> </beans> Run Project Once you are done creating the source and configuration files, run your application. Rightclick on MainApp.java in your application and use run as Java Application command. If everything is fine with your application, it will print the following message. Age : 11 Method Signature: Integer com.tutorialspoint.Student.getAge() Returning 11 Print Page Previous Next Advertisements ”;
Category: Java
Spring DI – Autowiring ByName ”; Previous Next This mode specifies autowiring by property name. Spring container looks at the beans on which auto-wire attribute is set to byName in the XML configuration file. It then tries to match and wire its properties with the beans defined by the same names in the configuration file. If matches are found, it will inject those beans. Otherwise, bean(s) will not be wired. For example, if a bean definition is set to autowire byName in the configuration file, and it contains a spellChecker property (that is, it has a setSpellChecker(…)method), Spring looks for a bean definition named spellChecker, and uses it to set the property. Still you can wire the remaining properties using <property> tags. The following example will illustrate the concept. Example The following example shows a class TextEditor that can only be dependency-injected using pure setter-based injection. Let”s update the project created in Spring DI – Create Project chapter. We”re adding following files − TextEditor.java − A class containing a SpellChecker as dependency. SpellChecker.java − A dependency class. MainApp.java − Main application to run and test. Here is the content of TextEditor.java file − package com.tutorialspoint; public class TextEditor { private SpellChecker spellChecker; private String name; public void setSpellChecker( SpellChecker spellChecker ){ this.spellChecker = spellChecker; } public SpellChecker getSpellChecker() { return spellChecker; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void spellCheck() { spellChecker.checkSpelling(); } } Following is the content of another dependent class file SpellChecker.java − package com.tutorialspoint; public class SpellChecker { public SpellChecker(){ System.out.println(“Inside SpellChecker constructor.” ); } public void checkSpelling(){ System.out.println(“Inside checkSpelling.” ); } } Following is the content of the MainApp.java file − package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(“applicationcontext.xml”); TextEditor te = (TextEditor) context.getBean(“textEditor”); te.spellCheck(); } } Following is the configuration file applicationcontext.xml which has configuration for autowiring byName − <?xml version = “1.0” encoding = “UTF-8”?> <beans xmlns = “http://www.springframework.org/schema/beans” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”> <!– Definition for textEditor bean –> <bean id = “textEditor” class = “com.tutorialspoint.TextEditor” autowire = “byName”> <property name = “name” value = “Generic Text Editor” /> </bean> <!– Definition for spellChecker bean –> <bean id = “spellChecker” class = “com.tutorialspoint.SpellChecker”></bean> </beans> Output Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message − Inside SpellChecker constructor. Inside checkSpelling. Print Page Previous Next Advertisements ”;
Spring SpEL – Home
Spring SpEL Tutorial PDF Version Quick Guide Resources Job Search Discussion The Spring Expression Language, SpEL is a very powerful expression language and it supports querying and manipulating an object graph at runtime. It offers many advanced features like method invocation and basic string templating functionality. Spring Expression Language was originally created for the Spring community to have a single well supported expression language to be used across all the products in the Spring portfolio. While SpEL serves as the foundation for expression evaluation within the Spring portfolio, it is not directly tied to Spring and can be used independently. In this tutorial, we”ll cover Spring Expression Language, SpEL in Spring framework which helps in solving the common problems developers/users face in spring framework based applications. Audience This tutorial is designed for Java programmers with a need to understand the Spring framework in detail along with its architecture and actual usage. This tutorial will bring you at an intermediate level of expertise, from where you can take yourself to higher levels of expertise. Prerequisites Before proceeding with this tutorial, you should have a good understanding of Java programming language. Print Page Previous Next Advertisements ”;
Spring DI – Quick Guide
Spring Dependency Injection – Quick Guide ”; Previous Next Spring DI – Overview Spring is the most popular application development framework for enterprise Java. Millions of developers around the world use Spring Framework to create high performing, easily testable, and reusable code. Spring framework is an open source Java platform. It was initially written by Rod Johnson and was first released under the Apache 2.0 license in June 2003. Spring provides Ioc Containers which tend to be lightweight, especially when compared to EJB containers, for example. This is beneficial for developing and deploying applications on computers with limited memory and CPU resources. Dependency Injection (DI) The technology that Spring is most identified with is the Dependency Injection (DI) flavor of Inversion of Control. The Inversion of Control (IoC) is a general concept, and it can be expressed in many different ways. Dependency Injection is merely one concrete example of Inversion of Control. When writing a complex Java application, application classes should be as independent as possible of other Java classes to increase the possibility to reuse these classes and to test them independently of other classes while unit testing. Dependency Injection helps in gluing these classes together and at the same time keeping them independent. What is dependency injection exactly? Let”s look at these two words separately. Here the dependency part translates into an association between two classes. For example, class A is dependent of class B. Now, let”s look at the second part, injection. All this means is, class B will get injected into class A by the IoC. Dependency injection can happen in the way of passing parameters to the constructor or by post-construction using setter methods. As Dependency Injection is the heart of Spring Framework, we will explain this concept in a separate chapter with relevant example. Spring DI – Environment Setup This chapter will guide you on how to prepare a development environment to start your work with Spring Framework. It will also teach you how to set up JDK, Maven and Eclipse on your machine before you set up Spring Framework − Setup Java Development Kit (JDK) You can download the latest version of SDK from Oracle”s Java site − Java SE Downloads. You will find instructions for installing JDK in downloaded files, follow the given instructions to install and configure the setup. Finally set PATH and JAVA_HOME environment variables to refer to the directory that contains java and javac, typically java_install_dir/bin and java_install_dir respectively. If you are running Windows and have installed the JDK in C:jdk-11.0.11, you would have to put the following line in your C:autoexec.bat file. set PATH=C:jdk-11.0.11;%PATH% set JAVA_HOME=C:jdk-11.0.11 Alternatively, on Windows NT/2000/XP, you will have to right-click on My Computer, select Properties → Advanced → Environment Variables. Then, you will have to update the PATH value and click the OK button. On Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk-11.0.11 and you use the C shell, you will have to put the following into your .cshrc file. setenv PATH /usr/local/jdk-11.0.11/bin:$PATH setenv JAVA_HOME /usr/local/jdk-11.0.11 Alternatively, if you use an Integrated Development Environment (IDE) like Borland JBuilder, Eclipse, IntelliJ IDEA, or Sun ONE Studio, you will have to compile and run a simple program to confirm that the IDE knows where you have installed Java. Otherwise, you will have to carry out a proper setup as given in the document of the IDE. Setup Eclipse IDE All the examples in this tutorial have been written using Eclipse IDE. So we would suggest you should have the latest version of Eclipse installed on your machine. To install Eclipse IDE, download the latest Eclipse binaries from www.eclipse.org/downloads. Once you download the installation, unpack the binary distribution into a convenient location. For example, in C:eclipse on Windows, or /usr/local/eclipse on Linux/Unix and finally set PATH variable appropriately. Eclipse can be started by executing the following commands on Windows machine, or you can simply double-click on eclipse.exe %C:eclipseeclipse.exe Eclipse can be started by executing the following commands on Unix (Solaris, Linux, etc.) machine − $/usr/local/eclipse/eclipse After a successful startup, if everything is fine then it should display the following result − Set Maven In this tutorial, we are using maven to run and build the spring based examples. Follow the Maven – Environment Setup to install maven. Spring DI – IoC Containers The Spring container is at the core of the Spring Framework. The container will create the objects, wire them together, configure them, and manage their complete life cycle from creation till destruction. The Spring container uses DI to manage the components that make up an application. These objects are called Spring Beans, which we will discuss in the next chapter. The container gets its instructions on what objects to instantiate, configure, and assemble by reading the configuration metadata provided. The configuration metadata can be represented either by XML, Java annotations, or Java code. The following diagram represents a high-level view of how Spring works. The Spring IoC container makes use of Java POJO classes and configuration metadata to produce a fully configured and executable system or application. Spring provides the following two distinct types of containers. Sr.No. Container & Description 1 Spring BeanFactory Container This is the simplest container providing the basic support for DI and is defined by the org.springframework.beans.factory.BeanFactory interface. The BeanFactory and related interfaces, such as BeanFactoryAware, InitializingBean, DisposableBean, are still present in Spring for the purpose of backward compatibility with a large number of third-party frameworks that integrate with Spring. 2 Spring ApplicationContext Container This container adds more enterprise-specific functionality such as the ability to resolve textual messages from a properties file and
Spring Boot JPA – Quick Guide ”; Previous Next Spring Boot JPA – Overview What is JPA? Java Persistence API is a collection of classes and methods to persistently store the vast amounts of data into a database which is provided by the Oracle Corporation. Where to use JPA? To reduce the burden of writing codes for relational object management, a programmer follows the ‘JPA Provider’ framework, which allows easy interaction with database instance. Here the required framework is taken over by JPA. JPA History Earlier versions of EJB, defined persistence layer combined with business logic layer using javax.ejb.EntityBean Interface. While introducing EJB 3.0, the persistence layer was separated and specified as JPA 1.0 (Java Persistence API). The specifications of this API were released along with the specifications of JAVA EE5 on May 11, 2006 using JSR 220. JPA 2.0 was released with the specifications of JAVA EE6 on December 10, 2009 as a part of Java Community Process JSR 317. JPA 2.1 was released with the specification of JAVA EE7 on April 22, 2013 using JSR 338. JPA Providers JPA is an open source API, therefore various enterprise vendors such as Oracle, Redhat, Eclipse, etc. provide new products by adding the JPA persistence flavor in them. Some of these products include − Hibernate, Eclipselink, Toplink, Spring Data JPA, etc. Spring Boot JPA – Environment Setup This chapter will guide you on how to prepare a development environment to start your work with Spring Boot Framework. It will also teach you how to set up JDK, Eclipse on your machine before you set up Spring Boot Framework − Step 1 – Setup Java Development Kit (JDK) Java SE is available for download for free. To download click here, please download a version compatible with your operating system. Follow the instructions to download Java, and run the .exe to install Java on your machine. Once you have installed Java on your machine, you would need to set environment variables to point to correct installation directories. Setting Up the Path for Windows 2000/XP Assuming you have installed Java in c:Program Filesjavajdk directory − Right-click on ”My Computer” and select ”Properties”. Click on the ”Environment variables” button under the ”Advanced” tab. Now, edit the ”Path” variable and add the path to the Java executable directory at the end of it. For example, if the path is currently set to C:WindowsSystem32, then edit it the following way C:WindowsSystem32;c:Program Filesjavajdkbin. Setting Up the Path for Windows 95/98/ME Assuming you have installed Java in c:Program Filesjavajdk directory − Edit the ”C:autoexec.bat” file and add the following line at the end − SET PATH=%PATH%;C:Program Filesjavajdkbin Setting Up the Path for Linux, UNIX, Solaris, FreeBSD Environment variable PATH should be set to point to where the Java binaries have been installed. Refer to your shell documentation if you have trouble doing this. For example, if you use bash as your shell, then you would add the following line at the end of your .bashrc − export PATH=/path/to/java:$PATH” Alternatively, if you use an Integrated Development Environment (IDE) like Borland JBuilder, Eclipse, IntelliJ IDEA, or Sun ONE Studio, you will have to compile and run a simple program to confirm that the IDE knows where you have installed Java. Otherwise, you will have to carry out a proper setup as given in the document of the IDE. Step 2 – Setup Eclipse IDE All the examples in this tutorial have been written using Eclipse IDE. So we would suggest you should have the latest version of Eclipse installed on your machine. To install Eclipse IDE, download the latest Eclipse binaries from www.eclipse.org/downloads/. Once you download the installation, unpack the binary distribution into a convenient location. For example, in C:eclipse on Windows, or /usr/local/eclipse on Linux/Unix and finally set PATH variable appropriately. Eclipse can be started by executing the following commands on Windows machine, or you can simply double-click on eclipse.exe %C:eclipseeclipse.exe Eclipse can be started by executing the following commands on Unix (Solaris, Linux, etc.) machine − $/usr/local/eclipse/eclipse After a successful startup, if everything is fine then it should display the following result − Step 3 – Setup m2eclipse M2Eclipse is eclipse plugin which is very useful integration for Apache Maven into the Eclipse IDE. We are using maven in this tutorial to build spring boot project and examples are run within eclipse using m2eclipse. Install the latest M2Eclipse release by using the Install New Software dialog in Eclipse IDE,and point it to this p2 repository − https://download.eclipse.org/technology/m2e/releases/latest/ Step 3 – Setup Spring Boot Project Now if everything is fine, then you can proceed to set up your Spring Boot. Following are the simple steps to download and install the Spring Boot Project on your machine. Go to spring initializer link to create a spring boot project, https://start.spring.io/. Select project as Maven Project. Select language as Java. Select Spring Boot version as 2.5.3. Set Project Metadata – Group as com.tutorialspoint, Artifact as springboot-h2, name as springboot-h2, Description as Demo project for Spring Boot and H2 Database and package name as com.tutorialspoint.springboot-h2. Select packaging as Jar. Select java as 11. Add dependencies as Spring Web, Spring Data JPA, H2 Database and Spring Boot DevTools. Now click on GENERATE Button to generate the project structure. Once the maven based spring boot project is downloaded, then import the maven project into eclipse and rest eclipse will handle. It will download the maven dependencies and build the project to make it ready for further development. Step 4 – POSTMAN for REST APIs Testing
Spring AOP – Advice Types
Spring AOP – Advice Types ”; Previous Next Spring aspects can work with five kinds of advice mentioned in the following table. Sr.No. Advice & Description 1 before Run advice before the method execution. 2 after Run advice after the method execution, regardless of its outcome. 3 after-returning Run advice after the method execution, only if the method completes successfully. 4 after-throwing Run advice after the method execution, only if the method exits by throwing an exception. 5 around Run advice before and after the advised method is invoked. Print Page Previous Next Advertisements ”;
Spring AOP – XML Based After Returning Advice ”; Previous Next After is an advice type which ensures that an advice runs after the method execution only if the method completes successfully. Following is the syntax of after advice. Syntax <aop:config> <aop:aspect id = “log” ref = “logging”> <aop:pointcut id = “PointCut-id” expression = “execution( expression )”/> <aop:after-returning pointcut-ref = “PointCut-id” returning = “retVal” method = “methodName”/> </aop:aspect> </aop:config> Where, PointCut-id − id of the PointCut. methodName − Method name of the function to be called after a called function returns successfully. To understand the above-mentioned concepts related to After Returning Advice, let us write an example which will implement After Returning advice. To write our example with few advices, let us have a working Eclipse IDE in place and use the following steps to create a Spring application − Step Description 1 Update the project Student created under chapter Spring AOP – Application. 2 Update the bean configuration and run the application as explained below. Following is the content of Logging.java file. This is actually a sample of aspect module, which defines the methods to be called at various points. package com.tutorialspoint; public class Logging { /** * This is the method which I would like to execute * when any method returns. */ public void afterReturningAdvice(Object retVal){ System.out.println(“Returning:” + retVal.toString() ); } } Following is the content of the Student.java file. package com.tutorialspoint; public class Student { private Integer age; private String name; public void setAge(Integer age) { this.age = age; } public Integer getAge() { System.out.println(“Age : ” + age ); System.out.println(“Exception raised”); throw new IllegalArgumentException(); return age; } public void setName(String name) { this.name = name; } public String getName() { System.out.println(“Name : ” + name ); return name; } public void printThrowException(){ System.out.println(“Exception raised”); throw new IllegalArgumentException(); } } Following is the content of the MainApp.java file. package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”); Student student = (Student) context.getBean(“student”); student.getName(); student.getAge(); } } Following is the configuration file Beans.xml. <?xml version = “1.0” encoding = “UTF-8”?> <beans xmlns = “http://www.springframework.org/schema/beans” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xmlns:aop = “http://www.springframework.org/schema/aop” xsi:schemaLocation = “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd “> <aop:config> <aop:aspect id = “log” ref = “logging”> <aop:pointcut id = “selectAll” expression = “execution(* com.tutorialspoint.*.*(..))”/> <aop:after-returning pointcut-ref = “selectAll” method = “afterReturningAdvice” returning = “retVal”/> </aop:aspect> </aop:config> <!– Definition for student bean –> <bean id = “student” class = “com.tutorialspoint.Student”> <property name = “name” value = “Zara” /> <property name = “age” value = “11”/> </bean> <!– Definition for logging aspect –> <bean id = “logging” class = “com.tutorialspoint.Logging”/> </beans> Run Project Once you are done creating the source and configuration files, run your application. Rightclick on MainApp.java in your application and use run as Java Application command. If everything is fine with your application, it will print the following message. Name : Zara Returning : Name Age : 11 Exception raised Print Page Previous Next Advertisements ”;
Spring AOP – Environment Setup ”; Previous Next This chapter will guide you on how to prepare a development environment to start your work with Spring Framework. It will also teach you how to set up JDK, Maven and Eclipse on your machine before you set up Spring Framework − Step 1 – Setup Java Development Kit (JDK) You can download the latest version of SDK from Oracle”s Java site − Java SE Downloads. You will find instructions for installing JDK in downloaded files, follow the given instructions to install and configure the setup. Finally set PATH and JAVA_HOME environment variables to refer to the directory that contains java and javac, typically java_install_dir/bin and java_install_dir respectively. If you are running Windows and have installed the JDK in C:jdk-11.0.11, you would have to put the following line in your C:autoexec.bat file. set PATH=C:jdk-11.0.11;%PATH% set JAVA_HOME=C:jdk-11.0.11 Alternatively, on Windows NT/2000/XP, you will have to right-click on My Computer, select Properties → Advanced → Environment Variables. Then, you will have to update the PATH value and click the OK button. On Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk-11.0.11 and you use the C shell, you will have to put the following into your .cshrc file. setenv PATH /usr/local/jdk-11.0.11/bin:$PATH setenv JAVA_HOME /usr/local/jdk-11.0.11 Alternatively, if you use an Integrated Development Environment (IDE) like Borland JBuilder, Eclipse, IntelliJ IDEA, or Sun ONE Studio, you will have to compile and run a simple program to confirm that the IDE knows where you have installed Java. Otherwise, you will have to carry out a proper setup as given in the document of the IDE. Step 2 – Setup Eclipse IDE All the examples in this tutorial have been written using Eclipse IDE. So we would suggest you should have the latest version of Eclipse installed on your machine. To install Eclipse IDE, download the latest Eclipse binaries from https://www.eclipse.org/downloads/. Once you download the installation, unpack the binary distribution into a convenient location. For example, in C:eclipse on Windows, or /usr/local/eclipse on Linux/Unix and finally set PATH variable appropriately. Eclipse can be started by executing the following commands on Windows machine, or you can simply double-click on eclipse.exe %C:eclipseeclipse.exe Eclipse can be started by executing the following commands on Unix (Solaris, Linux, etc.) machine − $/usr/local/eclipse/eclipse After a successful startup, if everything is fine then it should display the following result − Step 3 – Download Maven Archive Download Maven 3.8.4 from https://maven.apache.org/download.cgi. OS Archive name Windows apache-maven-3.8.4-bin.zip Linux apache-maven-3.8.4-bin.tar.gz Mac apache-maven-3.8.4-bin.tar.gz Step 4 – Extract the Maven Archive Extract the archive, to the directory you wish to install Maven 3.8.4. The subdirectory apache-maven-3.8.4 will be created from the archive. OS Location (can be different based on your installation) Windows C:Program FilesApache Software Foundationapache-maven-3.8.4 Linux /usr/local/apache-maven Mac /usr/local/apache-maven Step 5 – Set Maven Environment Variables Add M2_HOME, M2, MAVEN_OPTS to environment variables. OS Output Windows Set the environment variables using system properties. M2_HOME=C:Program FilesApache Software Foundationapache-maven-3.8.4 M2=%M2_HOME%bin MAVEN_OPTS=-Xms256m -Xmx512m Linux Open command terminal and set environment variables. export M2_HOME=/usr/local/apache-maven/apache-maven-3.8.4 export M2=$M2_HOME/bin export MAVEN_OPTS=-Xms256m -Xmx512m Mac Open command terminal and set environment variables. export M2_HOME=/usr/local/apache-maven/apache-maven-3.8.4 export M2=$M2_HOME/bin export MAVEN_OPTS=-Xms256m -Xmx512m Step 6 – Add Maven bin Directory Location to System Path Now append M2 variable to System Path. OS Output Windows Append the string ;%M2% to the end of the system variable, Path. Linux export PATH=$M2:$PATH Mac export PATH=$M2:$PATH Step 7 – Verify Maven Installation Now open console and execute the following mvn command. OS Task Command Windows Open Command Console c:> mvn –version Linux Open Command Terminal $ mvn –version Mac Open Terminal machine:~ joseph$ mvn –version Finally, verify the output of the above commands, which should be as follows − OS Output Windows Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537) Maven home: C:Program FilesApache Software Foundationapache-maven-3.8.4 Java version: 11.0.11, vendor: Oracle Corporation, runtime: C:Program FilesJavajdk11.0.11 Default locale: en_IN, platform encoding: Cp1252 OS name: “windows 10”, version: “10.0”, arch: “amd64”, family: “windows” Linux Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537) Java version: 11.0.11 Java home: /usr/local/java-current/jre Mac Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537) Java version: 11.0.11 Java home: /Library/Java/Home/jre Print Page Previous Next Advertisements ”;
Spring AOP – Core Concepts
Spring AOP – Core Concepts ”; Previous Next Before we start working with AOP, let us become familiar with the AOP concepts and terminologies. These terms are not specific to Spring, rather they are related to AOP. Sr.No. Terms & Description 1 Aspect A module which has a set of APIs providing cross-cutting requirements. For example, a logging module would be called AOP aspect for logging. An application can have any number of aspects depending on the requirement. 2 Join point This represents a point in your application where you can plug-in AOP aspect. You can also say, it is the actual place in the application where an action will be taken using Spring AOP framework. 3 Advice This is the actual action to be taken either before or after the method execution. This is the actual piece of code that is invoked during program execution by Spring AOP framework. 4 PointCut This is a set of one or more joinpoints where an advice should be executed. You can specify PointCuts using expressions or patterns as we will see in our AOP examples. 5 Introduction An introduction allows you to add new methods or attributes to existing classes. 6 Target object The object being advised by one or more aspects. This object will always be a proxied object. Also referred to as the advised object. 7 Weaving Weaving is the process of linking aspects with other application types or objects to create an advised object. This can be done at compile time, load time, or at runtime. Print Page Previous Next Advertisements ”;
Spring AOP – XML Based After Throwing Advice ”; Previous Next After-throwing is an advice type which ensures that an advice runs after the method execution, only if the method exits by throwing an exception. Following is the syntax of after-throwing advice. Syntax <aop:config> <aop:aspect id = “log” ref = “logging”> <aop:pointcut id = “PointCut-id” expression = “execution( expression )”/> <aop:after-throwing pointcut-ref = “PointCut-id” throwing = “ex” method = “methodName”/> </aop:aspect> </aop:config> Where, PointCut-id − id of the PointCut. ex − Exception to be thrown. methodName − Method name of the function to be called when a called function throws an exception and exits. To understand the above-mentioned concepts related to After Throwing Advice, let us write an example which will implement After Throwing Advice. To write our example with few advices, let us have a working Eclipse IDE in place and use the following steps to create a Spring application − Step Description 1 Update the project Student created under chapter Spring AOP – Application. 2 Update the bean configuration and run the application as explained below. Following is the content of Logging.java file. This is actually a sample of aspect module, which defines the methods to be called at various points. package com.tutorialspoint; public class Logging { /** * This is the method which I would like to execute * if there is an exception raised. */ public void afterThrowingAdvice(IllegalArgumentException ex) { System.out.println(“There has been an exception: ” + ex.toString()); } } Following is the content of the Student.java file. package com.tutorialspoint; public class Student { private Integer age; private String name; public void setAge(Integer age) { this.age = age; } public Integer getAge() { System.out.println(“Age : ” + age ); return age; } public void setName(String name) { this.name = name; } public String getName() { System.out.println(“Name : ” + name ); return name; } public void printThrowException(){ System.out.println(“Exception raised”); throw new IllegalArgumentException(); } } Following is the content of the MainApp.java file. package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”); Student student = (Student) context.getBean(“student”); student.printThrowException(); } } Following is the configuration file Beans.xml. <?xml version = “1.0” encoding = “UTF-8”?> <beans xmlns = “http://www.springframework.org/schema/beans” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xmlns:aop = “http://www.springframework.org/schema/aop” xsi:schemaLocation = “http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd “> <aop:config> <aop:aspect id = “log” ref = “logging”> <aop:pointcut id = “selectAll” expression = “execution(* com.tutorialspoint.*.*(..))”/> <aop:after-throwing pointcut-ref = “selectAll” throwing = “ex” method = “afterThrowingAdvice”/> </aop:aspect> </aop:config> <!– Definition for student bean –> <bean id = “student” class = “com.tutorialspoint.Student”> <property name = “name” value = “Zara” /> <property name = “age” value = “11”/> </bean> <!– Definition for logging aspect –> <bean id = “logging” class = “com.tutorialspoint.Logging”/> </beans> Run Project Once you are done creating the source and configuration files, run your application. Rightclick on MainApp.java in your application and use run as Java Application command. If everything is fine with your application, it will print the following message. Exception raised There has been an exception: java.lang.IllegalArgumentException Exception in thread “main” java.lang.IllegalArgumentException at com.tutorialspoint.Student.printThrowException(Student.java:25) … Print Page Previous Next Advertisements ”;