环境:IntelliJ 14 ; jdk1.8

Spring操作步骤

1.新建项目---Spring Batch2.IntelliJ会自动加载jar包3.现在就可以在src目录下写Java类文件了4.将相应的类部署在XML配置文件spring-config.xml中 (Eclipse需要手动创建,貌似名为bean.xml)5.写主程序main (右键-->Run)

HelloWord小程序

此处直接从第3步开始

3.首先定义两个接口,Person和Axe

public interface Person {public void useAxe();
}
public interface Axe {public String chop();
}

然后定义接口的实现类,Chinese和StoneAxe

public class Chinese implements Person {private Axe axe;//设值注入public void setAxe(Axe axe) {this.axe = axe;}@Overridepublic void useAxe() {System.out.println(axe.chop());}
}
public class StoneAxe implements Axe {@Overridepublic String chop() {return "石斧砍柴好慢";}
}

4.修改spring-config.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 id="chinese" class="org.spring.service.impl.Chinese"><property name="axe" ref="stoneAxe"/></bean><bean id="stoneAxe" class="org.spring.service.impl.StoneAxe"/>
</beans>

5.写主程序main

public class SpringTest {public static void main(String[] args){ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");Person p = (Person) ctx.getBean("chinese", Person.class);p.useAxe();}
}

程序到此结束,运行结果为:

石斧砍柴好慢

Spring的核心机制:依赖注入(Dependency Inhection)(控制反转IoC—Inversion of Control)

依赖注入通常包括以下两种:

设值注入:使用属性的setter方法来注入被依赖的实例

构造注入:使用构造器来注入被依赖的实例

请看下面的例子

public class Chinese implements Person {private Axe axe;//设值注入public void setAxe(Axe axe) {this.axe = axe;}//构造注入public Chinese(Axe axe) {this.axe = axe;}@Overridepublic void useAxe() {System.out.println(axe.chop());}
}

不同的注入方式在配置文件中的配置也不一样

<!--设值注入-->
<bean id="chinese" class="org.spring.service.impl.Chinese"><property name="axe" ref="steelAxe"/>
</bean>
<!--构造注入-->
<bean id="chinese" class="org.spring.service.impl.Chinese"><constructor-arg ref="stoneAxe"/>
</bean>

建议采用以设值注入为主,构造注入为辅的注入策略。对于依赖关系无需变化的注入,尽量采用构造注入;而其他的依赖关系的注入,则考虑使用设值注入。

Spring的两个核心接口Spring容器最基本的接口是BeanFactory。它有一个子接口:ApplicationContext。

ApplicationContext有两个常用的实现类:

FileSystemXmlApplicationContext:以基于文件系统的XML配置文件创建实例
ClassPathXmlApplicationContext:以类加载路径下的XML配置文件创建实例

转载于:https://www.cnblogs.com/weilunhui/p/4143668.html

Spring_HelloWord相关推荐

最新文章

  1. Unity3d 开发-基础篇
  2. vba 根据分辨率 缩放显示比例_【显示百闻录】第一讲:关于屏幕尺寸、比例以及分辨率...
  3. C语言如何使用函数交换两个变量的值
  4. 一、 promise
  5. Spring中Bean的生命周期是怎样的?
  6. linux 命令行 解密,解密Linux终端命令 学好Linux
  7. 模拟航班查询及预定系统 编写示例
  8. 用姓名字段统计人数_基于 Wide amp; Deep 网络和 TextCNN 的敏感字段识别
  9. 学术 | 基于深度学习的图像边缘和轮廓提取方法介绍
  10. python工资一般多少p-预测python数据分析师的工资
  11. Yii需要php版本,yii框架2.0.9版本发布了
  12. 线性可分支持向量机与硬间隔最大化
  13. Linux上安装GCC编译器过程实录
  14. 我的第一个MASM32程序(MASM32环境配置)
  15. light动名词_英语语法(5)动名词
  16. 苹果亮度自动调节怎么关闭_Redmi K30 Pro自动亮度调节和iPhone基本一致,安卓手机的大进步...
  17. 零基础学前端系列教程 | 和前端谈恋爱的第002天——约会邀请
  18. emeditor的快捷键
  19. 区块链钱包开发(Android篇)
  20. 微信商城开发:公众号平台接口配置及调试

热门文章

  1. PHP(三)——PHP基础知识
  2. 嵌入式linux通过DHCP自动获取IP地址实现
  3. 微服务微应用的安全测试_提高微服务安全性的11个方法
  4. 从零开始学电脑_带你从零开始学装机 打造自己的专属电脑之固态和机械硬盘搭配篇...
  5. mysql 5.0 修改字符集_修改及查看mysql数据库的字符集
  6. python tcp通信如何实现多人聊天,Python实现多用户全双工聊天(一对一),python多用户,多用户全双工聊天简陋...
  7. Unity项目代码书写规范
  8. vscode编辑器,自己喜欢的颜色
  9. Python 常用系统模块整理
  10. Web程序中使用EasyUI时乱码问题