zoukankan      html  css  js  c++  java
  • Spring_HelloWorld

    目录:

    各个类文件:

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.hy</groupId>
    <artifactId>spring-1</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-1 Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <build>
    <finalName>spring-1</finalName>
    </build>
    <properties>
    <!-- Spring -->
    <spring-framework.version>4.1.6.RELEASE</spring-framework.version>
    </properties>
    <dependencies>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
    </dependency>
    <!-- spring需要的jar包 -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>${spring-framework.version}</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring-framework.version}</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring-framework.version}</version>
    </dependency>

    <dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>${spring-framework.version}</version>
    </dependency>
    </dependencies>
    </project>

    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.xsd">

    <bean id="helloWord" class="com.hy.spring.beans.HelloWord">
    <property name="name" value="Tom"></property>
    </bean>
    </beans>

    HelloWord.java

    package com.hy.spring.beans;

    public class HelloWord {
    private String name;

    public void setName(String name) {
    System.out.println("setName:" + name);
    this.name = name;
    }

    public void hello(){
    System.out.println("Hello:" + name);
    }

    public HelloWord() {
    System.out.println("HelloWorld's Constructor");
    }
    }

    Main.java

    package com.hy.spring.beans;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class Main {

    public static void main(String[] args) {
    //1.创建 Spring 的 IOC容器的对象
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    //2.从IOC容器中获取Bean实例
    HelloWord helloWord = (HelloWord) ctx.getBean("helloWord");
    //3.调用hello方法
    helloWord.hello();
    }

    }

    结果:

  • 相关阅读:
    #一周五# (视频) 手掌四轴Estes 4606,树莓派2和WRTNode,WinHEC 2015深圳
    Android 自定义标题栏
    (视频)《快速创建网站》 4.1 为啥造软件不同于造汽车,为啥是软件就一定会有Bug - 构建开发运维一体化(DevOps)
    (视频) 《快速创建网站》3.4 网站改版3分钟搞定 - WordPress主题安装和备份
    OpenCV由汉字生成图片(透明)----可以对抗论文查重!!!
    Codeforces Round #295 (Div. 2)
    Codeforces Round #294 (Div. 2)
    Codeforces Round #293 (Div. 2)
    Codeforces Round #292 (Div. 2)
    暴力/set Codeforces Round #291 (Div. 2) C. Watto and Mechanism
  • 原文地址:https://www.cnblogs.com/yang-hao/p/5792405.html
Copyright © 2011-2022 走看看