vs2012+Spring.Core.dll
Ⅰ.Spring的点点滴滴--序章
spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架
.net篇(环境为vs2012+Spring.Core.dll)
新建一个控制台
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个配置文件
app.config(如果是web项目肯定是web.config)
<?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
<objects xmlns="http://www.springframework.net">
<description>一个简单的控制反转例子</description>
<object id="PersonDao" type="SpringBase.PersonDao, SpringBase" />
</objects>当app.config的resource选择为注释掉的那一条的时候, 资源配置文件为当前目录下的objects.xml文件, 里面的xmlns属性是对节点的规范在vs中会自动提示节点
- file://表示文件目录并且是相对于程序的目录
- config://表示的是配置节点上的某个节点,
- typeAliases表示type的别名,所以配置文件和xml文件上面的type参数不一样, 但是2个文件的效果是一样的
- singleton参数表示这个实例是否为单例,默认为false
- id为程序中所调用这个实例的name, 也可以设置为name="PersonDao" type="PersonDaoAlias"效果是一样的
- 当配置节点中context只有一个的时候可以不添加name属性, 程序中直接通过ContextRegistry.GetContext()获得程序的上下文
java篇(环境为Maven+Jdk1.7)
新建一个空的maven项目,在pom.xml文件配置
<?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包
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入口
IPersonDao.java
package springdemo;
public interface IPersonDao {
void sayhello();
}PersonDao.java
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来配置,而不需要改代码
- 下一篇:Ⅱ.spring的点点滴滴--对象
- 本文链接地址:Ⅰ.Spring的点点滴滴--序章
vs2012+Spring.Core.dll的更多相关文章
- 多个IoC容器适配器设计及性能测试(Castle.Windsor Autofac Spring.Core)
[转]多个IoC容器适配器设计及性能测试和容器选择 1. 采用的IoC容器和版本 Autofac.2.6.3.862 Castle.Windsor.3.1.0 Spring.Core.2.0.0 2. ...
- C# Winform 运行异常 CefSharp.core.dll 找不到指定的模块
C# Winform开发中使用了CefSharp,之前在VS2012中运行很正常,今天换了一台Windows XP 打开VS2010 运行时,发生异常:System.IO.FileNotFoundEx ...
- 【Spring开发】—— Spring Core
原文:[Spring开发]-- Spring Core 前言 最近由于一些工作的需要,还有自己知识的匮乏再次翻开spring.正好整理了一下相关的知识,弥补了之前对spring的一些错误认知.这一次学 ...
- spring core 与 context理解
Spring core是核心层,拥有这BeanFactory这个强大的工厂,是所有bean的管理器: 而spring context是上下文运行环境,基于spring core之上的一个架构, 它之上 ...
- 解决Could not load file or assembly CefSharp.Core.dll的问题
这个问题的中文提示是: 未能加载文件或程序集“CefSharp.Core.dll”或它的某一个依赖项.找不到指定的模块 具体原因是因为CefSharp运行时需要Visual C++ Redistrib ...
- 是否缺少对 Microsoft.CSharp.dll 和 System.Core.dll 的引用?
错误提示 : 预定义的类型“Microsoft.CSharp.RuntimeBinder.Binder”未定义或未导入 是否缺少对 Microsoft.CSharp.dll 和 System.Core ...
- 找不到编译动态表达式所需的一种或多种类型。是否缺少对 Microsoft.CSharp.dll 和 System.Core.dll 的引用?
提示“找不到编译动态表达式所需的一种或多种类型.是否缺少对 Microsoft.CSharp.dll 和 System.Core.dll 的引用? ”错误 解决方法: 将引入的COM对象(misc ...
- win 8系统:System.IO.FileNotFoundException: 未能加载文件或程序集“CefSharp.Core.dll”或它的某一个依赖项。找不到指定的模块
最近用CefSharp做了一个chrome核心的浏览器. 在win 7.win 10系统上都正常运行,但是在win 8系统上报错了. win 8系统:System.IO.FileNotFoundExc ...
- Spring core resourc层结构体系及JDK与Spring对classpath中资源的获取方式及结果对比
1. Spring core resourc层结构体系 1.1. Resource相关结构体系 1.2. ResourceLoader相关体系 2. JDK与Spring对classpath中资源的获 ...
随机推荐
- Hdu 3962 Microgene (AC自己主动机+矩阵)
标题效果: 构造一个字符串,使得有两个和两个以上的目标串.长短L这一系列有多少串都. IDEAS: 只有全款减有1一些字符串,没有目标就是答案. 假定数据是非常小的,够用dp解.dp[i][j][k] ...
- 我展示了视频采集前端vfe和camera,decode等交互驱动的体系结构
到现在都与处理器接触较多.更深入的驱动主要是前端视频采集.控制TI的DM64xx,DM3730.纪氏A31等待.他们发现,它们的使用的基本框架的是不一样的. 当然,典型camera例如ov系列,dec ...
- HDU 3415 Max Sum of Max-K-sub-sequence 最长K子段和
链接:http://acm.hdu.edu.cn/showproblem.php?pid=3415 意甲冠军:环.要找出当中9长度小于等于K的和最大的子段. 思路:不能採用最暴力的枚举.题目的数据量是 ...
- 试想一下,在代码学习Swift!
文件 https://itunes.apple.com/us/book/the-swift-programming-language/id881256329?mt=11 htt ...
- iOS # Charles拦截封包
Charles: 是在Mac下常用的截取网络封包的工具,在做iOS开发时,我们为了调试与服务器端的网络通讯协议,常常需要截取网络封包来分析.Charles通过将自己设置成系统的网络访问代理服务器,使得 ...
- 【Android UI设计和开发】动画(Animation)详细说明(一)
Android开发之动画效果浅析 请尊重他人的劳动成果.转载请注明出处:Android开发之动画效果浅析 程序执行效果图: Android动画主要包括补间动画(Tween)View Animation ...
- iOS、真机调试
Xcode中IOS.真机测试 一.购买开发者账号(需要有信用卡.每年支付$99.0) 二.直接淘宝购买一个.用于测试,但是不能上传App 1.获取手机的UUID(Identifier xxxxxx9e ...
- ASP.NET学习笔记2--自己写代码绑定Gridview
像以前一样,先写好自己的样式布局, 第二步,在数据库建立一个商品表 代码如下: CREATE TABLE [SHANGPING_INFO] ( [Shangping_Id] INT PRIMARY K ...
- ExtJs--15--Ext.is*各种类型推断的方法,简单看源代码就能够明确了
/** * Returns true if the passed value is empty, false otherwise. The value is deemed to be empty if ...
- ASP.NET MVC中加载WebForms用户控件(.ascx)
原文:ASP.NET MVC中加载WebForms用户控件(.ascx) 问题背景 博客园博客中的日历用的是ASP.NET WebForms的日历控件(System.Web.UI.WebControl ...