新建一个控制台 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来配置,而不须要改代码