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();
        }
    }

    项目结构

    测试

  • 相关阅读:
    3. docker-compose实战--ghost app
    3. nacos服务发现
    1. nacos初识, 和eureka的比较, 在docker上安装nacos
    2.nacos服务配置中心
    如何将PHP对象数组转换成普通数组
    php 通过变量 来调用函数
    linux svn
    Sublime text 3安装svn插件
    php 字符串 定界符 json_last_error()
    centos7 rm -rf 删除文件的找回
  • 原文地址:https://www.cnblogs.com/chenyanbin/p/12929425.html
Copyright © 2011-2022 走看看