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.



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




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".



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.



Here is the result that we print it to console:


Download Source Code



No comments:

Post a Comment