Spring入门系列-创建一个Spring的helloworld项目

时间:2021-06-28 19:43:13   收藏:0   阅读:0

创建一个Spring的helloworld项目

  1. 创建maven项目,导入Spring的环境依赖

可以参考:https://www.cnblogs.com/nwu-edu/p/9542074.html,为了简化直接导入webmvc的依赖

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
  1. 创建实体类
package com.dreamcold.demo.pojo;

public class Hello {

    private String str;

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    @Override
    public String toString() {
        return "Hello{" +
                "str=‘" + str + ‘\‘‘ +
                ‘}‘;
    }

}

  1. resource目录下创建beans.xml文件
类型 变量名=new 类型();
id 等价于变量名
class 全路径类名
property 给对象的属性设置一个值
<?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-3.0.xsd">



    <bean id="hello" class="com.dreamcold.demo.pojo.Hello">
        <property name="str" value="spring"></property>
    </bean>

</beans>
  1. 写一个测试
package com.dreamcold.demo;

import com.dreamcold.demo.pojo.Hello;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloTest {

    @Test
    public void test(){

        //获取Spring的上下文对象
        ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
        //我们的对象都在Spring中进行管理了,我们一旦使用直接从中取出来就可以了

        Hello hello= (Hello) context.getBean("hello");
        System.out.println(hello.toString());
    }
}

  1. 结果

技术图片

  1. 目录结构

技术图片

过程的总结

修改案例

在上一个案例中,我们可以让Spring来管理对象,我们首先要创建beans.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-3.0.xsd">


    <bean id="MysqlImpl" class="com.dreamcold.dao.UserDaoMysqlImpl">
    </bean>

    <bean id="OracleImpl" class="com.dreamcold.dao.UserDaoOracleImpl">
    </bean>

    <bean id="ServiceImpl" class="com.dreamcold.service.UserServiceImpl">
        <property name="userDao" ref="MysqlImpl"></property>
        <!--  普通对象用value,引用对象用ref   引用Spring中创建好的对象   -->
        <!--   Value是一个具体的值,比如基本数据类型     -->
    </bean>


</beans>

测试一下


import com.dreamcold.service.UserService;

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



public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
        UserService userService=(UserService)context.getBean("ServiceImpl");
        userService.getUser();
    }
}

我们可以思考这样的好处,假如我们底层要修改实现的话,比如修改为Oracle实现,那么仅仅需要修改配置文件就可以了

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


    <bean id="MysqlImpl" class="com.dreamcold.dao.UserDaoMysqlImpl">
    </bean>

    <bean id="OracleImpl" class="com.dreamcold.dao.UserDaoOracleImpl">
    </bean>

    <bean id="ServiceImpl" class="com.dreamcold.service.UserServiceImpl">
        <property name="userDao" ref="OracleImpl"></property>
        <!--  普通对象用value,引用对象用ref   引用Spring中创建好的对象   -->
        <!--   Value是一个具体的值,比如基本数据类型     -->
    </bean>


</beans>

这样我们就避免了修改代码

技术图片

学习自链接:狂神说

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!