In this example we will learn how to declare a bean in Spring Application. We are going to build a simple Maven project to demonstrate it. So let’s begin by setting up our Maven project.
Creating a Maven Project
Below is the directory structure of our Maven Project.
.
├─ pom.xml
└─ src
└─ main
├─ java
│ └─ org
│ └─ kodejava
│ └─ spring
│ └─ core
│ ├─ Hello.java
│ ├─ HelloImpl.java
│ └─ HelloWorldDemo.java
└─ resources
└─ spring.xml
Configuring Maven pom.xml
File
We need to create a pom.xml
file and add our project configuration and library dependency.
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>kodejava-example</artifactId>
<groupId>org.kodejava</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>kodejava-springframework-core</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.3.23</version>
</dependency>
</dependencies>
</project>
Creating a Bean
Next we will create a simple bean called HelloImpl
. This bean implements an interface called Hello
with a single method sayHello()
to be implemented. Here is the interface it’s implementation definition.
package org.kodejava.spring.core;
public interface Hello {
void sayHello();
}
package org.kodejava.spring.core;
public class HelloImpl implements Hello {
public void sayHello() {
System.out.println("Hello World!");
}
}
Register the Bean in Spring Configuration
After having the bean we need to create the Spring configuration, which is an xml file, and we named it spring.xml
. The bean declared using the bean
element in the configuration file. At minimum the declaration contains the bean’s id
and it’s class
.
<?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.xsd">
<bean id="hello" class="org.kodejava.spring.core.HelloImpl" />
</beans>
Use the Bean in Our Application
Now we have the bean declared in the Spring container. The next step show you how to get the bean from the container and use it in our program. There are many ways that can be used to load the Spring container. Here we will use the ClassPathXmlApplicationContext
. This class load the configuration that found in the runtime classpath.
package org.kodejava.spring.core;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloWorldDemo {
public static void main(String[] args) {
String config = "spring.xml";
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
Hello hello = (Hello) context.getBean("hello");
hello.sayHello();
context.close();
}
}
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024