Spring(XML方式)简单入门

环境准备

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

项目创建

Spring(XML方式)简单入门_第1张图片

Spring(XML方式)简单入门_第2张图片

Spring(XML方式)简单入门_第3张图片

Spring(XML方式)简单入门_第4张图片

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.0modelVersion>
  <groupId>com.cybgroupId>
  <artifactId>springartifactId>
  <packaging>warpackaging>
  <version>0.0.1-SNAPSHOTversion>
  <name>spring Maven Webappname>
  <url>http://maven.apache.orgurl>
  <dependencies>
            
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>5.0.7.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>5.0.7.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>5.0.7.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-expressionartifactId>
            <version>5.0.7.RELEASEversion>
        dependency>

         
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
        dependency>
  dependencies>
  <build>
    <finalName>springfinalName>
            <plugins>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.2version>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                    <encoding>UTF-8encoding>
                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();
    }
}

项目结构

Spring(XML方式)简单入门_第5张图片

测试

你可能感兴趣的:(Spring(XML方式)简单入门)