Thursday, April 27, 2017

How to create spring bean with Maven Project

First step is to create the Maven Project and given name as you want. For me I am going to create the project name "Maven_DEMO_BLOG", after you create the project you expand the project structure to see other folders...


We will create the package by right click on folder "src/main/java" and go to New -> package and given name as com.ratanak.demo (as you prefer). In that package we will create a User class. And then create id, name, sex as field and setter/getter method for it.

package com.ratanak.demo;
public class User {
//Field
private int id;
private String name;
private String sex;
//Constructor
public User(){
}
public User(int id, String name, String sex) {
super();
this.id = id;
this.name = name;
this.sex = sex;
}
//Setter and Getter
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
view raw User.java hosted with ❤ by GitHub


To work with bean we need 3 dependencies like: spring-beans, spring-core, spring-context and you add it in pom.xml as below :

<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>Maven_Demo_BLOG</groupId>
<artifactId>Maven_Demo_BLOG</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Maven_Demo_BLOG</name>
<description>Maven_Demo_BLOG</description>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.1.RELEASE</version>
</dependency>
</dependencies>
</project>
view raw pom.xml hosted with ❤ by GitHub



Let's create beans and name it as beans.xml. You can right click on "src/main/java" and type spring in the search box, for mine at first the spring feature is not appeared so I install Spring IDE from Eclipse Marketplace. If you don't have you can do like me.
Click on "Spring Bean Configuration File" and name it as "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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- class properties is point to the User class, You can click on it to see if this bean can find the Class -->
<bean id="user" class="com.ratanak.demo.User">
<!-- Add 3 constructor according to class User constructor-->
<constructor-arg name="id" value="222"></constructor-arg>
<constructor-arg name="name" value="Ratanak Pek"></constructor-arg>
<constructor-arg name="sex" value="Male"></constructor-arg>
</bean>
</beans>
view raw beans.xml hosted with ❤ by GitHub

Finally we will create a demo class "Demo_Bean.java" and some code as below. In this code, we are trying to get User Object from beans Container and and assign to User Object. After the object had valued, so we print it out to the console.

package com.ratanak.demo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo_Bean {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ac = new ClassPathXmlApplicationContext("com/ratanak/demo/beans.xml");
//Get Users Object from beans and assign to user class
User user = (User) ac.getBean("user");
System.out.println("*****************************");
System.out.println("User ID : "+ user.getId());
System.out.println("Name : "+ user.getName());
System.out.println("Sex : "+ user.getSex());
System.out.println("*****************************");
}
}
view raw Demo_Bean.java hosted with ❤ by GitHub


Here is the result that we print it to console:


Download Source Code



No comments:

Post a Comment