菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
110
0

Ⅰ.Spring的点点滴滴--序章

原创
05/13 14:22
阅读数 99630

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.configresource选择为注释掉的那一条的时候, 资源配置文件为当前目录下的objects.xml文件, 里面的xmlns属性是对节点的规范在vs中会自动提示节点

  1. file://表示文件目录并且是相对于程序的目录
  2. config://表示的是配置节点上的某个节点,
  3. typeAliases表示type的别名,所以配置文件和xml文件上面的type参数不一样, 但是2个文件的效果是一样的
  4. singleton参数表示这个实例是否为单例,默认为true
  5. id为程序中所调用这个实例的name, 也可以设置为name="PersonDao" type="PersonDaoAlias"效果是一样的
  6. 当配置节点中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的实例化日志)

  1. classpath表示是项目的目录下的文件,当然也存在file://等一系列的地址规范
  2. singleton表示当前对象是否为单例,默认为false和C#一样
  3. id也可以用name属性和效果是一样的

从上面可以看出javac#几乎是一样的,几乎类名的都不多, 而且我们实例化一个对象可以直接通过xml来配置,而不需要改代码


发表评论

0/200
110 点赞
0 评论
收藏