环境: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. 【Ubuntu安装,ATX基于uiautomator2】之安装步骤

    Ubuntu系统下安装uiautomator2步骤: 1.安装命令: pip install --upgrade --pre uiautomator2 但是报错: Command "pyth ...

  2. springmvc 环境配置图

  3. [搬家]新域名 akagi201.org

    现在感觉自己做了好多年的垃圾信息制造者 以后只在网络上发布有用的东西, 垃圾或者对别人没用的东西就放到自己的硬盘上把 http://akagi201.org

  4. [转]Maven2中snapshot快照库的使用

    Post by 铁木箱子 in Java, 技术杂谈 on 2011-10-28 12:12. [转载声明] 转载时必须标注:本文来源于铁木箱子的博客http://www.mzone.cc[原文地址] ...

  5. TP数据删除

    [数据删除及执行原生sql语句] delete()  返回受影响的记录条数 $goods -> delete(30);   删除主键值等于30的记录信息 $goods -> delete( ...

  6. splay tree旋转操作 hdu 1890

    很神奇的旋转操作. 目前没看到其他数据结构能实现这个功能.平衡树不好处理区间操作,线段树很难旋转.splay tree搞这个就很简单了. 下面用的这个模板跑了700ms,好慢,估计是删除操作太费时了, ...

  7. Hints of sd0061(快排思想)

    Hints of sd0061 Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  8. Instapaper 使用经验和技巧

    Instapaper 分类本质是文件夹整理,没有标签. 文件夹意味着一篇文章只能放在一个文件夹里,不像标签可以实现一篇文章多个标签的功能. 一.文件夹和Like功能 1.已有文件夹: Home:存放所 ...

  9. mysql_数据库_操作

    1.查看数据库 show databases; # 默认数据库: test - 用于用户测试数据 information_schema - MySQL本身架构相关数据 2.创建数据库 #utf- 编码 ...

  10. The C Programming Language Second Edition

    %12d  at least #include <stdio.h> main() { ,sum=,w=; ; ; w<=end; w++ ) { sum+=w; // for(wb= ...