代码改变世界

spring-01

2018-11-04 16:02  crow!  阅读(126)  评论(0编辑  收藏  举报

IMessage.java

1 package cn.mldn.service;
2 
3 public interface IMessage {
4     public String getMsg() ;
5 }
IMessage.java

MessageImpl.java

 1 package cn.mldn.service.Impl;
 2 
 3 import cn.mldn.service.IMessage;
 4 
 5 public class MessageImpl implements IMessage {
 6 
 7     @Override
 8     public String getMsg() {
 9         return "Hello World";
10     }
11 }
 1 package cn.mldn.test;
 2 
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4 
 5 import cn.mldn.service.IMessage;
 6 
 7 public class TestMessage {
 8 
 9     public static void main(String[] args) {
10     //通过 ClassPath 路径加载 配置文件 applicationContext.xml 
11     ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");    
12 
13     IMessage msg =  ctx.getBean("msg", IMessage.class) ;
14     
15     System.out.println(msg.getMsg());
16     
17     }
18 
19 }
TestMessage

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
    
    <bean id="imsg" class="cn.mldn.service.IMessage" abstract="true" />
    <bean id="msg" class="cn.mldn.service.Impl.MessageImpl" parent="imsg" />
    
</beans>