文章目录
一. 新建自己的模块
- 一. 新建自己的模块
- 二. 学好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框架. 多看官方文档.
- 多调试代码
- 多掌握设计模式, 和注解