zoukankan      html  css  js  c++  java
  • 【Spring】入门HelloWorld

    参考:https://www.yiibai.com/spring/spring-tutorial-for-beginners.html

    一、创建项目

    1.利用IntelliJ创建Maven项目
    2.配置pom.xml,引入Spring


    4.0.0

    <groupId>com.jh</groupId>
    <artifactId>testspring</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <dependencies>
        <!-- Spring Core -->
            <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>4.1.4.RELEASE</version>
            </dependency>
    
            <!-- Spring Context -->
            <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.1.4.RELEASE</version>
            </dependency>
    
    </dependencies>
    

    二、编写HelloWorld

    1.编写接口类HelloWorld

    public interface HelloWorld {
        public void sayHello();
    }
    

    2.编写实现类HellWorldImplOne

    public class HellWorldImplOne implements HelloWorld {
        public void sayHello() {
            System.out.println("hello one");
        }
    }
    

    3. 编写依赖类HelloWordDependanceTest

    public class HelloWordDependanceTest {
        private HelloWorld helloWorld;
    
    
        public void setHelloWorld(HelloWorld helloWorld) {
            this.helloWorld = helloWorld;
        }
    
        public HelloWorld getHelloWorld() {
            return this.helloWorld;
        }
    
    }
    

    4.编写测试类HelloWorldDependanceMain

    
    public class HelloWorldDependanceMain {
        public static void main(String[] args){
            ApplicationContext context =
                    new ClassPathXmlApplicationContext("bean.xml");
    
            HelloWordDependanceTest service =
                    (HelloWordDependanceTest) context.getBean("helloWorldDependanceTest");
    
            HelloWorld hw= service.getHelloWorld();
    
            hw.sayHello();
    
        }
    }
    

    5.编写配置文件bean.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="HelloWorldImplOne" class="com.jh.spring.HellWorldImplOne"></bean>
    
        <bean id="helloWorldDependanceTest" class="com.jh.spring.HelloWordDependanceTest">
                     <property name="helloWorld" ref="HelloWorldImplOne"/>
            </bean>
    </beans>
    

    6.运行测试类HelloWorldDependanceMain

  • 相关阅读:
    系统可靠性计算
    jira与readmine区别
    linux下批量替换文件内容
    JMeter学习(十九)JMeter测试MongoDB
    mongoVUE1.5.3 破解方法
    Junit使用GroboUtils进行多线程测试
    JMeter学习(十八)JMeter测试Java(二)
    JMeter学习(十七)JMeter测试Java
    Tomcat 和 Resin 比较,哪个更适合你?
    JMeter学习(十四)JMeter监控Tomcat性能
  • 原文地址:https://www.cnblogs.com/grape1211/p/9426208.html
Copyright © 2011-2022 走看看