Spring 控制反转(Ioc )的思想:
第一种境界(穷叼丝的境界):如果想吃各种特色菜,就需要到专门的做这个特色菜的餐厅去吃(每个餐厅挨个去,许多穷叼丝就是这样追女朋友的)
第二种境界(白领的境界):如果想吃特色菜,就自己去买菜并请相应的厨师到家里来做
第三种境界(高富帅的境界):如果想吃特色菜,告诉管家,剩下就等着吃(大部分管家的模式都是一样的)
在 Spring 中,编程人员生产各种对象,每个人都可以依靠 Spring 这个管家,享受高富帅的待遇。
下面举一个具体的例子:
现在使用 Maven 构建整个项目,具体的目录结构遵循 Maven 的标准,如下:
其中 target 文件夹是 maven 执行之后生成的
下面是编程思路:HelloWorldClient 想要打印一个 HelloWorld, 他知道有许多方式可以获得 HelloWorld, 比如本地文件系统的 fileHelloWorld 、网络远程、 数据库系统,这些信息都是Spring这个管家告诉它的,现在他选择了 fileHelloWorld 这个方式,他并不知道 fileHelloWorld 具体是怎么做的,只知道用 getContent 就可以打印出 HelloWorld,下面是他的代码:
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class HelloWorldClient {
public static void main(String[] args) {
try{
//获取fileHelloWorld
Resource resource = new ClassPathResource("appcontext.xml");
BeanFactory bFactory = new XmlBeanFactory(resource);
HelloWorld hw = (HelloWorld)bFactory.getBean("fileHelloWorld");
//打印HelloWorld
System.out.println(hw.getContent());
} catch(Exception e) {
System.out.println("sorry error create:" + e.getMessage());
}
}
}
spring 管家它又是怎么知道又哪些人可以打印HelloWorld呢?这主要就是依靠豆子配置文件 appcontext.xml ,能做某件事的豆子都会在这个文件中登记自己,并告诉 Spring 管家他是干嘛的?下面是该文件的内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean name="fileHelloWorld" class="pri.clebeg.spring.HelloWorld">
<constructor-arg>
<ref bean="fileHello"/>
</constructor-arg>
</bean>
<bean name="fileHello" class="pri.clebeg.spring.FileHelloStr">
<constructor-arg>
<value>HelloWorld.properties</value>
</constructor-arg>
</bean>
</beans>
现在就把所有涉及到的豆子生产出来,下面是其他几个类的代码:
public interface HelloStr {
public String getContent();
}
import java.io.InputStream;
import java.util.Properties;
public class FileHelloStr implements HelloStr{
private String propFileName;
public FileHelloStr(String propFileName) {
this.propFileName = propFileName;
}
public String getContent() {
String helloWorld = "";
try {
Properties properties = new Properties();
InputStream is = getClass().getClassLoader().getResourceAsStream(propFileName);
properties.load(is);
is.close();
helloWorld = properties.getProperty("HelloWorld");
} catch (Exception ex) {
System.out.println(ex);
}
return helloWorld;
}
}
public class HelloWorld {
private HelloStr hStr;
public HelloWorld(HelloStr hStr) {
this.hStr = hStr;
}
public String getContent() {
return hStr.getContent();
}
}
接下来为了能让 maven 知道该如何工作,需要编写 pom.xml, 下面是它的内容:
<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>pri.clebeg.spring</groupId>
<artifactId>ch02</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ch01</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Spring version -->
<version.spring>3.1.0.RELEASE</version.spring>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${version.spring}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${version.spring}</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--添加maven插件,jar包管理插件,下面主要是把执行文件入口告诉maven,maven自动添加相应信息到manifest文件中-->
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>pri.clebeg.spring.HelloWorldClient</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
<!--添加maven插件,依赖管理的插件,下面主要是复制依赖到项目的lib目录中-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>