Spring简单入门配置maven+idea

版权声明:Leo.All Rights Reserved. https://blog.csdn.net/qq_41113081/article/details/89197683

1.创建bean对象

package entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Serializable {
    /**
     *
     */
    private Integer id;

    /**
     *
     */
    private String name;

    /**
     *
     */
    private String sex;

    /**
     *
     */
    private String specialty;

    /**
     *
     */
    private String grade;

    public Student(String name, String sex, String specialty, String grade) {
        this.name = name;
        this.sex = sex;
        this.specialty = specialty;
        this.grade = grade;
    }
}

2.创建applicationContext.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-->
    <bean id="student" class="entity.Student">
        <!--为属性赋值-->
        <property name="id" value="666"/>
        <property name="name" value="Spring"/>
        <property name="sex" value=""/>
        <property name="grade" value="大六"/>
        <property name="specialty" value="信息安全"/>
    </bean>
</beans>

3.简单使用

import entity.Student;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @ClassName: SpringTest
 * @Author: Leo
 * @Description:
 * @Date: 2019/4/10 22:30
 */
public class SpringTest {
    private static Logger logger = LogManager.getLogger("SpringTest");

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student) applicationContext.getBean("student");
        logger.debug(student.toString());
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41113081/article/details/89197683