zoukankan      html  css  js  c++  java
  • Spring(XML方式)简单入门

    环境准备

    1. maven
    2. jdk
    3. Spring
    4. Eclipse

    项目创建

    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.cyb</groupId>
      <artifactId>spring</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>spring Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
                <!-- spring 核心组件中的4个依赖 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>5.0.7.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.0.7.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>5.0.7.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-expression</artifactId>
                <version>5.0.7.RELEASE</version>
            </dependency>
    
             <!-- 单元测试Junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
      </dependencies>
      <build>
        <finalName>spring</finalName>
                <plugins>
                <!-- 配置Maven的JDK编译级别 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
            </plugins>
      </build>
    </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="UserService" class="com.cyb.service.UserServiceImpl"></bean>
    </beans>

    UserService.java

    package com.cyb.service;
    
    public interface UserService {
        void speak();
    }

    UserServiceImpl.java

    package com.cyb.service;
    
    public class UserServiceImpl implements UserService {
    
        @Override
        public void speak() {
            System.out.println("陈彦斌博客:https://www.cnblogs.com/chenyanbin/");
        }
    }

    TestUserService.java

    package com.cyb.test;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.cyb.service.UserService;
    
    public class TestUserService {
        @Test
        public void TestIoc() {
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            // 方式一
            UserService service1 = context.getBean(UserService.class);
            service1.speak();
            // 方式二
            UserService service2 = (UserService)context.getBean("UserService");
            service2.speak();
        }
    }

    项目结构

    测试

  • 相关阅读:
    软件工程 2+ 自动生成四则运算题 测试版
    面向对象 (5)计算柱体体积可换底
    面向对象 (4)正方形的周长和面积
    软件工程 3.关于软件质量保障初探
    面向对象 (3)四棱柱的体积与数的判断
    面向对象 (1)矩形的面积和周长
    面向对象 (2)n的阶乘与两点距离
    软件工程 2.20194650 自动生成四则运算题第一版报告
    软件工程 1.《现代软件工程—构建之法》-概论(精读一章有感+练习与讨论部分)
    第四次博客作业-结对项目
  • 原文地址:https://www.cnblogs.com/chenyanbin/p/12929425.html
Copyright © 2011-2022 走看看