Spring4 learning review of the road 01-HelloWorld

Before the company has been using spring3.0, most recently started using the 4.0, 5.0 official website already have, but a lot of knowledge has been forgotten about, while the project is now busy writing essays, one to look back on my knowledge, and secondly, if I can help more than white white, that's better, from time to time update, Reviewing the Old.

Spring's official website ( https://spring.io )

Spring What is that?

spring is to simplify enterprise-class open source application development framework and students (mainly simplifies the development EJB2 the past), it is lightweight (non-invasive) and stop (the ability to integrate strong) framework, is also an IOC ( the DI) (dependency injection) and the AOP (aspect-oriented programming) of the container (including the management of the life cycle of application objects) frame.

Spring brings java second spring, so some people say that Spring is equivalent to JavaEE, though not appropriate, but there is some truth in the following figure is the main module of Spring:

(Specific and detailed description with reference to the official website, there is not too much in the introduction, just start writing the first HelloWorld)

First, the establishment of a project (based IntelliJ IDEA), corresponding to the reference jar, as shown:

 

Establish Studet.java:

package com.lql.spring01;

/**
 * @author: lql
 * @date: 2019.09.24
 * Description:
 */
public class Student {
private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public void Hello() { System.out.println("Hello :" + this.getName()); } }

  Compared with the traditional approach: first Student class instantiation, then the object by assigning the call to the Hello ();

-----------------------------------------------------------------------------------------------------------------------------------------------

Spring的HelloWorld如下:

 首先在工程里创建一个spring的配置文件“applicationContext.xml”(名称可以随意命名,官网建议此命名applicationContext),在applicationContext.xml里面配置bean,整体效果如下:

相对应的注释和注意事项已经写在截图里,接下来就是将bean交给Spring了;代码如下(不用记)

    public static void main(String[] args) {
//        Student student = new Student();
//        student.setName("lql");
//        student.Hello();

        //创建IOC容器
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取bean
        Student student = app.getBean("student", Student.class);
        //调用方法
        student.Hello();
    }

 

 

 打印如下,注释在截图中:

 至此,一个简单的HelloWorld的程序写完了,接下来我们讲讲IOC&DI,Spring中的第一个核心。

(小白程序猿,有错欢迎纠错)

 

虽然有些事情,从一开始就意味着结束,从一开始就知道没有结果,那是不是就不让它开始呢?如果害怕失去就放弃拥有的权利,那么人生好像也就失去了意义

 

Guess you like

Origin www.cnblogs.com/-qilin/p/11578316.html