美文网首页
spring helloworld

spring helloworld

作者: simplerandom | 来源:发表于2020-06-09 19:09 被阅读0次

    实体类

    package org.spring;
    
    public class Hello {
        @Override
        public String toString() {
            return "hello world";
        }
    }
    

    添加配置文件my.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"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    
        <!--调用默认构造-->
        <bean id="hello" class="org.spring.Hello"></bean>
        <!--普通工厂方法构造-->
    先生成HelloFactory对象后调用getHello方法
    返回值赋给hello
        <bean id="hellofactory" class="org.spring.HelloFactory" ></bean>
        <bean id="hello" factory-bean="hellofactory" factory-method="getHello"></bean>
        <!--静态工厂方法构造-->
        <bean id="hello" class="org.spring.HelloFactory" factory-method="getHello"></bean>
    
    
    </beans>
    

    测试

    public class Application {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("my.xml");
            Hello hello = context.getBean("hello", Hello.class);
            System.out.println(hello.toString());
        }
    }
    

    相关文章

      网友评论

          本文标题:spring helloworld

          本文链接:https://www.haomeiwen.com/subject/kgehlhtx.html