业务逻辑层和表现层解耦:工厂创建对象和spring.net(类似于微软的unity)创建对象
两者的比较:后者功能强大,能实现
di(依赖注入,就是通过容器来创建对象的时候,在对象的初始化时可以给一些属性、构造方法的参数注入默认值)
aop(面向切面编程,有权限校验、日志处理等功能,类似于微软的filter)
下面是spring.net使用教程
下载:点击打开链接
spring.net中ioc和di的使用
1.引用Spring.Core.dll和Common.Logging.dll2.配置
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="config://spring/objects"/>
</context>
<objects xmlns="http://www.springframework.net">
<description>An example that demonstrates simple IoC features.</description>
<object name="UserInfoService" type="CZBK.ItcastOA.SpringNet.UserInfoService,CZBK.ItcastOA.SpringNet"> <!--类名、命名空间+类名、程序集名称-->
<property name="UserName" value="zhangsan"/><!--di、属性名、值-->
<property name="Person" ref="Person"/>
</object>
<object name="Person" type="CZBK.ItcastOA.SpringNet.Person, CZBK.ItcastOA.SpringNet">
<property name="Age" value="16"/>
</object>
</objects>
</spring>
</configuration>
3.配置文件分离
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="config://spring/objects"/>
<resource uri="file://services.xml"/><!--获取单独的xml文件-->
</context>
<objects xmlns="http://www.springframework.net">
<!--此处内容单独放一个xml文件-->
</objects>
</spring>
</configuration>
新建service.xml,将其属性-复制到输出目录改为始终复制或者如果较新则复制
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<description>An example that demonstrates simple IoC features.</description>
<object name="UserInfoService" type="CZBK.ItcastOA.SpringNet.UserInfoService,CZBK.ItcastOA.SpringNet">
<property name="UserName" value="zhangsan"/>
<property name="Person" ref="Person"/>
</object>
<object name="Person" type="CZBK.ItcastOA.SpringNet.Person, CZBK.ItcastOA.SpringNet">
<property name="Age" value="16"/>
</object>
</objects>
4.创建对象
IApplicationContext ctx = ContextRegistry.GetContext();
IUserInfoService lister = (IUserInfoService)ctx.GetObject("UserInfoService");
spring.net在asp.net mvc中的使用
1.导入dll文件Spring.Core.dll
Common.Logging.dll
Spring.Web.dll
Spring.Web.Extensions.dll
Spring.Web.Mvc4.dll
2.配置
webconfig
controller.xml,将其属性-复制到输出目录改为始终复制或者如果较新则复制
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object type="MyOA.UI.Controllers.UserInfoController,MyOA.UI" singleton="false"><!--控制器名、程序集名-->
<property name="UserInfoService" ref="UserInfoService"/><!--类名、相关联的object节名-->
</object>
<object type="MyOA.UI.Controllers.UserLoginController,MyOA.UI" singleton="false">
<property name="UserInfoService" ref="UserInfoService"/>
</object>
</objects>
service.xml,将其属性-复制到输出目录改为始终复制或者如果较新则复制
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object name="UserInfoService" type="MyOA.Service.UserInfoService,MyOA.Service" singleton="false"/><!--service层类名、程序集名-->
<object name="RoleInfoService" type="MyOA.Service.RoleInfoService,MyOA.Service" singleton="false"/>
<object name="ActionInfoService" type="MyOA.Service.ActionInfoService,MyOA.Service" singleton="false"/>
<object name="UserActionService" type="MyOA.Service.UserActionService,MyOA.Service" singleton="false"/>
</objects>
3.controller中创建对象
4.修改Global文件
继承SpringMvcApplication