spring快速入门

目录

1 Spring程序开发步骤

2 导入Spring开发的基本包坐标

3 编写Dao接口和实现类

 4 创建Spring核心配置文件

5 在Spring配置文件中配置UserDaoImpl

6 使用Spring的API获得Bean实例

7 项目结构

8 运行效果

9 Spring注解之@Autowired:Setter 方法上使用@Autowired注解


1 Spring程序开发步骤

①导入 Spring 开发的基本包坐标

②编写 Dao 接口和实现类

③创建 Spring 核心配置文件

④在 Spring 配置文件中配置 UserDaoImpl

⑤使用 Spring 的 API 获得 Bean 实例

注意事项:

之后无法注入

 

 

2 导入Spring开发的基本包坐标

    <properties>
        <spring.version>5.0.5.RELEASE</spring.version>
    </properties>
    <!--导入spring的context坐标,context依赖core、beans、expression-->
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>

3 编写Dao接口和实现类

public interface UserDao {
    public void save();
}
public class UserDaoImpl implements UserDao {

    public void save() {
        System.out.println("UserDao save method running....");
    }
}

 4 创建Spring核心配置文件

<?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">
</beans>

5 在Spring配置文件中配置UserDaoImpl

   <bean id="userDao" class="com.zgl.dao.impl.UserDaoImpl"></bean>

6 使用Spring的API获得Bean实例

package com.zgl.demo;

import com.zgl.dao.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserDaoDemo {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDao = (UserDao) applicationContext.getBean("userDao");
        userDao.save();
    }
}

7 项目结构

8 运行效果

9 Spring注解之@Autowired:Setter 方法上使用@Autowired注解

​ 可以在 JavaBean中的 setter 方法中使用 @Autowired 注解。当 Spring遇到一个在 setter 方法中使用的 @Autowired 注解时,它会在方法中按照类型自动装配参数值。创建测试类User,并且添加属性student,

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * @author oldlu
 */
@Component
public class User implements Serializable {
    private static final long serialVersionUID = 6089103683553156328L;
    private Long id;
    private Student student;

    public Student getStudent() {
        return student;
    }

    @Autowired
    public void setStudent(Student student) {
        this.student = student;
    }

    public void isStu() {
        student.studentStudy();
        System.out.println("------ 装配Bean成功 ---------");
    }
}

​ 下面创建依赖的类文件 Student.java,切莫忘记在类文件上添加注解 @Component:

import lombok.Getter;
import lombok.Setter;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Date;

/**
 * @author oldlu
 */
@Getter
@Setter
@Component
public class Student implements Serializable {

    private static final long serialVersionUID = -5246589941647210011L;

    //姓名
    private String name;
  
    public Student() {
        System.out.println("A default student constructor." );
    }
    public void studentStudy() {
        System.out.println("A student is studying." );
    }
}

​ 修改Spring Boot启动类,通过Spring容器拿到Bean实例user:

import com.east7.bean.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

/**
 * @author oldlu
 */
@SpringBootApplication
public class East7Application {

    public static void main(String[] args) {
       ApplicationContext act = SpringApplication.run(East7Application.class, args);
       User user = (User) act.getBean("user");
       user.isStu();
    }

}

启动应用程序,控制台将会输出以下消息:

A default student constructor.
A student is studying.
------ 装配Bean成功 ---------

说明student属性被装配成功。如果setStudent方法不加注解,程序运行时,会抛出如下异常:

Exception in thread "main" java.lang.NullPointerException
    at com.east7.bean.User.isStu(User.java:28)
    at com.east7.East7Application.main(East7Application.java:25)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵广陆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值