Hello All, In this Spring framework tutorial, you lot volition acquire how to write the hi globe illustration inwards the Spring framework. This should hold out your start tutorial to start learning Spring framework, every bit it gets the ball rolling. While coding in addition to running this example, you lot acquire a lot most Spring framework, Spring XSD files, necessary JAR files, in addition to to a greater extent than importantly how Spring framework works. This HelloWorld computer program inwards Spring framework is an extension of the classical Java hi globe program, which you lot powerfulness conduct hold seen earlier. This computer program is written using the Dependency Injection blueprint pattern by using the Spring Framework's IOC container. Even though similar a shot you lot tin laissez passer on the axe configure Spring dependency using annotations in addition to Java configuration, this illustration uses a traditional XML means to configure dependencies.
This is of import to acquire because in that place are many Spring-based Java projects, which are already alive inwards production in addition to yet using XML configuration. If you lot conduct hold to hold them, you lot must sympathize how to configure Spring dependency inwards the XML file.
Spring is i of the most pop Java application framework, which promotes to a greater extent than or less best practices piece creating a Java application like Dependency Injection and ease of testing. Spring provides an IOC container to contend life-cycle of Spring beans in addition to provides back upward to acquire beans whatsoever fourth dimension from IOC container.
Apart from Spring's IOC container, it besides provides rich API to simplify many of mutual Java tasks like JdbcTemplate help you lot to write JDBC code without taking aid of boilerplate things similar closing connection, statement, resultset etc.
Similarly, the Spring framework besides provides JmsTemplate to only JMS related chore similar sending in addition to receiving messages inwards Java.
Spring Framework is total of such goodness in addition to that's why I propose every Java developer who wants to acquire Spring is to acquire through a comprehensive course of report like Spring Framework 5: Beginner to Guru, which covers Spring really well.
Its start few lessons are really of import to acquire jump framework because they explicate to you lot how dependency injection provides improved testing, loose coupling, in addition to help inwards writing produce clean code you lot ever wanted.
This is of import to acquire because in that place are many Spring-based Java projects, which are already alive inwards production in addition to yet using XML configuration. If you lot conduct hold to hold them, you lot must sympathize how to configure Spring dependency inwards the XML file.
Spring is i of the most pop Java application framework, which promotes to a greater extent than or less best practices piece creating a Java application like Dependency Injection and ease of testing. Spring provides an IOC container to contend life-cycle of Spring beans in addition to provides back upward to acquire beans whatsoever fourth dimension from IOC container.
Apart from Spring's IOC container, it besides provides rich API to simplify many of mutual Java tasks like JdbcTemplate help you lot to write JDBC code without taking aid of boilerplate things similar closing connection, statement, resultset etc.
Similarly, the Spring framework besides provides JmsTemplate to only JMS related chore similar sending in addition to receiving messages inwards Java.
Spring Framework is total of such goodness in addition to that's why I propose every Java developer who wants to acquire Spring is to acquire through a comprehensive course of report like Spring Framework 5: Beginner to Guru, which covers Spring really well.
Its start few lessons are really of import to acquire jump framework because they explicate to you lot how dependency injection provides improved testing, loose coupling, in addition to help inwards writing produce clean code you lot ever wanted.
Spring in addition to Java HelloWorld Example
In this Spring tutorial, nosotros volition see one of the most unproblematic examples of dependency Injection like Hello Example. Message to Hello shape is provided yesteryear the Spring framework using Dependency Injection.
We conduct hold created a edible bean or a Java shape called Hello, which accepts a String message every bit a dependency. This jump edible bean is initialized using a jump configuration file like spring-config.xml. All beans declared inwards the jump configuration file is created in addition to managed yesteryear the Spring IOC container.
If you lot hold off Spring configuration file, hence you lot volition honor that id of the edible bean is "hello", which volition hold out farther used to acquire the reference of this edible bean from Spring framework using the getBean() method of ApplicationContext or BeanFactory class.
In guild to seek out this Spring HelloWorld example, nosotros conduct hold created a Main class, which has a classical public static void main(String args[]) method. In the main method, nosotros are creating an event of ClassPathXMLApplicationContext yesteryear providing spring-config.xml, which must hold out available inwards the classpath, inwards guild for Spring to initialize beans.
Next 2 lines of code are self-explanatory, where nosotros are getting the reference of Hello edible bean yesteryear using the "id" attribute of Hello edible bean declaration.
Btw, if you lot are a consummate beginner on Spring framework in addition to non familiar amongst essential Spring terminology similar Beans, ApplicationContext etc, hence I besides propose you lot to start acquire through set upward logging inwards every Java application because System.out.println() is non expert plenty for real-world Java application.
I conduct hold configured log4j using log4j.xml which is besides available inwards the classpath. By the way, in that place are mainly 2 ways to inject dependency using Spring, Constructor injection, in addition to Setter Injection, and this illustration uses setter injection to overstep the message to Hello object.
We conduct hold created a edible bean or a Java shape called Hello, which accepts a String message every bit a dependency. This jump edible bean is initialized using a jump configuration file like spring-config.xml. All beans declared inwards the jump configuration file is created in addition to managed yesteryear the Spring IOC container.
If you lot hold off Spring configuration file, hence you lot volition honor that id of the edible bean is "hello", which volition hold out farther used to acquire the reference of this edible bean from Spring framework using the getBean() method of ApplicationContext or BeanFactory class.
In guild to seek out this Spring HelloWorld example, nosotros conduct hold created a Main class, which has a classical public static void main(String args[]) method. In the main method, nosotros are creating an event of ClassPathXMLApplicationContext yesteryear providing spring-config.xml, which must hold out available inwards the classpath, inwards guild for Spring to initialize beans.
Next 2 lines of code are self-explanatory, where nosotros are getting the reference of Hello edible bean yesteryear using the "id" attribute of Hello edible bean declaration.
Btw, if you lot are a consummate beginner on Spring framework in addition to non familiar amongst essential Spring terminology similar Beans, ApplicationContext etc, hence I besides propose you lot to start acquire through set upward logging inwards every Java application because System.out.println() is non expert plenty for real-world Java application.
So this Spring hi globe illustration contains the next configuration files in addition to Java classes.
- Hello.java - Hello Bean which prints given message provided using jump dependency Injection
- Main.java - Test shape for this Spring HelloWorld example
- spring-config.xml - Spring configuration file which contains edible bean declaration
- log4j.xml - log4j configuration file
If you lot desire to implement this illustration using constructor injection hence you lot demand to brand a duet of changes inwards Hello shape in addition to Spring configuration file.
Make certain your Hello shape convey the String message inwards the constructor in addition to when you lot configure that edible bean inwards Spring configuration file, instead of using property, role the constructor-arg tag.
If you lot are interested, you lot tin laissez passer on the axe besides check Spring Master Class - Beginner to Expert course on Udemy for a duet of to a greater extent than examples on constructor injection in addition to writing hi globe using musical note inwards Spring 5.0.
Required Spring dependency JAR files
This Spring hi globe illustration uses Spring 3.1.2.RELEASE.jar files but you lot tin laissez passer on the axe role the version you lot desire similar Spring 5.1 every bit well. Though, it's meliorate to role Maven or Gradle for dependency injection because they volition automatically download the correct version of subject JAR files for you.
- spring-core-3.1.2.RELEASE.jar
- commons-logging-1.1.1.jar
- log4j-1.2.16.jar
- spring-context-3.1.2.RELEASE.jar
- spring-aop-3.1.2.RELEASE.jar
- aopalliance-1.0.jar
- spring-beans-3.1.2.RELEASE.jar
In guild to run this Spring hi globe example, exactly run the Main shape every bit Java application from ascendency employment or Eclipse IDE. Btw, if you lot are interested inwards learning Spring v in addition to Spring Boot 2 from scratch, inwards a guided, code-focused way, Learn Spring: The Certification Class Eugen is to a greater extent than or less other awesome course of report to endeavour out.
Spring HelloWorld Java Class
import org.apache.log4j.Logger;
/*
* Java shape which convey the message every bit dependency injected
*/
public shape Hello {
private static in conclusion Logger logger = Logger.getLogger(Hello.class);
private String message;
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void sayHello(){
logger.info(“Hello globe Spring message is:” + message); }
}
Main-Class to Test HelloWorld Bean
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/*
* Main shape to start in addition to seek out this Java application
*/
public shape Main {
public static void main(String args[]){
ApplicationContext context = new ClassPathXmlApplicationContext(
"spring-config.xml");
Hello hi = (Hello) context.getBean("hello");
hello.sayHello();
}
}
log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender" >
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-4r [%t] %-5p %c %x - %m%n" />
</layout>
</appender>
<logger name="org.springframework" >
<level value="ERROR" />
<appender-ref ref="console" />
</logger>
<logger name="org.apache">
<level value="DEBUG" />
<appender-ref ref="console" />
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
Spring Configuration File spring-config.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" xmlns:p="http://www.springframework.org/schema/p"
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="hello" class="Hello">
<property name="message" value="Good Afternoon" />
</bean>
</beans>
Output
Hello globe Spring message is: Good Afternoon
That's all on this Spring in addition to Java HelloWorld Example using Dependency Injection. For a Java programmer, noesis of Spring framework is speedily becoming from expert to have, to must conduct hold in addition to it's high fourth dimension to acquire in addition to role Spring framework piece writing Java applications.
Initially, it takes to a greater extent than or less fourth dimension to acquire used of XML in addition to Spring configuration file, diverse jump releases in addition to JAR files in addition to Spring API. Once you lot acquire concord of the basics of Spring framework and dependency Injection blueprint principle, writing Java computer program inwards Spring framework is actually fun.
In the next business office of this tutorial, You volition acquire how to write Spring hi globe using annotations in addition to Java configuration, hence rest tuned in addition to till hence taste the life, but if you lot can't hold off hither are to a greater extent than or less of the useful resources to acquire Spring evolution using Annotations in addition to Java Configuration.
Further Reading
Spring Framework 5: Beginner to Guru
courses)
P.S. - If you lot desire to acquire how to educate RESTful Web Service using Spring MVC inwards depth, I propose you lot bring together the REST amongst Spring master copy class yesteryear Eugen Paraschiv. One of the best course of report to acquire REST amongst Spring MVC.