Spring入门

IMessage.java

1
2
3
4
package com.umgsai.spring;
public interface IMessage {//定义接口
    public abstract String getMessage();
}

Message.java

1
2
3
4
5
6
package com.umgsai.spring;
public class Message implements IMessage {
    public String getMessage() {
        return "Hello world!";
    }
}

Hello.java

1
2
3
4
5
6
package com.umgsai.spring;
public class Hello implements IMessage{
    public String getMessage() {
        return "你好!";
    }
}

类路径下applicationContext.xml

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
                                 
    <bean id="a1" class="com.umgsai.spring.Message"/>
    <bean id="a2" class="com.umgsai.spring.Hello"/>
</beans>

MainClass.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
MainClass.java
package com.umgsai.spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainClass {
    public static void main(String[] args) {
        //IMessage message = MessageFactory.create("1");
        //创建spring Bean工厂
        BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
        //BeanFactory factory = new ClassPathXmlApplicationContext("app*.xml");通配符
        IMessage message = (IMessage)factory.getBean("a1");
        System.out.println(message.getMessage());
        message = (IMessage)factory.getBean("a2");   
        System.out.println(message.getMessage());
    }
}


运行,控制台输出结果

Hello world!

你好!




applicationContext.xml模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
       
    xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
                      
    <bean id="a1" class="com.umgsai.spring.Message"/>
</beans>



本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1253534

posted @   商商-77  阅读(115)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥
点击右上角即可分享
微信分享提示