Objective
Link: http://websystique.com/spring/spring-4-hello-world-example-annotation-tutorial-full-example/
This tutorial will show a spring 4 Hello World example based on spring annotation configuration, explaining the basic concepts and usage of Spring 4.
As well as providing an XML-based configuration example as a comparison, we will create a MAVEN-based project that uses the spring version of 4.0.6.
Relevant technology and development tools involved
- Spring 4.0.6.RELEASE
- Maven 3
- JDK 1.6
- Eclipse JUNO Service Release 2
Catalog of engineering Structures
The following is the final structure of the project directory:
If you want to learn how to create a maven project, please refer to the following link: Maven tutorial. It contains detailed steps to explain how to create a MAVEN project.
So, next, let's add something to the above directory structure.
Step One: Add Spring dependencies in Maven's Pom.xml
We can add all dependencies to the MAVEN project through Pom.xml, as follows:
<Projectxmlns= "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, where the spring annotations are defined in Spring-context.
Step Two: Create the Pojo class
Spring advocates low-coupling and interface-based programming.
So, first we create a POJO interface and its implementation class, and this pojo will act as a spring bean.
Package publicinterface HelloWorld { void SayHello ( String name);}
Package publicclassimplements helloworld{ public void SayHello (String name) { System.out.println ("Hello" +name);} }
Step three: Create a spring configuration file
The spring configuration class contains definitions for some of the beans to be used in the project. @Configuration Note Declaration This class is a spring configuration class that can contain a method for @bean annotations that creates a bean to be given to spring container management.
Packagecom.websystique.spring.configuration;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;Importorg.springframework.context.annotation.Description; @Configuration Public classhelloworldconfig {@Bean (name= "Helloworldbean") @Description ("This is a sample HelloWorld Bean") PublicHelloWorld HelloWorld () {return NewHelloworldimpl (); } }
@Description is a new annotation introduced by spring 4 to describe the definition of a bean
The above annotation-based configuration has the same effect as the following spring XML configuration (we name this file helloworld-config.xml)
<Beansxmlns= "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"> <BeanID= "Helloworldbean"class= "Com.websystique.spring.domain.HelloWorldImpl"> </Beans>
Step four: Create the Main method to execute the program
Packagecom.websystique.spring;ImportOrg.springframework.context.annotation.AnnotationConfigApplicationContext;ImportOrg.springframework.context.support.AbstractApplicationContext;ImportCom.websystique.spring.configuration.HelloWorldConfig;ImportCom.websystique.spring.domain.HelloWorld; Public classAppmain { Public Static voidMain (String args[]) {Abstractapplicationcontext context=NewAnnotationconfigapplicationcontext (Helloworldconfig.class); HelloWorld Bean= (HelloWorld) context.getbean ("Helloworldbean"); Bean.sayhello ("Spring 4"); Context.close (); } }
AnnotationConfigApplicationContext以我们的配置类(用@Configuration标注)作为入参创建spring应用上下文环境,在程序运行时注册所有在配置类中定义的bean。一旦我们创建了AnnotationConfigApplicationContext对象,就可以通过其getBean
方法从spring容器中得到指定的bean对象,并且可以调用该
The method of the Bean object performs some operations.
HelloWorld bean = (HelloWorld) context.getbean ("Helloworldbean"); Bean.sayhello ("Spring 4");
Running the above program, you will get the following results:
Hello Spring 4
If you use an XML-based configuration, the main method above should write:
Packagecom.websystique.spring;ImportOrg.springframework.context.support.AbstractApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.websystique.spring.domain.HelloWorld; Public classAppmain { Public Static voidMain (String args[]) {Abstractapplicationcontext context=NewClasspathxmlapplicationcontext ("Helloworld-config.xml"); HelloWorld Bean= (HelloWorld) context.getbean ("Helloworldbean"); Bean.sayhello ("Spring 4"); Context.close (); } }
Where the Helloworld-config.xml file was created in step three, under engineering classpath (e.g./src/main/resources).
Conclusion
In the next article, we'll see an example of a bean's auto-detection, which is based on annotations that automatically finds all the beans in the application environment without having to declare them in a configuration class.
This example source code
http://websystique.com/?smd_process_download=1&download_id=778
Spring 4 Hello World Example