IDEA+Maven+Spring5.X project creation

Spring core concepts

  • IoC ( core in the core ): Inverse of Control, inversion of control. The right of object creation is handed over to the Spring framework by the programmer
  • AOP: Aspect Oriented Programming, aspect-oriented programming. Enhance the function of Bean in the IoC container without modifying the source code of the target object
  • DI: Dependency Injection, dependency injection. When the Spring framework is responsible for creating Bean objects, it dynamically injects dependent objects into Bean components! !
  • Spring container: IoC container

1. Add dependencies

 <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
    </dependencies>

2. Add the configuration file applicationContext.xml

Create applicationContext.xml under src/main/resources

<?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="video" class="net.cybclass.sp.domain.Video">
        <property name="id" value="9"></property>
        <property name="title" value="Spring5.X课程"></property>
    </bean>
</beans>

bean tag

  • id attribute: specify the name of the bean, used when the bean is dependent on other classes
  • name attribute: used to specify the alias of the bean, if there is no id, you can also use name
  • Class attribute: used to specify the source of the Bean, to create the Bean class, a fully qualified name is required

3. Create Video.java 

package net.cybclass.sp.domain;

/**
 * @author: wangxiaobo
 * @create: 2020-10-03 15:08
 **/
public class Video {
    private int id;
    private String title;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

4. Create app.java

package net.cybclass.sp;

import net.cybclass.sp.domain.Video;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * @author: wangxiaobo
 * @create: 2020-10-03 15:11
 **/
public class app {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        Video video=(Video) applicationContext.getBean("video");
        System.out.println(video.getTitle());
    }
}

5. Run

Guess you like

Origin blog.csdn.net/qq_34709784/article/details/108910464