Example of Spring 4 Hello World, springhello

Source: Internet
Author: User

Example of Spring 4 Hello World, springhello
Preface

Http://websystique.com/spring/spring-4-hello-world-example-annotation-tutorial-full-example/.

This tutorial will show an example of Spring 4 Hello world configured based on Spring annotations, explaining the basic concepts and usage of Spring 4.

The XML-based configuration example is also provided as a comparison between the two. We will create a maven-based project using spring version 4.0.6.

Related Technologies and Development Tools
  • Spring 4.0.6.RELEASE
  • Maven 3
  • JDK 1.6
  • Eclipse JUNO Service Release 2
Project Structure directory

The following is the final structure directory of the project:

To learn how to create a maven project, see the following link: Maven tutorial. It contains detailed steps to illustrate how to create a maven project.

Next, let's add specific content to the above directory structure.

Step 1: Add spring dependency in maven pom. xml

We can add all dependencies to the maven project through pom. xml, as shown 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>com.websystique.spring</groupId>    <artifactId>Spring4HelloWorldExample</artifactId>    <version>1.0.0</version>    <packaging>jar</packaging>     <name>Spring4HelloWorldExample</name>     <properties>        <springframework.version>4.0.6.RELEASE</springframework.version>    </properties>     <dependencies>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>${springframework.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>${springframework.version}</version>        </dependency>    </dependencies>    <build>        <pluginManagement>            <plugins>                <plugin>                    <groupId>org.apache.maven.plugins</groupId>                    <artifactId>maven-compiler-plugin</artifactId>                    <version>3.2</version>                    <configuration>                        <source>1.6</source>                        <target>1.6</target>                    </configuration>                </plugin>            </plugins>        </pluginManagement>    </build> </project>

For this example, we only need to add Spring core and Spring context dependencies. spring annotations are defined in spring-context.

Step 2: Create a POJO class

Spring advocates low coupling and interface-based programming.

Therefore, we first create a POJO interface and its implementation class, which will act as a spring bean.

package com.websystique.spring.domain; public interface HelloWorld {     void sayHello(String name);}
package com.websystique.spring.domain; public class HelloWorldImpl implements HelloWorld{     public void sayHello(String name) {        System.out.println("Hello "+name);    } }
Step 3: Create a Spring configuration file

The Spring configuration class contains some bean definitions to be used in the project. @ Configuration annotation declares that this class is a Spring Configuration class, which can contain @ Bean annotation methods. This method creates beans for spring container management.

package com.websystique.spring.configuration; import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Description; @Configurationpublic class HelloWorldConfig {     @Bean(name="helloWorldBean")    @Description("This is a sample HelloWorld Bean")    public HelloWorld helloWorld() {        return new HelloWorldImpl();    } }

@ Description is a new annotation introduced by Spring 4 to describe bean definitions.

The above annotation-based configuration has the same effect as the following Spring XML configuration (we name this file as a helloworld-config.xml)

<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-4.0.xsd">     <bean id="helloWorldBean" class="com.websystique.spring.domain.HelloWorldImpl">  </beans>
Step 4: Create the main method and execute the program
package com.websystique.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.support.AbstractApplicationContext; import com.websystique.spring.configuration.HelloWorldConfig;import com.websystique.spring.domain.HelloWorld; public class AppMain {     public static void main(String args[]) {        AbstractApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfig.class);        HelloWorld bean = (HelloWorld) context.getBean("helloWorldBean");        bean.sayHello("Spring 4");        context.close();    } }

AnnotationConfigApplicationContext uses our Configuration class (marked with @ Configuration) as the input parameter to create the spring application context environment, and registers all beans defined in the Configuration class when the program is running. Once the AnnotationConfigApplicationContext object is createdgetBeanMethod to get the specified bean object from the spring container, and you can callBean object method to perform some operations.

HelloWorld bean = (HelloWorld) context.getBean("helloWorldBean");bean.sayHello("Spring 4");

Run the above program and you will get the following results:

Hello Spring 4

If XML-based configuration is used, the above main method should be written as follows:

package com.websystique.spring; import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext; import com.websystique.spring.domain.HelloWorld; public class AppMain {     public static void main(String args[]) {        AbstractApplicationContext context = new ClassPathXmlApplicationContext("helloworld-config.xml");        HelloWorld bean = (HelloWorld) context.getBean("helloWorldBean");        bean.sayHello("Spring 4");        context.close();     } }

The helloworld-config.xml file is created in step 3 and is located under the project classpath (for example,/src/main/resources ).

Conclusion

In the next article, we will see an example of automatic bean detection. Based on annotations, this feature can automatically find all beans in the application environment without declaring them in the configuration class.

Source code in this example

Http://websystique.com /? Smd_process_download = 1 & download_id = 778

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.