spring编写hello world

 
Spring目前较为流行的框架之一.核心技术.DI,AOP
虽然不是一个完整的java规范,但在j2ee的开发领域却占着重要的比例.
目前较为流行的SSH体系结构.Struts用于表示层,Spring用于控制层,而hibernate用于数据库的持久层.而Spring在其中却成了其中最较重要的部分.
Spring编写hello world
编写环境eclipse3.2.1,myeclipse 5.1
第一步:创建一个web项目spring1.
第二步:创建一个包,把涉及到的几个类和配置文件放到包中.这里我的包名为test.lyx
第三步:加入spring capabilities..这里我们加入核心包就可以了.还没有用到其它的技术.这时需要产生一个配置文件spring必不可少的.这里我以applicationContext.xml命名.
第四步:创建一个类User.代码如下:
package test.lyx;
public class User {
    private String userName ;
    public String getUserName() {
        return userName ;
    }
    public void setUserName(String userName) {
        this . userName = userName;
    }
}
第五步:创建一个类TestUser.代码如下:(用于测试用)
package test.lyx;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class TestUser {
    public static void main(String[] args) {
         ApplicationContext context=new FileSystemXmlApplicationContext("/src/test/lyx/applicationContext.xml");
        User user=(User)context.getBean("user");
        System.out.print(user.getUserName());
    }
}
第六步:修改applicationContext.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 = "user" class = "test.lyx.User" abstract = "false"
       singleton = "true" lazy-init = "default" autowire = "default"
       dependency-check = "default" >
       < property name = "userName" >
           < value > hello liuyuanxi </ value >
       </ property >
    </ bean >
</ beans >
这一步就是注入的过程.所谓注入就是由容器控制程序之间的关系,在运行的时候给予所有指定的值.
进行 TestUser 你就会看 .hello liuyuanxi.
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值