Maven by Nagoor Babupdf
Maven by Nagoor Babupdf
Maven by Nagoor Babupdf
1
e
g
a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
2
e
g
a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
Index
1. Introduction………………………………………………………
………………………………………………………………… ………….Page 04
2. Project Object Model……………………………………………………….Page 05
3. Archetypes in MAVEN………………………………………….…………..Page 19
4. Steps to prepare MAVEN Project in Eclipse ……………………………..Page 22
5. Web Application in Maven………………………………………………….Page 34
6. Spring Core Module Application in MAVEN………………...……………Page 43
3
e
g
a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
MAVEN
Introduction
Maven was originally designed to simplify building processes in Jakarta Turbine pr oject. There
were several projects and each project contained slightly different ANT build files. JARs
JARs were
checked into CVS.
MAVEN is a "Project Management Framework", it is much more than a simple Build tool, its
declarative and standard approach simplifies many aspects of the Project Lifecycle.
MAVEN follows "Convention over Configuration" Principle, which means that developers are not
required to create build process themselves, Developers do not have to mention each and
every configuration detail. Maven provides sensible default behavior for projects.
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
In pom file, initial we will identify "Projection Description", it contains Project name, version
number, packaged type,.....
To specify the above details, we need to use the following XML tags.
1. <project ..... >
2. <!-- Project Description -->
3. <modelVersion> --- </modelVersion>
4. <groupid> --- </groupid>
5. <artifactid> -- </artifactid>
6. <version> -- </version>
7. -----
8. -----
9. </project>
1. <project ..... >
2. <!-- Project Description -->
3. <modelVersion>4.0.0</modelVersion>
4. <groupid>com.durgasoft.banking</groupid>
5. <artifactid>icici.accounts</artifactid>
6. <version>1.0</version>
7. <name> Account Application</name>
8. <description> Application for Accounts module in icici Bank project </description>
9. ----- 6
e
10. </project> g
a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
2) Repository:
If we use Dependencies in MAVEN Project then MAVEN will search for the dependent JARs in
Repositories.
MAVEN will use three types of Repositories in order to get dependencies.
1. Local Repository:
It is a location to manage and supply all dependencies, it will be created by MAVEN when we
execute any MAVEN command first time.
2. Central Repository:
In MAVEN applications, if we want to use the above explict repositories then we have to configure
them in pom file by using the following xml tags.
1. <repositories>
2. <repository>
3. <id> jboss</id>
4. <name> jboss repo</name>
5. <url>http://repository.jboss.org/nexus/content/groups/public/</url>
6. </repository>
7. </repositories>
3. Remote Repository:
In some Situations, Maven does not find the dependencies in Local Repository and in central
repository, in this context, MAVEN stops the build process and generates some Exceptions. To
overcome this problems, Maven has provided a new Features like "Remote Repository". 7
e
g
a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
To configure Remote Repository, we have to use the following XML tags in pom.xml file.
1. <repositories>
2. <repository>
3. <id>durgasoft.lib</id>
4. <url>http://library.durgasoft.com/maven2/lib</url>
5. </repository>
6. </repositories>
When we run MAVEN project then MAVEN will search for the dependencies in the following order.
1) First, MAVEN will search for the dependencies in local repository, if the required
dependencies are available at Local Repository the MAVEN will use them in application. If
the dependencies are not available at Local Repository then MAVEN search for them at
Central Repository.
2) If the required Dependencies are existed in central repository then MAVEN will load them
into Local Repository and MAVEN will use them in the applications. If the required
dependencies are not existed in Central Repository then MAVEN will search for them in
Remote Repositories as per configuration.
3) If Remote Repository is not configured then MAVEN will stop the application execution and
generated some Exceptions.
4) If Remote Repository is configured then MAVEN will search for the required dependencies
in Remote Repository, if they are identified then MAVEN will load them into Local
Repository for futur reference. If the dependencies are not existed at Remote Repositories
then MAVEN will stop the execution and generate some Exceptions.
8
e
g
a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
3) Dependency Management:
In Applications, Dependencies are the libraries[Collection of JARs] which are required to compile,
test and run our applications.
In General, in application development, we will download the required libraries from internet and
we will store them in application directory structer.
The main Advantage of MAVEN in applications development is that not to store any Dependent
JAR files in Project Directory Structer by downloading them explicitly, MAVEN has given f lexibility
to the developers like to specify dependent JAR files names in pom file, where MAVEN will search
for them in the repositories and MAVEN will load them into the project directory structer
automatically.
If we need any Library in MAVEN based applications then we have to declare them in pom file like
below.
1. <dependencies>
2. <dependency>
3. <groupId>org.hibernate</groupId>
4. <artifactId>hibernate-core</artifactId>
5. <version>3.5.6-Final</version>
6. <scope>provided</scope>
7. </dependency> 9
e
8. </dependencies> g
a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
If we provide the dependency like above then MAVEN will search for the hibernate library with the
name like
http://repo1.maven.org/maven2/org/hibernate/hibernate-core/3.5.6-Final/
MAVEN is following "Transitive Dependencies Mechanism", that is, if our dependencies are
required any other libraries then MAVEN will get them automatically without loading them explicitly
by the developers.
Dependency Scopes:
In Applications, some dependencies are required to all phases of the project lifecycle like compile,
test, run,... and some other required only some of phases of the project lifecycle.
In order to limit the dependencies for the lifecycle phases we will use Dependency Scopes.
1. Compile:
It is the default scope in MAVEN . This scope will make the dependencies to avail all phases like
compile, test, run,....
EX:
1. <dependency>
2. <groupId>org.hibernate</groupId>
3. <artifactId>hibernate-core</artifactId>
4. <version>3.5.6-Final</version>
5. <scope>compile</scope>
6. </dependency>
0
1
e
g
a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
2. Provided:
This scope will make the dependency libraries to avail upto compilation and and upto testinbg, not
for runtime, because, at runtime, JDKs or Containers will provide the required dependencies at
runtime.
EX:
In web applications, Servlet API is required expliclty to compile and test the project, but, Servlet
API is provided by the container at runtime automatically, so that, they are not required to be
exported at runtime.
1. <dependency>
2. <groupId> javax.servlet</groupId>
3. <artifactId>servlet-api</artifactId>
4. <version>3.0.1</version>
5. <scope>provided</scope>
6. </dependency>
3. Runtime:
This scope indicates that the dependency is not required for compilation, but is for execution. It is
in the runtime and test class paths, but not the compile class path.
EX:
1. <dependency>
2. <groupId>com.thoughtworks.xstream</groupId>
3. <artifactId>xstream</artifactId>
4. <version>1.4.4</version>
5. <scope>runtime</scope>
6. </dependency>
4. Test:
1
1
This scope indicates that the dependency is not required for normal use of the application, and is e
g
only available for the test compilation and execution phases. This scope is not transitive. a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
EX:
1. <dependency>
2. <groupId> junit</groupId>
3. <artifactId> junit</artifactId>
4. <version>4.12</version>
5. <scope>test</scope>
6. </dependency>
5. System:
Dependencies with system are similar to ones with scope provided. The only difference is system
dependencies are not retrieved from remote repository. They are present under project’s
subdirectory and are referred from there.
EX:
1. <dependency>
2. <groupId>Explicit_Dependency</groupId>
3. <artifactId>Explicit_Dependency</artifactId>
4. <scope>system</scope>
5. <version>1.0</version>
6. <systemPath>apps\app.war\WEB-INF\lib\Explicit_Dependency.jar </systemPath>
7. </dependency>
6. Import:
1. <dependencyManagement> 2
1
2. <dependencies> e
g
3. <dependency> a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
4) Project Inheritance:
In MAVEN based applications, it is possible to inherit configurations from one pom file to another
pom file inorder to avoid configurations redundency.
To declare parent pom , we have to use "pom" as value to <packaging> tag in parent pom file.
EX:
1. <project ... >
2. <modelVersion>4.0.0</modelVersion>
3. <groupId>com.durgasdoft</groupId>
4. <artifactId>my-parent</artifactId>
5. <version>0.0.1-SNAPSHOT</version>
6. <packaging>pom</packaging>
7. ---
8. </project>
If we want to inherit parent pom configuration details into a particular chaild pom then we have to
configure parent pom in chaild pom.
EX:
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
Note: In JAVA, java.lang.Object class is common and default super class for every java class
inorder to provide 11 common and default methods to each and every java class, similarily , there
is a common and default super pom file is existed in maven inorder to provide all common
configurations and settings to the chaild pom file.
Note: By default, Maven looks for the parent POM first at project’s root, then the local repository,
and lastly in the remote repository. If parent POM file is not located in any other place, then you
can use code tag. This relative path shall be relative to project root.
EX:
1. <parent>
2. <groupId>com.durgasoft</groupId>
3. <artifactId>MavenExamples</artifactId>
4. <version>0.0.1-SNAPSHOT</version>
5. <relativePath>../baseapp/pom.xml</relativePath>
6. </parent>
Note: If we want to get super pom from MAVEN then use the following command on command
prompt from the project root location which contains project specific pom file.
mvn help:effective-pom
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
MAVEN is simply the collection of plugins, where plugins are used to perfor m the actions like
creating jar files, creating war files, compile Source code, executing unit test code, create project
documentation, ......
MAVEN is having "Plugin Execution Framework" at its heart inorder to execute all plugins.
1.Build Plugins: These plugins are executed during the build and they should be configured in
the <build/> element from the POM.
EX
1.Clean : It is used when you want to remove files generated at build-time in a
project's directory.
2.Compiler: Compiles Java source code.
3.Deploy: It can be used to store artifacts in remote repository while deploying the
applications in order to share to other projects .
4.Install: It can be used to install artifacts into local repository.
5.Resources: It will include all the project resources in output directory while creating
JAR files.
6.Ear: create ear file from the current project.
7.jar: creates jar file from the current project.
8.war: creates war file from the current project.
9.rar: creates rar file from the current project.
2. Reporting plugins: These plugins are executed during the site generation and they should be
configured in the <reporting/> element from the POM.
EX:
1.changelog: Generate a list of recent changes from your SCM[Software Configuration
Management].
2.changes: Generate a report from an issue tracker or a change document.
3.javadoc: Generate Javadoc for the project.
5
4.project-info-resports: Generate standard project reports.
1
5.surfire-report: Generate a report based on the results of unit tests. e
g
a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
1. <project ---- >
2. <build>
3. <plugins>
4. <plugin>
5. <groupId>org.apache.maven.plugins</groupId>
6. <artifactId>maven-compiler-plugin</artifactId>
7. <configuration>
8. <source>1.8</source>
9. <target>1.8</target>
10. </configuration>
11. </plugin>
12. </plugins>
13. </build>
14. </project>
By default, all files placed in "src\main\config" are packaged into the generated project artifact and
any file which we placed in "src\test\resources" are available in project classpath during unit t ests.
If we want to provide our own customized resources location in project then we have to configure
them in pom.xml file under <build> tag like below.
1. <build>
2. ----
3. <resources>
4. <resource>
5. <directory>src/main/config</directory</directory>
6. </resource>
7. <resource>
8. <directory>src/main/resources</directory</directory>
9. </resource>
10. ----
11. </resources>
12. ----
13. </build>
6
1
e
g
a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
IN general, profiles are used to customize the build lifecycle for different environments like
development, testing, production,.....
Example:
1. <profiles>
2. <profile>
3. <id>development</id>
4. <activation>
5. <activeByDefault>true</activeByDefault>
6. </activation>
7. <properties>
8. <jdbc.connection.url> jdbc:oracle:thin:@localhost:1521:xe</jdbc.connection.url>
9. </properties>
10. </profile>
11. <profile>
12. <id>test</id>
13. <properties>
14. <jdbc.connection.url> jdbc:mysql://localhost:3306/durgadb</jdbc.connection.url>
15. </properties>
16. </profile>
17. </profiles>
Where each and every profile has its own id, it can be used to access the respective environment
or profile.
In src/main/resources/db.properties
jdbc.connection.url = ${jdbc.connection.url}
If we provide the above setups like above then at compilation time, the respective jdbc URL will be
injected to the "jdbc.connection.url" property depending on the target environment.
Use the following command on command prompt inorder to compile the project.
C:/apps>mvn compile 7
Here "jdbc.connection.profile" property will take "jdbc:oracle:thin:@localhost:1521:xe" value. 1
e
g
a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
If we provide the above setups like above then at compilation time, the respective jdbc URL will be
injected to the "jdbc.connection.url" property depending on the target environment.
Use the following command on command prompt inorder to compile the project.
C:/apps>mvn compile
Here "jdbc.connection.profile" property will take "jdbc:oracle:thin:@localhost:1521:xe" value.
8
1
e
g
a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
Archetypes in MAVEN
To create sample MAVEN standalone application directory structer we will use the following
command on command prompt .
Installation Process:
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
If we do the above steps then MAVEN will create quick start project with the following directories
and files.
E:/mvn_projects
Sampleapp
|--src
| |---main
| | |----java
| | |---com
| | |---durgasoft
| | |----App.java
| |---test
| |----java
| |---com
| |----durgasoft
| |-----AppTest.java
|---pom.xml
App.java
1. package com.durgasoft;
2. 0
3. /** 2
e
4. * Hello world! g
a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
AppTest.java
1. package com.durgasoft;
2.
3. import static org.junit.Assert.assertTrue;
4.
5. import org.junit.Test;
6.
7. /**
8. * Unit test for simple App.
9. */
10. public class AppTest
11.{
12. /**
13. * Rigorous Test :-)
14. */
15. @Test
16. public void shouldAnswerWithTrue()
17. {
18. System.out.println("Welcome To Durgasoft");
19. assertTrue( true );
20. }
21.}
To Compile JAVA code, first, goto application folder then use the following command on
command prompt.
1
E:\mvn_projects\sampleapp>mvn compile 2
e
g
a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
E:\mvn_projects\sampleapp>mvn test
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
src/test/java/AppTest.java
1. package com.durgasoft;
2. import junit.framework.Test;
3. import junit.framework.TestCase;
4. import junit.framework.TestSuite;
5. public class AppTest extends TestCase
6. {
7. public AppTest( String testName )
8. {
9. super ( testName );
10. }
11. public static Test suite()
12. {
13. return new TestSuite( AppTest.class );
14. }
15. public void testApp()
16. {
17. System.out.println("Hello Maven!"); 4
18. assertTrue( true ); 2
e
19. } g
a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
To prepare JDBC Application with MAVEN then we have to use the following steps with all the
above steps.
1. <dependencies>
2. <dependency>
3. <groupId>oracle</groupId>
4. <artifactId>oracle-jdbc</artifactId>
5. <version>11.2.0-XE</version>
6. <scope>runtime</scope>
7. </dependency>
8. </dependencies>
EX:
5
├───src 2
│ ├───main e
g
a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
pom.xml
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
JdbcApp.java
1. package com.durgasoft;
2.
3. import java.sql.Connection;
4. import java.sql.DriverManager;
5. import java.sql.ResultSet;
6. import java.sql.Statement;
7.
8. public class JdbcApp {
9. Connection con;
10. Statement st;
11. ResultSet rs;
12. public JdbcApp() {
13. try {
14. Class.forName("oracle.jdbc.OracleDriver");
15. con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system
", "durga");
16. st = con.createStatement();
17. } catch (Exception e) { 7
2
18. e.printStackTrace(); e
g
19. } a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
AppTest.java
1. package com.durgasoft;
2.
3. import java.sql.Connection;
4. import java.sql.DriverManager;
5. import java.sql.ResultSet;
6. import java.sql.Statement;
7.
8. import junit.framework.Test;
9. import junit.framework.TestCase;
10. import junit.framework.TestSuite;
11.
12./**
13. * Unit test for simple App.
14. */
15. public class AppTest
16. extends TestCase
17.{
18. /**
8
19. * Create the test case 2
20. * e
g
a
21. * @param testName name of the test case P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
If we want to use MySQL Database in Our project then we have to use the following dependency
in pom file.
1. <dependency>
2. <groupId>mysql</groupId>
3. <artifactId>mysql-connector-java</artifactId>
4. <version>8.0.11</version>
5. </dependency>
In Java Application
Driver Class: com.mysql.cj.jdbc.Driver
Driver UIRL: jdbc:mysql://localhost:3306/durgadb
DB User Name: root
DB Password : root
9
2
Example: e
g
a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
JdbcApp.java
1. package com.durgasoft;
2.
3. import java.sql.Connection;
4. import java.sql.DriverManager;
5. import java.sql.ResultSet;
6. import java.sql.Statement;
7.
8. public class JdbcApp {
9. Connection con;
10. Statement st;
11. ResultSet rs;
12. public JdbcApp() {
13. try {
14. Class.forName("com.mysql.jdbc.Driver");
15. con = DriverManager.getConnection("jdbc:mysql://localhost:3300/durgadb", "root", "
root");
16. st = con.createStatement();
17. } catch (Exception e) {
18. e.printStackTrace();
19. } 1
3
20. } e
g
21. public void displayEmpDetails() { a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
AppTest.java
1. package com.durgasoft;
2.
3. import java.sql.Connection;
4. import java.sql.DriverManager;
5. import java.sql.ResultSet;
6. import java.sql.Statement;
7.
8. import junit.framework.Test;
9. import junit.framework.TestCase;
10. import junit.framework.TestSuite;
11.
12./**
13. * Unit test for simple App.
14. */
15. public class AppTest
16. extends TestCase
17.{
18. /**
19. * Create the test case
20. *
21. * @param testName name of the test case
22. */ 2
3
23. public AppTest( String testName ) e
g
24. { a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
1. <plugin>
2. <groupId>org.apache.maven.plugins</groupId>
3. <artifactId>maven-compiler-plugin</artifactId>
4. <version>3.7.0</version>
5. <configuration> 4
6. <source>1.8</source> 3
e
g
7. <target>1.8</target> a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
1. <plugin>
2. <groupId>org.apache.tomcat.maven</groupId>
3. <artifactId>tomcat7-maven-plugin</artifactId>
4. <version>2.2</version>
5. </plugin>
1. <dependency>
2. <groupId> javax.servlet</groupId>
3. <artifactId> javax.servlet-api</artifactId>
4. <version>3.1.0</version>
5. <scope>provided</scope>
6. </dependency>
EX: pom.xml
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
EX: loginform.html
1. <html>
2. <body>
3. <form action="./hello" method="post">
4. User Name<input type="text" name="uname" /><br>
5. Password <input type="password" name="upwd" /><br>
6. <input type="submit" value="Login" />
7. </form>
8. </body>
9. </html>
10.
11.success.html
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <meta charset="ISO-8859-1">
5. <title>Insert title here</title>
6. </head>
7. <body>
8. <h1>Durga Software Solutions</h1>
9. <h2>User Login Status</h2> 7
3
10. <font color ="red" size="7">User Login Success</font> e
g
11. <h3> a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <meta charset="ISO-8859-1">
5. <title>Insert title here</title>
6. </head>
7. <body>
8. <h1>Durga Software Solutions</h1>
9. <h2>User Login Status</h2>
10. <font color ="red" size="7">User Login Failure</font>
11. <h3>
12. <a href ="./loginform.html">|User Login Form|</a>
13. </h3>
14. </body>
15. </html>
b) Prepare Servlets:
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
EX: LoginServlet.java
1. package com.durgasoft.servlets;
2.
3. import java.io.IOException;
4.
5. import javax.servlet.RequestDispatcher;
6. import javax.servlet.ServletException;
7. import javax.servlet.http.HttpServlet;
8. import javax.servlet.http.HttpServletRequest;
9. import javax.servlet.http.HttpServletResponse;
10.
11. import com.durgasoft.service.UserService;
12. public class LoginServlet extends HttpServlet {
13. private static final long serialVersionUID = 1L;
14. protected void doPost(HttpServletRequest request, HttpServletResponse response) thr
ows ServletException, IOException {
15. response.setContentType("text/html");
16. String uname = request.getParameter("uname");
17. String upwd = request.getParameter("upwd");
18. RequestDispatcher reqDispatcher = null;
19. UserService userService = new UserService();
20. String status = userService.checkLogin(uname, upwd);
21. if (status.equals("success")) {
22. reqDispatcher = request.getRequestDispatcher("success.html");
23. reqDispatcher.forward(request, response);
24. }else {
25. reqDispatcher = request.getRequestDispatcher("failure.html");
26. reqDispatcher.forward(request, response);
27. }
28.
29. }
30.
9
31.} 3
e
g
a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
web.xml
EX:UserService.java
0
4
1. package com.durgasoft.service; e
g
2. a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
4. Update Project:
1
1. Right Click on Project. 4
2. Select "Maven". e
g
a
3. Select "Update Project". P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
2
4
e
g
a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
EX:
1. <build>
2. <plugins>
3. <plugin>
4. <groupId>org.apache.maven.plugins</groupId>
3
5. <artifactId>maven-compiler-plugin</artifactId>
4
6. <version>3.7.0</version> e
g
a
7. <configuration> P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
EX:
1. <dependencies>
2. <dependency>
3. <groupId>org.springframework</groupId>
4. <artifactId>spring-core</artifactId>
5. <version>4.3.18.RELEASE</version>
6. </dependency>
7.
8. <dependency>
9. <groupId>org.springframework</groupId>
10. <artifactId>spring-context</artifactId>
11. <version>4.3.18.RELEASE</version>
12. </dependency>
13.
14. <dependency>
15. <groupId>org.springframework</groupId>
16. <artifactId>spring-aop</artifactId>
17. <version>4.3.18.RELEASE</version>
18. </dependency>
19. </dependencies>
Note: In the above dependencies, just add spring-core and spring-context dependencies in
pom.xml file then automatically all other dependencies are also be loaded.
4
4
e
g
a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
EX: pom.xml
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
EX: HelloBean.java
1. package com.durgasoft.beans;
2.
3. public class HelloBean {
4. public String sayHello() {
5. return "Hello User!";
6. }
7. }
EX: applicationContext.xml
7
c) Prepare Test Application: 4
e
g
a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]
EX:TestClient.java
1. package com.durgasoft.test;
2.
3. import org.springframework.context.ApplicationContext;
4. import org.springframework.context.support.ClassPathXmlApplicationContext;
5.
6. import com.durgasoft.beans.HelloBean;
7.
8. public class TestClient {
9. public void test() {
10. ApplicationContext context = new ClassPathXmlApplicationContext("com/durgasoft/re
sources/applicationContext.xml");
11. HelloBean bean = (HelloBean) context.getBean("helloBean");
12. String message = bean.sayHello();
13. System.out.println(message);
14. }
15.}
EX:TestApp.java
1. package com.durgasoft;
2.
3. import com.durgasoft.test.TestClient;
4.
5. import junit.framework.Test;
6. import junit.framework.TestCase; 8
4
7. import junit.framework.TestSuite; e
g
8. public class AppTest a
P
CONTACT US:
Mobile: +91- 8885 25 26 27 Mail ID: [email protected]