Hello world spring IOC - the first spring program

Keywords: Java Spring xml encoding

Take Learning seriously

Recently, I reread spring's official documents to record and share useful high-frequency matches.

brief introduction

Control Inversion (IoC) is also called Dependency Injection (DI). It is a process in which an object defines its dependencies, and its dependencies are other objects that cooperate with it. This process can only be achieved by constructing method parameters, factory method parameters, or setting its properties by setter method after being constructed or returned from the factory method. The container then injects these dependencies when creating beans. This process is essentially the reverse, in which the bean itself controls instantiation, or locates its own dependencies directly through the class structure or Service locator pattern, hence the name control inversion.

container

The org. spring framework. context. ApplicationContext container interface in spring. In stand-alone applications, it is common to create instances of ClassPathXml ApplicationContext or FileSystem Xml ApplicationContext, which implement the ApplicationContext interface. In general application scenarios, spring containers are not currently created, such as web environments... But the simplest part of the exercise is to use simple Java applications to learn, which can also help you understand the configuration in the future.

metadata

Metadata is usually configurated through xml. Versions after spring 2.5 support annotations. Versions after spring 3.0 support the configuration of java interfaces. But both of these configurations violate the principle of separation of code and configuration. At the same time, the configuration provided by XML is more diversified, so I do not recommend using them (although annotations can improve the opening up). Hair efficiency). In the examples below, we use XML configuration.

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 this bean go here -->
    </bean>

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions go here -->

</beans>

This is the simplest configuration for spring bean s. id is the unique identifier of the bean, and class is the class of the bean. Let's build our first helloworld.

jar

To use spring ioc, the prime minister = the prime minister introduces the jar package of spring IOC

pom.xml

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

 

Establishing metadata

public class Event {
    private Long id;

    private String title;
    private Date date;

    public Event() {
        // this form used by Hibernate
    }

    public Event(String title, Date date) {
        // for application use, to create new events
        this.title = title;
        this.date = date;
    }

    public Long getId() {
        return id;
    }

    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 setTitle(String title) {
        this.title = title;
    }
}

 

Configure the 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 this bean go here -->
    </bean>
    
  

    <!-- more bean 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);
    
        
    }

}

 

Operation results.

com.wanda.entity.Event@5c3cee0d

So spring helped us build an Event object.

This is the next section of a spring introduction. Let's take a look at the key points.

Posted by Permutant on Tue, 25 Dec 2018 01:06:06 -0800