您的位置:首页 > 编程语言 > Java开发

Spring IoC/DI容器 的helloworld工程

2017-02-14 11:52 369 查看

第一步:新建一个java project

第二步:添加需要的jar包:

2.1在工程下新建文件夹lib,引入需要的jar包:

(1)Spring提供给我们用的jar包(2)Spring需要使用的jar包

2.2 把引入的jar包添加到eclipse中:右键工程-properties-Java Build Path -Libaries面板 Add JARs -选择对应工程下的lib文件夹



第三步:编程

3.1写应用程序,写法和java一样

//定义接口
public interface Api {

public String t(int a);
}

//定义接口的实现类
public class ImplOne implements Api {

@Override
public String t(int a) {
// TODO Auto-generated method stub
System.out.println("now in Impl One t,a==" + a);
return "hello==" + a;
}

}


3.2 为Spring写配置文件,name和class

在src文件夹下新建文件applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
default-lazy-init="true">

<!--相当于Api api = new ImplOne();-->
<bean name="test" class="cn.javass.spring3.hello.ImplOne"></bean>

</beans>


3.3写客户端程序

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {
public static void main(String[] args) {

// 第一步:读配置文件,来创建ApplicationContext,相当于bean的工厂
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });

// 第二步:获取需要操作的bean
Api api = (Api) context.getBean("test");

////////// 第三步:以下和spring就没有关系了,面向接口编程////////////
String s = api.t(21);
System.out.println("s==" + s); // 打印要有前缀

}

}


Spring 对bean的使用 有两种方式

方式一:基于xml的配置方式

方式二:注解

一些想法:

1.什么情况下叫用了Spirng框架,什么情况下是没用Spring框架:

答:引用了Spring提供的jar包和Sprign依赖的要使用的jar包

2.怎么用Spring框架?

答:通过配置文件,上面这个例子是通过写Spring的配置文件 来使用Spring IoC容器

3.使用Spring框架的好处?

答:松散类之间的耦合,在上面的例子中,客户端Client类和Api接口没有关系

4. bean对象和IoC容器?

答:凡是受 Spring IoC容器管理的对象都叫bean对象。

Ioc容器管理bean对象的实例化,装配和生命周期。

所以bean对象是Spring框架特有的。

5.SpringK框架的非侵入性怎么体现?

答:除了测试代码,其他地方没有出现Spring的组件。所谓非侵入性,就是实现了功能,但是没有使用Spring的api
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring