Hello World Spring ioc--First Spring program

Source: Internet
Author: User

Take a serious approach to learning

Recently re-read the spring official document to record and share useful high-frequency matching.

Brief introduction

The inversion of Control (IoC) is also known as Dependency Injection (DI). It is the process by which an object defines its dependencies, and its dependencies are other objects that work with it, which can be implemented only by constructing method parameters, factory method parameters, or by constructing or returning from a factory method by setting its properties through the Settter method. The container then injects these dependencies when the bean is created. This process is essentially in turn, controlled by the bean itself, or by locating its own dependencies directly through the class's structure or service locator pattern, so it is called control reversal.

Container

The org.springframework.context.ApplicationContext container interface in spring, which is typically created in a standalone application Classpathxmlapplicationcontext or Filesystemxmlapplicationcontext instances, they implement the ApplicationContext interface. In general scenarios, creating a spring container, such as a web environment, does not exist. But the simplest thing to do in practice is to use a simple Java application to learn, or to help you understand the configuration later.

Meta data

Metadata is typically configured in XML, spring2.5 later versions support the use of annotations, the spring3.0 version supports the configuration of the Java interface, but both of these configurations violate the principle of separation of code from configuration, while the XML provides a more diversified configuration, so I'm not quite suggesting it (but using annotations can improve development efficiency). In the example below, we are using the XML configuration method.

The simplest configuration file.

<?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 ">    <bean id=" ... "class=" ... ">        <!--Collaborators and configuration for the This bean go-here-and    </bean>    <bean id=" ... "class=" ... "> <!--Collaborators and configuration for the This bean go----and    </bean>    <! --More beans definitions go here--></beans>

This is the simplest configuration for spring beans. The ID is the bean's unique identifier, class is the bean's classes, let's build our first HelloWorld.

Jar

Want to use spring IOC, first = Prime Minister to introduce the spring IOC jar package

Pom. XML

<dependency>            <groupId>org.springframework</groupId>            <artifactid>spring-context </artifactId>            <version>4.3.9.RELEASE</version>        </dependency>

Building metadata

public class Event {    private Long ID;    Private String title;    private date date;    Public event () {        //This form used to Hibernate } public event (String title, date date) {//For Applica tion use, to-create new events this.title = title; this.date = date;} public Long getId () {return ID ; The private void setId (Long id) {this.id = ID;} public Date GetDate () {return date,} public void
                  
                    SetDate (date date) {this.date =
                    date;} public String GetTitle () {return title, public void sett Itle (String title) {this.title = title;}}     
                        

Configuration XML Name Event.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 ">    <bean id=" Event "class = "Full Write package name." Event ">        <!--Collaborators and configuration for the This bean go all-in    </bean>          <!--more be An definitions go here--></beans>

Test class

public class Springtest {    @SuppressWarnings ("resource") public    static void main (string[] args) {        ApplicationContext context = new Classpathxmlapplicationcontext ("Spring_bean_cfg.xml");        Event bean = Context.getbean (Event.class);            System.out.println (bean);}                }    

Run the results.

[Email protected]

So spring helped us set up an event object.

This is a spring introduction the next section we analyze the key points inside.

Hello World Spring ioc--First Spring program

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.