前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring之hello world(Spring入门)

Spring之hello world(Spring入门)

作者头像
别先生
发布2018-01-02 11:53:26
6100
发布2018-01-02 11:53:26
举报
文章被收录于专栏:别先生

spring各个版本中: 在3.0以下的版本,源码有spring中相关的所有包【spring功能 + 依赖包】 如2.5版本; 在3.0以上的版本,源码中只有spring的核心功能包【没有依赖包】 (如果要用依赖包,需要单独下载!)

1:第一步,引包咯,毕竟使用的是框架吗!首先引入5个必须的包,如下所示:

commons-logging-1.1.3.jar                 日志    spring-beans-3.2.5.RELEASE.jar           bean节点    spring-context-3.2.5.RELEASE.jar        spring上下文节点    spring-core-3.2.5.RELEASE.jar            spring核心功能    spring-expression-3.2.5.RELEASE.jar   spring表达式相关表

2:核心配置文件:

  (1):Spring配置文件:applicationContext.xml/bean.xml

     (两个名称都可以,一般使用applicationContext.xml)

  (2):核心配置文件的约束如何配置,下面介绍一种方法:

applicationContext.xml配置模板如下所示:

代码语言:javascript
复制
<?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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
     
     
</beans>     

applicationContext.xml开发配置如下所示:

代码语言:javascript
复制
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xsi:schemaLocation="
 7         http://www.springframework.org/schema/beans
 8         http://www.springframework.org/schema/beans/spring-beans.xsd
 9         http://www.springframework.org/schema/context
10         http://www.springframework.org/schema/context/spring-context.xsd">
11      
12      
13      <!-- IoC容器的配置,要创建的所有的对象都配置在这里 -->
14      <bean id="user" class="com.bie.po.User"></bean>
15      
16 </beans>     

3:创建一个实体类,如User.java:

代码语言:javascript
复制
 1 package com.bie.po;
 2 /** 
 3 * @author BieHongLi 
 4 * @version 创建时间:2017年3月12日 下午5:45:06 
 5 * 
 6 */
 7 public class User {
 8 
 9     private int id;
10     private String name;
11     public int getId() {
12         return id;
13     }
14     public void setId(int id) {
15         this.id = id;
16     }
17     public String getName() {
18         return name;
19     }
20     public void setName(String name) {
21         this.name = name;
22     }
23     
24     
25 }

4:使用junit进行测试,如下所示,两种方式都可以完成,推荐第二种方式:

代码语言:javascript
复制
 1 package com.bie.hello;
 2 
 3 import org.junit.Test;
 4 import org.springframework.beans.factory.BeanFactory;
 5 import org.springframework.beans.factory.xml.XmlBeanFactory;
 6 import org.springframework.context.ApplicationContext;
 7 import org.springframework.context.support.ClassPathXmlApplicationContext;
 8 import org.springframework.core.io.ClassPathResource;
 9 import org.springframework.core.io.Resource;
10 
11 import com.bie.po.User;
12 
13 /** 
14 * @author BieHongLi 
15 * @version 创建时间:2017年3月12日 下午5:46:23 
16 * 
17 */
18 public class UserTest {
19 
20     @Test
21     public void springHello(){
22         //以前古老的方式创建对象
23         //User user=new User();
24         
25         //现在创建对象交给IoC容器了
26         //第一步:加载配置文件
27         Resource resource=new ClassPathResource("applicationContext.xml");
28         //第二步:创建容器对象(bean工厂),IoC容器=bean工厂+aplicationContext.xml
29         BeanFactory factory=new XmlBeanFactory(resource);
30         //第三步:得到容器创建的对象
31         User user=(User)factory.getBean("user");
32         
33         System.out.println(user);
34     }
35     
36     @Test
37     public void test2(){
38         //第一步:得到IoC容器对象
39         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
40         //第二步:从容器中获取bean
41         User user = (User)ac.getBean("user");
42         
43         System.out.println(user);
44     }    
45 }

Spring最牛X的框架,我来了~~~

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-03-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档