环境:IntelliJ 14 ; jdk1.8

 

Spring操作步骤

1.新建项目---Spring Batch

2.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;
} @Override
public void useAxe() {
System.out.println(axe.chop()); }
}
public class StoneAxe implements Axe {
@Override
public 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;
} @Override
public 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配置文件创建实例

Spring_HelloWord的更多相关文章

  1. 05_ssm基础(三)之Spring基础

    11.spring入门引导 12.spring_HelloWord程序 实现步骤: 0.找到spring压缩包,并解压 1.拷贝jar包 2.添加主配置文件(官方文档约28页) 3.在测试中使用 13 ...

  2. java之spring之helloword

    这篇文章主要讲 spring的基础的使用案例 项目整体目录结构: 1.新建一个java项目:spring_helloworld 2.在项目下创建一个lib文件夹,并把一些必须的jar包复制过去 新建l ...

随机推荐

  1. 非侵入式Ajax

    基本准备 首先,新建一个ASP.NET MVC 3的空项目. 然后新增一个Model,代码如下: using System; using System.Collections.Generic; usi ...

  2. 配置LANMP环境(3)-- 安装anmp前准备与实用软件安装

    一.安装配置vim 1.安装 yum -y install vim* 2.创建文件夹 mkdir -p /etc/vim/ 3.配置 vim ~/.vimrc  "插入模式时 光标的上下左右 ...

  3. jq serialize 系列化 乱码 解决办法

    query = form.find('input,select,textarea').serialize(); $.post(target,decodeURIComponent(query)).suc ...

  4. 使用google地图API

    1.获取key 2.获取相应地方的坐标:https://support.google.com/maps/answer/18539?co=GENIE.Platform%3DDesktop&hl= ...

  5. Spring MVC复选框

    以下示例显示如何在使用Spring Web MVC框架的表单中使用复选框(Checkbox).首先使用Eclipse IDE来创建一个WEB工程,并按照以下步骤使用Spring Web Framewo ...

  6. IDEA15入门常用设置

    打开IDEA设置的快捷键:Ctrl + Alt + S 打开选中的项目属性快捷键:Shift + Ctrl + Alt + S 1.IDEA默认不会使用我们独立安装的Maven配置,需要手动设置,并且 ...

  7. Ant Design使用问题记录

    公司的测试管理平台前端使用的是Ant Design of React框架,后台使用的是python,数据库用的是mysql.没有参与前期的开发,听说是工作了10年积累下来的一个暂且可用的管理平台,开发 ...

  8. Python Socket套接字

    socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. socket起源于Un ...

  9. Laravel5.1 模型 --一对一关系

    这篇文章主要记录模型的一对一关系,关联关系是Model的一种非常方便的功能. 1 实现一对一关系 1.1 准备工作 首先我们需要创建两张表和对应的两个模型,第一个模型是用户表,第二个模型是账号表. 这 ...

  10. 【Python】Python获取命令行參数

    有时候须要用同一个Python程序在不同的时间来处理不同的文件,此时假设老是要到Python程序中去改动输入.输出文件名称.就太麻烦了. 而通过Python获取命令行參数就方便多了.以下是我写得一个小 ...