新建一個控制檯 html
using Spring.Context; using Spring.Context.Support; using System; namespace SpringBase{ class Program { static void Main(string[] args){ IoCMethod(); Console.ReadLine(); } private static void IoCMethod() { IApplicationContext ctx = ContextRegistry.GetContext("test"); IPersonDao dao = ctx.GetObject("PersonDao") as IPersonDao; if (dao != null) { dao.sayhello(); } } } public class PersonDao : IPersonDao { public void sayhello() { Console.WriteLine("hello"); } } public interface IPersonDao{ void sayhello(); } }添加2個配置文件 java
app.config(若是是web項目確定是web.config) web
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="spring"> <section name="typeAliases" type="Spring.Context.Support.TypeAliasesSectionHandler, Spring.Core"/> <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" /> <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /> </sectionGroup> </configSections> <spring> <typeAliases> <alias name="PersonDaoAlias" type="SpringBase.PersonDao,SpringBase" /> </typeAliases> <context name="test"> <resource uri="config://spring/objects" /> <!--<resource uri="file://objects.xml" />--> </context> <objects xmlns="http://www.springframework.net"> <description>一個簡單的控制反轉例子</description> <object id="PersonDao" type="PersonDaoAlias" singleton="false" /> </objects> </spring> </configuration>objects.xml spring
<objects xmlns="http://www.springframework.net"> <description>一個簡單的控制反轉例子</description> <object id="PersonDao" type="SpringBase.PersonDao, SpringBase" /> </objects>當app.config的resource選擇爲註釋掉的那一條的時候, 資源配置文件爲當前目錄下的objects.xml文件, 裏面的xmlns屬性是對節點的規範在vs中會自動提示節點 express
- file://表示文件目錄而且是相對於程序的目錄
- config://表示的是配置節點上的某個節點,
- typeAliases表示type的別名,因此配置文件和xml文件上面的type參數不同, 可是2個文件的效果是同樣的
- singleton參數表示這個實例是否爲單例,默認爲true
- id爲程序中所調用這個實例的name, 也能夠設置爲name="PersonDao" type="PersonDaoAlias"效果是同樣的
- 當配置節點中context只有一個的時候能夠不添加name屬性, 程序中直接經過ContextRegistry.GetContext()得到程序的上下文
新建一個空的maven項目,在pom.xml文件配置 apache
<?xml version="1.0" encoding="UTF-8"?> <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>springbase</groupId> <artifactId>springdemo</artifactId> <version>1</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.4.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project>在META-INF下的MANIFEST.MF文件配置當main函數和引用的jar包 c#
Manifest-Version: 1.0 Class-Path: lib/aopalliance-1.0.jar lib/commons-logging-1.1.1.jar lib/ spring-aop-3.2.4.RELEASE.jar lib/spring-beans-3.2.4.RELEASE.jar lib/s pring-context-3.2.4.RELEASE.jar lib/spring-core-3.2.4.RELEASE.jar lib /spring-expression-3.2.4.RELEASE.jar Main-Class: springdemo.SpringBase新建一個類一個接口一個main入口 app
IPersonDao.java 框架
package springdemo; public interface IPersonDao { void sayhello(); }PersonDao.java maven
package springdemo; public class PersonDao implements IPersonDao { public void sayhello() { System.out.println("Hello World!"); } }SpringBase.java
package springdemo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class SpringBase { public static void main(String[] args) { ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:bean.xml"); IPersonDao dao = (IPersonDao) ctx.getBean("PersonDao"); if (dao != null) { dao.sayhello(); } } }以及一個和csharp差很少的配置文件 bean.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="PersonDao" class="springdemo.PersonDao" singleton="false"/> </beans>而後mark當前模塊會輸出一個jar包, 在cmd上面輸入java -jar springdemo.jar就能夠看到helloword 的效果(前面附帶一些spring的實例化日誌)
- classpath表示是項目的目錄下的文件,固然也存在file://等一系列的地址規範
- singleton表示當前對象是否爲單例,默認爲false和C#同樣
- id也能夠用name屬性和效果是同樣的
從上面能夠看出java和c#幾乎是同樣的,幾乎類名的都很少, 並且咱們實例化一個對象能夠直接經過xml來配置,而不須要改代碼