Spring_第一个Spring入门案例IOC

一、使用myeclipse创建spring框架

今天我们来写我们的第一个spring 

第一步 建立一个java project

第二步 添加我们的五个jar文件

第三步 在项目中建立一个com.zk.spring包

第四步 建立我们的userservice接口和userserviceImpl.java文件

 在我们的UserService.java和UserServiceImpl.java添加

UserService.java

1
2
3
4
5
package com.zk.Spring;
 
public interface UserService {
    public void addUser();
}

 UserServiceImpl.java

1
2
3
4
5
6
7
8
9
10
11
12
package com.zk.Spring;
 
public class UserServiceImpl implements UserService{
 
     
    @Override
    public void addUser() {
        // TODO Auto-generated method stub
        System.out.println("UserService");
    }
 
}

第五步 在我们的项目中写一个ApplicationContext.xml文件,放在src目录下,并在ApplicationContext.xml文件中添加如下代码:

ApplicationContext.xml

1
2
3
4
5
6
7
8
9
<?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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="userservice" class="com.zk.Spring.UserServiceImpl"></bean>
</beans>

最后写一个test测试文件

TestDemo.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.zk.Spring;
 
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 
public class TestDemo {
    @Test
    public void test()
    {
        String xmlpath="ApplicationContext.xml";
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlpath);
        UserService userservice=(UserService) applicationContext.getBean("userservice");
        userservice.addUser();
    }
} 

运行程序

成功 

二、使用ideal创建spring Helloworld

新建一个Spring项目

 

生成项目之后新建java文件Helloworld.java和MainApp.java,并在resources目录下创建Bean.xml

 Helloworld.java

1
2
3
4
5
6
7
8
9
10
11
public class HelloWorld {
    private String message;
 
    public void setMessage(String message) {
        this.message = message;
    }
 
    public void getMessage() {
        System.out.println("message : " + message);
    }
}

MainApp.java

1
2
3
4
5
6
7
8
9
10
11
12
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class MainApp {
    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml");
 
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        obj.getMessage();
    }
 
}

Bean.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.xsd">
 
    <bean id="helloWorld" class="HelloWorld">
        <property name="message" value="Hello World!" />
    </bean>
 
</beans>

 运行MainApp之后执行结果如下:

 刚执行MainApp的时候有可能找不到Bean.xml的路径,需要建resources目录下Bean.xml。 

posted @   leagueandlegends  阅读(2422)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示