• 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();
    }

    }

    结果:

  • 相关阅读:
    2018-2019-2 网络对抗技术 20165212 Exp4 恶意代码分析
    2018-2019-2 20165212 《网络对抗技术》Exp3 免杀原理与实践
    2018-2019-2 20165212《网络对抗技术》Exp2 后门原理与实践
    2018-2019-2 20165212《网络对抗技术》Exp1 PC平台逆向破解
    小议Android多进程以致Application多次初始化
    Android Studio Gradle编译时『No resource found that matches the given name』解决方法(windows系统的坑)
    用gradle编译任意结构的Android项目
    Android 上能提高学习工作效率的应用
    求医记(一)
    Android应用开发中的夜间模式实现(一)
  • 原文地址:https://www.cnblogs.com/yang-hao/p/5792405.html
Copyright © 2020-2023  润新知