环境: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. PHP 7的一些引人注目的新特性简单介绍

    1. ?? 运算符(NULL 合并运算符)把这个放在第一个说是因为我觉得它很有用.用法: ? 1 $a = $_GET['a'] ?? 1; 它相当于: ? 1 2 <?php $a = iss ...

  2. 关于LNMP服务器 Thinkphp5验证码不显示问题

    关于LNMP服务器 Thinkphp5验证码不显示问题   浏览:246 发布日期:2017/09/20 分类:ThinkPHP5专区 关键字: thinkphp验证码不显示 nginx下验证码不显示 ...

  3. DWR相关知识

    解决问题:服务器给前台推送消息 用途:聊天,微信签到墙,设备报警

  4. 【转】支付宝WAP支付接口开发

    支付宝WAP支付接口开发 因项目需要,要增加支付宝手机网站支付功能,找了支付宝的样例代码和接口说明,折腾两天搞定,谨以此文作为这两天摸索的总结.由于公司有自己的支付接口,并不直接使用这个接口,所以晚些 ...

  5. java 获取网页指定内容-2(实践+修改)

    import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; ...

  6. 微信 oauth4

    4.最后 4. 使用access_token获取用户信息 请求方法: https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN& ...

  7. CStdioFile UNICODE编译 读取中文汉字乱码 .

    函数原形为:char *setlocale( int category, const char *locale );头文件:<locale.h>所支持的操作系统为:ANSI, Win 95 ...

  8. 测试kernel.pid_max值

    # sysctl kernel.pid_max kernel.pid_max = # sysctl - kernel.pid_max = #include <unistd.h> #incl ...

  9. Problem H. Hotel in Ves Lagos

    Problem H. Hotel in Ves Lagos Input le: hotel.in Output le: hotel.out Time limit: 1 second Memory li ...

  10. QL 获取当前日期,年、月、日、周、时、分、秒

    ?select GETDATE() as '当前日期', DateName(year,GetDate()) as '年', DateName(month,GetDate()) as '月', Date ...