Spring简单入门配置maven+idea

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());
    }
}

全部评论

相关推荐

听说改名字就能收到offer哈:Radis写错了兄弟
点赞 评论 收藏
分享
拒绝无效加班的小师弟很中意你:求职意向没有,年龄、课程冗余信息可以删掉,需要提升项目经历。排版需要修改。
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务