zoukankan      html  css  js  c++  java
  • Spring 环境搭建

    1、导包

    2、编写Helloworld程序

    1 package cn.test.helloWorld;
    2 
    3 public class HelloWorld {
    4     public void sayHello(){
    5         System.err.println("1234567890");
    6     }
    7 }

    3、在src目录下写配置文件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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <!-- 
      beans
           把一个类放入到spring容器中,该类就称为bean
    -->
    <!-- 
      一个bean就代表一个类
       id就是唯一标识符[推荐命令规则是类的第一个字母小写]
    -->
    <bean name="helloWorld" class="cn.test.helloWorld.HelloWorld"></bean>
    
    </beans>

     这里也可以使用别名来配置,在bean下面添加:<alias name="helloWorld" alias="bm"/>,在后面的调用中就可以使用别名来调用了。

     4、编写启动客户端

     1 @Test
     2     public void doSomething(){
     3         /*
     4          * 1、启动spring容器
     5          * 2、从spring容器中把helloWorld拿出来
     6          * 3、对象.方法
     7          */
     8         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("/applicationContext.xml");
     9         HelloWorld helloWorld=(HelloWorld) applicationContext.getBean("helloWorld");
    10         helloWorld.sayHello();
    11     }

     使用别名调用:效果相同

    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("/applicationContext.xml");
    HelloWorld helloWorld=(HelloWorld) applicationContext.getBean("bm");
    helloWorld.sayHello();

  • 相关阅读:
    vector容器
    CSS3文字与字体 text-overflow 与 word-wrap
    div 居中
    C# 邮件发送
    SD详解-销售过程
    js 常用
    finereport报表--动态格间运算 二
    finereport报表--动态格间运算 一
    CSS 渐变色
    CSS3 box-shadow 属性 紧跟在 -webkit-, -ms- 或 -moz-
  • 原文地址:https://www.cnblogs.com/liuwt365/p/4224159.html
Copyright © 2011-2022 走看看