初学Java Spring
最近刚开始接触java spring框架相关知识,将最近整理的学习资料记录下:
Java Spring 初探
- 本部分内容主要对Spring中的核心的IoC Container,反向控制进行解释,如下图为经典的spring 框架

-
IoC 全称为inversion of control, 是Java Spring中非常核心的部分,主要用来配置和管理java 对象的生命周期。
-
接下来通过一个例子来详细描述IoC是如何进行控制的。
- 首先创建一个工程 Java Maven Project,勾选create a simple project

2.填写artifact。

3.在pom.xml中引入java spring 的jar包依赖
<pre><code>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.testSpring</groupId>
<artifactId>TestSpring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- Spring Core -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<!-- Spring Context -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
</dependencies>
</project>
</code></pre>
<blockquote><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.testSpring</groupId>
<artifactId>TestSpring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- Spring Core -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<!-- Spring Context -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
</dependencies>
</project></blockquote>
4.开始Spring 的编程
- 一般Spring中都会有面向接口编程的良好体现,所以我们首先创建一个HelloWorld的接口:

<blockquote>
package com.testSpring;
public interface HelloWorld {
public void sayHello();
}
</blockquote>
- 然后在同一个包下建立一个helloWorld的服务

<blockquote>
package com.testSpring;
public class HelloWorldService {
private HelloWorld helloWorld3;
public HelloWorldService() {
}
public void setHelloWorld(HelloWorld helloWorld2) {
this.helloWorld3 = helloWorld2;
}
public HelloWorld getHelloWorld() {
return this.helloWorld3;
}
}
</blockquote>
- 创建业务代码,去实现接口
<blockquote>
package com.testSpring.imp;
import com.testSpring.HelloWorld;
public class SpringHelloWorld implements HelloWorld {
public void sayHello() {
System.out.println("Spring Say Hello!!");
}
}
</blockquote>
- 创建核心的IoC 控制层,beans.xml

<blockquote>
<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="springHelloWorld"
class="com.testSpring.imp.SpringHelloWorld"></bean>
<bean id="strutsHelloWorld"
class="com.testSpring.imp.StrutsHelloWorld"></bean>
<bean id="helloWorldService"
class="com.testSpring.HelloWorldService">
<property name="helloWorld" ref="SpringHelloWorld" />
</bean>
</beans>
</blockquote>
- 最后创建main函数入口用于启动beans.xml

<blockquote>
package com.testSpring.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.testSpring.HelloWorld;
import com.testSpring.HelloWorldService;
public class startHelloWorld {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans.xml");
HelloWorldService service =
(HelloWorldService) context.getBean("helloWorldService");
HelloWorld hw= service.getHelloWorld();
hw.sayHello();
}
}
</blockquote>
具体运行过程过程主要如下:

核心的IoC创建 java bean业务,由于在bean.xml中注册了springHelloWorld业务(一般是通过settle方法导入到HelloWorldService中),也就是实例化的springHelloWorld被命名为helloWorld并传入到了settle器中,然后在主函数中执行

从而创建了service的内容,并打印了其方法。
从上述过程中可以了解到,基本spring是做到了业务层和控制层的分离。