美文网首页
maven 项目简单搭建spring运行环境

maven 项目简单搭建spring运行环境

作者: white周 | 来源:发表于2020-09-26 23:20 被阅读0次

    1.idea 新建maven项目。

    image

    2.pom.xml中引入spring的核心jar包。

           <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.2.8.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>5.2.8.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>5.2.8.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>5.2.8.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13</version>
                <scope>test</scope>
            </dependency>
    

    3.配置bean.xml在resources下(刚开始引入的路径错误,在src下maven项目并不能找到)


    image.png
    <?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-2.5.xsd">
        <bean id="user" class="zdc.User"></bean>
    </beans>
    

    4.在test目录下编写测试类进行测试。

        @Test
        public void demo(){
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
            User user = applicationContext.getBean("user",User.class);
            user.setName("zdc");
            String s = user.toString();
            System.out.println(s);
        }
    
    image

    相关文章

      网友评论

          本文标题:maven 项目简单搭建spring运行环境

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