Task - Spring Maven - FooBar Maven App #34
Replies: 15 comments
BeansFoo and Bar Classpackage spring.foobar.maven.demo;
public class Foo {
private String name;
public Foo(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Foo [name=" + name + "]";
}
}
package spring.foobar.maven.demo;
public class Bar {
private String name;
private int age;
private Foo foo;
public Bar() {
}
public Bar(String name, int age, Foo foo) {
super();
this.name = name;
this.age = age;
this.foo = foo;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Foo getFoo() {
return foo;
}
public void setFoo(Foo foo) {
this.foo = foo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Bar [name=" + name + ", age=" + age + ", foo=" + foo + "]";
}
}Main Applicationpackage spring.foobar.maven.demo;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("our Spring App");
Foo fooObj = context.getBean("foo", Foo.class);
Bar barObj = context.getBean("bar", Bar.class);
barObj.setFoo(fooObj);
System.out.println(barObj);
System.out.println(fooObj);
context.close();
}
}Application Properties Xml<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:context = "http://www.springframework.org/schema/context"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id = "foo" class = "spring.foobar.maven.demo.Foo">
<constructor-arg index = "0" value = "Anandhi"></constructor-arg>
</bean>
<bean id = "bar" class = "spring.foobar.maven.demo.Bar">
<property name = "age" value = "24"></property>
<property name = "name" value = "Anu"></property>
<property name = "foo" ref = "foo"></property>
</bean>
</beans> |
BeansFoo.javapackage com.spring.maven.demo.beans;
public class Foo {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Foo(String name) {
this.name = name;
}
public Foo() {
}
@Override
public String toString() {
return "Foo [name=" + name + "]";
}
}Bar.javapackage com.spring.maven.demo.beans;
public class Bar {
private String name;
private int age;
private Foo foo;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Foo getFoo() {
return foo;
}
public void setFoo(Foo foo) {
this.foo = foo;
}
public Bar(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Bar() {
super();
}
@Override
public String toString() {
return "Bar [name=" + name + ", age=" + age + "]";
}
}
Driverpackage com.spring.maven.demo;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.maven.demo.beans.Bar;
import com.spring.maven.demo.beans.Foo;
public class Application {
public static void main(String[] args) {
// TODO Auto-generated method stub
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("Welcome to spring Assignment");
Bar bar = context.getBean("bar", Bar.class);
System.out.println(bar);
System.out.println(bar.getFoo());
// getting the bean only with id
Foo foo = (Foo) context.getBean("foo");
System.out.println(foo);
context.close();
}
}Application Context Configuration<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:context = "http://www.springframework.org/schema/context"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="foo" class="com.spring.assignment.beans.Foo">
<constructor-arg index="0" value="ABC"></constructor-arg>
</bean>
<bean id="bar" class="com.spring.assignment.beans.Bar">
<constructor-arg type="String" value="GK"></constructor-arg>
<constructor-arg type="int" value="101"></constructor-arg>
<property name="foo" ref="foo"></property>
</bean>
</beans>screenshot |
|
//Visweshwaran M BeansFoo.javapublic class Foo {
private String name;
public Foo() {
}
public Foo(String name) {
this.setName(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return new StringBuilder().append("Foo [name=").append(name).append("]").toString();
}
}Bar.javapublic class Bar {
private String name;
private int age;
private Foo foo;
public Bar() {
}
public Bar(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Foo getFoo() {
return foo;
}
public void setFoo(Foo foo) {
this.foo = foo;
}
@Override
public String toString() {
return new StringBuilder().append("Bar [name=").append(name).append(", age=").append(age).append(", foo=")
.append(foo).append("]").toString();
}
public void printDataVar() {
System.out.println(new StringBuilder().append("\n First Name: ").append(this.name).append("\n Last Name: ")
.append(this.foo.getName()).append("\n Age: ").append(this.age).append("\n"));
}
}Driverpublic class Application {
public static void main(String[] args) {
System.out.println("Application.main() Started");
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Bar bar = context.getBean(Bar.class);
bar.printDataVar();
context.close();
System.out.println("Application.main() Ended");
}
}Application Context Configuration<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:context = "http://www.springframework.org/schema/context"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="foo" class="com.spring.foobar.Foo" >
<constructor-arg index="0" value="Muralidaran" ></constructor-arg>
</bean>
<bean id="bar" class="com.spring.foobar.Bar">
<constructor-arg type="String" value="Visweshwaran"></constructor-arg>
<constructor-arg type="int" value="26"></constructor-arg>
<property name="foo" ref="foo"></property>
</bean>
</beans>
Project Object Model - XML<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spring.app2</groupId>
<artifactId>first-maven-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>first-maven-project</name>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
</dependencies>
</project>Screenshots |
Foopackage com.example.springmavenfoobar; public class Foo { public Foo(String name) { public String getName() { public void setName(String name) { } @OverRide } Barpackage com.example.springmavenfoobar; public class Bar { public Bar() { } public Bar(String name, int age, Foo foo) { public int getAge() { public void setAge(int age) { public Foo getFoo() { public void setFoo(Foo foo) { public String getName() { public void setName(String name) { @OverRide Mainpackage com.example.springmavenfoobar; import org.springframework.context.support.AbstractApplicationContext; public class Main { public static void main(String[] args) { } ApplicationContext<beans xmlns = "http://www.springframework.org/schema/beans" SS |
Bar Classpublic class Bar {
String name;
int age;
Foo foo;
public Bar() {
super();
}
public Bar(String name, int age, Foo foo) {
super();
this.name = name;
this.age = age;
this.foo = foo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Foo getFoo() {
return foo;
}
public void setFoo(Foo foo) {
this.foo = foo;
}
@Override
public String toString() {
return "Boo [name=" + name + ", age=" + age + ", foo=" + foo + "]";
}
}Foo Class
public class Foo {
String name;
public Foo() {
super();
}
public Foo(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "[name=" + name + "]";
}
}ApplicationContext<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:context = "http://www.springframework.org/schema/context"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id = "foo" class = "com.spring.foobar.Foo">
<constructor-arg index = "0" value = "poorani"></constructor-arg>
</bean>
<bean id = "bar" class = "com.spring.foobar.Bar">
<property name = "age" value = "23"></property>
<property name = "name" value = "Thiru"></property>
<property name = "foo" ref = "foo"></property>
</bean>
</beans>Applicationpublic class Application {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Foo foo= context.getBean("foo", Foo.class);
Bar bar = context.getBean("bar", Bar.class);
bar.setFoo(foo);
System.out.println("My Details :"+bar);
System.out.println("Foo :"+foo);
context.close();
}
}
``Output |
Foo.javapackage com.maven.beans;
public class Foo {
private String name;
public Foo() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Foo(String name) {
this.name = name;
}
public void greetUser() {
System.out.println("Name"+" "+this.name);
}
@Override
public String toString() {
return "Foo [name=" + name + "]";
}
} |
Bar.javapackage com.maven.beans;
public class Bar {
private String name;
private int age;
private Foo foo;
public Bar() {
}
public Bar(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Foo getFoo() {
return foo;
}
public void setFoo(Foo foo) {
this.foo = foo;
}
public void greetUser() {
System.out.println("Personal Details"+" "+this.name+" "+this.age);
}
@Override
public String toString() {
return "Bar [name=" + name + ", age=" + age + "]";
}
} |
Applicationpackage com.maven.beans;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application {
public static void main(String[] args) {
// TODO Auto-generated method stub
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("Spring Maven Application Started!");
Bar bar = context.getBean("bar", Bar.class);
bar.greetUser();
Foo foo = context.getBean("foo", Foo.class);
foo.greetUser();
context.close();
}
} |
ApplicationContext.xml<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- bean definitions here -->
<bean id="foo" class="com.maven.beans.Foo">
<constructor-arg type="java.lang.String" value="Kalai"></constructor-arg>
</bean>
<bean id="bar" class="com.maven.beans.Bar">
<constructor-arg type="java.lang.String" value="Kalai"></constructor-arg>
<constructor-arg type="int" value="25"></constructor-arg>
<property name="foo" ref="foo"></property>
</bean>
</beans> |
|
applicationContext.xml <beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:context = "http://www.springframework.org/schema/context"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="foo" class="com.spring.maven.foobar.beans.Foo">
<constructor-arg index="0" value="Thandapani"></constructor-arg>
</bean>
<bean id="bar" class="com.spring.maven.foobar.beans.Bar">
<constructor-arg type="int" value="24"></constructor-arg>
<constructor-arg type="java.lang.String" value="Kanimozhi"></constructor-arg>
<property name="foo" ref="foo"></property>
</bean>
</beans> Application.java package com.spring.maven.foobar;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.maven.foobar.beans.Foo;
import com.spring.maven.foobar.beans.Bar;
public class Application {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");//Launches the Application
System.out.println("Our Spring Maven Demo Work Starts!!");
Foo foo = context.getBean("foo",Foo.class);
System.out.println(foo);
Bar bar = context.getBean("bar",Bar.class);
System.out.println(bar);
bar.printDataVar();
context.close();//Shut Down the App
}
}Foo.java package com.spring.maven.foobar.beans;
public class Foo{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Foo() {}
public Foo(String name) {
this.name = name;
}
@Override
public String toString() {
return "Foo[LastName="+ name +"]";
}
}Bar.java package com.spring.maven.foobar.beans;
import com.spring.maven.foobar.beans.Foo;
public class Bar{
private String name;
private int age;
private Foo foo;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setFoo(Foo foo) {
this.foo = foo;
}
public Foo getFoo() {
return foo;
}
//Constructors
public Bar() {};
public Bar(String name,int age) {
this.name = name;
this.age = age;
}
public void processFooName() {
System.out.println("Name in Injected Foo is:"+foo.getName());
}
@Override
public String toString() {
return "Bar[age="+age+" ,foo="+foo+",FirstName="+name+"]";
}
public void printDataVar() {
System.out.println(new StringBuilder().append("\n First Name: ").append(this.name).append("\n Last Name: ")
.append(this.foo.getName()).append("\n Age: ").append(this.age).append("\n"));
}
}pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spring</groupId>
<artifactId>spring-first-maven-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-first-maven-project</name>
<description>Spring Application With Maven</description>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<properties>
<spring.version>5.3.19</spring.version>
</properties>
</project> |
Foobar Apppackage com.spring.maven.foobar;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.maven.foobar.beans.Bar;
import com.spring.maven.foobar.beans.Foo;
public class FoobarApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//Launches Application Context
System.out.println("Spring Foobar app works..");
Foo f = context.getBean("foo",Foo.class);
Bar b = context.getBean("bar",Bar.class);
System.out.println(f);
System.out.println(b);
context.close();//close application
}
}Foopackage com.spring.maven.foobar.beans;
public class Foo {
private String name;
public Foo(String name) {
super();
this.name = name;
}
public Foo() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Foo [name=" + name + "]";
}
}Barpackage com.spring.maven.foobar.beans;
public class Bar {
private String name;
private int age;
Foo foo;
public Bar() {
super();
}
public Bar(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Foo getFoo() {
return foo;
}
public void setFoo(Foo foo) {
this.foo = foo;
}
@Override
public String toString() {
return "Bar [name=" + name + ", age=" + age + ", foo=" + foo + "]";
}
}
applicationContext.xml<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:context = "http://www.springframework.org/schema/context"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="foo" class="com.spring.maven.foobar.beans.Foo">
<constructor-arg index="0" value="testFoo"></constructor-arg>
</bean>
<bean id="bar" class="com.spring.maven.foobar.beans.Bar">
<constructor-arg type="int" value="32"></constructor-arg>
<constructor-arg type="java.lang.String" value="Manish Malhotra"></constructor-arg>
<property name="foo" ref="foo"></property>
</bean>
</beans>pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spring</groupId>
<artifactId>foobar-maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>foobar-maven</name>
<description>foobar spring application with maven</description>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<properties>
<spring.version>5.3.20</spring.version>
</properties>
</project>
|
|
https://github.com/harsha916/mavenFooBar |
|
Foo package com.spring.FooBar.maven.beans;
public class Foo {
private String name;
public Foo() {
super();
}
public Foo(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Foo [name = " + name + "]";
}
}Bar package com.spring.FooBar.maven.beans;
public class Bar {
private String name;
private int age;
private Foo foo;
public Bar() {
super();
}
public Bar(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Foo getFoo() {
return foo;
}
public void setFoo(Foo foo) {
this.foo = foo;
}
@Override
public String toString() {
return "Bar [name = " + name + ", age = " + age + "]";
}
}DriverClass package com.spring.FooBar.maven;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.FooBar.maven.beans.Bar;
import com.spring.FooBar.maven.beans.Foo;
public class ApplicationContext {
public static void main(String[] args) {
// TODO Auto-generated method stub
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.start();
Foo fo = context.getBean("Foo",Foo.class);
System.out.println(fo);
Bar ba = context.getBean("Bar",Bar.class);
System.out.println(baA);
context.close();
}
}pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spring</groupId>
<artifactId>spring-FooBar-maven-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-FooBar-maven-project</name>
<description>Spring FooBar application as a Maven project </description>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<properties>
<spring.version>5.3.20</spring.version>
</properties>
</project>
applicationContext.xml <beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:context = "http://www.springframework.org/schema/context"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="Foo" class="com.spring.FooBar.maven.beans.Foo">
<constructor-arg index="0" value="sankalp selokar Foo"></constructor-arg >
</bean>
<bean id="Bar" class="com.spring.FooBar.maven.beans.Bar">
<constructor-arg index="0" value="sankalp selokar Bar"></constructor-arg >
<constructor-arg index="1" value="21"></constructor-arg >
</bean>
</beans> |
Foopackage com.spring.foobar.maven.beans;
public class Foo {
private String name;
public Foo() {
super();
}
public Foo(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}Barpackage com.spring.foobar.maven.beans;
public class Bar {
private String name;
private int age;
private Foo foo;
public Bar() {
super();
}
public Bar(String name, int age) {
super();
this.name = name;
this.age = age;
}
public void setFoo(Foo foo){
this.foo = foo;
}
public void processFooName() {
System.out.println("Name in Injected Foo is: " + foo.getName());
}
@Override
public String toString(){
return "Bar has name = " + this.name + " and age = " + this.age;
}
}DriverClasspackage com.spring.foobar.maven;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.foobar.maven.beans.Bar;
import com.spring.foobar.maven.beans.Foo;
public class SpringMavenFoobarApplication {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("application-context-config.xml");
Foo foo = context.getBean("foo", Foo.class);
Bar bar = context.getBean("bar", Bar.class);
System.out.println(foo);
System.out.println(bar);
context.close();
}
}pom.xml<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spring</groupId>
<artifactId>spring-foobar-maven-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-foobar-maven-project</name>
<description>Spring foobar application as a maven project</description>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<properties>
<spring.version>5.3.20</spring.version>
</properties>
</project>
application-context-config.xml<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:context = "http://www.springframework.org/schema/context"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="foo" class="com.spring.foobar.maven.beans.Foo">
<constructor-arg index="0" value="Jits"></constructor-arg>
</bean>
<bean id="bar" class="com.spring.foobar.maven.beans.Bar">
<constructor-arg index="0" value="Jits"></constructor-arg>
<constructor-arg index="1" value="25"></constructor-arg>
</bean>
</beans>
|



Uh oh!
There was an error while loading. Please reload this page.
Create the previously demonstrated FooBar Spring application with Maven.
You can refer to discussion #30 for ideas and inspiration. The code structure will be mostly the same. The project will become a Maven project, instead of a Java Project. Thus, some of the files need to be kept in the Maven-specific directories. Define the pom.xml.
You can use the provided template for submitting your solution (Edit the discussion, copy the content, and add your own submissions under the appropriate sections).
All reactions