您当前的位置: 首页 >  spring

java持续实践

暂无认证

  • 0浏览

    0关注

    746博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

自己的模块中引用Spring源码

java持续实践 发布时间:2021-02-11 16:41:18 ,浏览量:0

文章目录
      • 一. 新建自己的模块
      • 二. 学好Spring源码的建议

一. 新建自己的模块

接着写一个接口

package com.demo.service;

/**
 * 类名称:WelcomeService
 * 类描述:TODO
 *
 * @author: https://javaweixin6.blog.csdn.net/
 * 创建时间:2021/2/11 15:58
 * Version 1.0
 */
public interface WelcomeService {

	public String sayHello(String name);

}

编写接口的实现类


public class WelcomeServiceImpl implements WelcomeService {

	@Override
	public String sayHello(String name) {
		System.out.println("欢迎你 "+ name);
		return "success";
	}
}

编写配置文件, 把bean注入到容器中去 .




    
	 

在build.gradle 中, 引入依赖Sping的依赖. 注意此时引用的就是本地的依赖compile(project(":spring-context")) 写一个运行函数. 该main方法, 就是从xml中获取bean的定义, 再通过ApplicationContext ,获取Bean. 获取到bean后, 调用sayHello方法进行打印.

import com.demo.service.WelcomeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Entrance {
	public static void main(String[] args) {
		System.out.println(" Hello World ");
		String xmlPath = "D:\\mycode\\spring_study\\spring-framework-5.2.0.RELEASE\\springdemo\\src\\main\\resources\\spring\\spring-config.xml";
		ApplicationContext applicationContext = new FileSystemXmlApplicationContext(xmlPath);
		WelcomeService welcomeService = (WelcomeService) applicationContext.getBean("welcomeService");
		welcomeService.sayHello("spring hello world ");
	}
}

运行程序后, 打印结果如下. 在这里插入图片描述

二. 学好Spring源码的建议
  1. 首先要熟练使用Spring框架. 多看官方文档.
  2. 多调试代码
  3. 多掌握设计模式, 和注解
关注
打赏
1658054974
查看更多评论
立即登录/注册

微信扫码登录

0.0490s