栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring5 03 HelloSpring

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring5 03 HelloSpring

1.1 HelloSpring

现在的步骤:

  1. 使用 meven 导入相关的包

        
        
            org.springframework
            spring-webmvc
            5.3.21
        
        
        
            org.springframework
            spring-jdbc
            5.3.21
        
    

  1. 写一个 Hello 实体类
package top.muquanyu.pojo;

public class Hello {

    private String str;

    public String getStr() {
        return str;
    }

    @Override
    public String toString() {
        return "Hello{" +
                "str='" + str + ''' +
                '}';
    }

    public void setStr(String str) {
        this.str = str;
    }

}
  1. 写一个 beans.xml 配置文件


    
        
    

  1. 实操部分
  • 根据 xml 文件,获取 上下文对象
// 获取 spring 的上下文 对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
  • 从容器中 拿到 我们刚才 设定的 Bean,任意 对象 都可以 在 xml 中 设定为 Bean。
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.muquanyu.pojo.Hello;

public class Test {
    public static void main(String[] args) {
        // 获取 spring 的上下文 对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        // 从容器中 去拿出 设定的对象,这些对象 都是 Bean
        Hello hello = (Hello) context.getBean("hello");

        System.out.println(hello.toString());
    }
}

这其实 就实现了 控制反转,我们通过 xml 文件配置 相关信息的方式,也就是 第三方。创建了一个 Hello 对象,并且 用 xml 文件配置 设定了 该对象的 str 属性值 为 “Spring”。

ClassPathXmlApplicationContext 其实实现了很多接口,哈哈。十分的不容易。


3.2 使用 Spring 的 IOC思想

ref:赋值 xml 文件 当中 配置好的 一些 bean 对象。
value:具体的一个常数值。



    
        
    
    
    
    
        
    

package top.muquanyu.service;

import top.muquanyu.dao.UserDao;
import top.muquanyu.dao.UserDaoImpl;
import top.muquanyu.dao.UserMySQLImpl;

public class UserServiceImpl implements UserService{

    private UserDao userDao = new UserDaoImpl();
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void printUser() {
        System.out.println(userDao.getUser());
    }

}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.muquanyu.dao.UserDaoImpl;
import top.muquanyu.dao.UserMySQLImpl;
import top.muquanyu.service.UserService;
import top.muquanyu.service.UserServiceImpl;

public class MyTest {
    public static void main(String[] args) {
//        UserServiceImpl userService = new UserServiceImpl(new UserDaoImpl());
//        userService.printUser();
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        UserServiceImpl userServiceImpl = (UserServiceImpl)context.getBean("UserServiceImpl");

        userServiceImpl.printUser();

    }
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1003834.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号