Spring DI 入门案例

Spring DI 入门案例

编程工具IDEA,采用maven导入依赖。

主要目录结构

spring_di
- src
- - main
- - - java
- - - - com.zhangxin9727.di
- - - - - UserService.java
- - - - - UserServiceImp.java
- - - resources
- - - - applicationContext.xml
- - test
- - - java
- - - - SpringDiTest.java
- pom.xml

主要文件内容

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <groupId>com.zhangxin9727</groupId>
    <artifactId>spring_di</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

UserService.java

package com.zhangxin9727.di;

public interface UserService {
    void sayHello();
}

UserServiceImp.java

package com.zhangxin9727.di;

public class UserServiceImpl implements UserService {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void sayHello() {
        System.out.println(name + ":Hello World!");
    }
}

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">

    <!-- 将UserService的创建权交给Spring -->
    <bean id="userService" class="com.zhangxin9727.di.UserServiceImpl">
    	<!--配置属性-->
        <property name="name" value="Alice"/>
    </bean>

</beans>

SpringIocTest.java

import com.zhangxin9727.di.UserService;
import com.zhangxin9727.di.UserServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringDiTest {
    @Test
    //传统方法
    public void test1() {
        UserServiceImpl userService = new UserServiceImpl();
        //设置属性
        userService.setName("Alice");
        userService.sayHello();
    }

    @Test
    //Spring DI 方法:工厂模式+反射机制+配置文件
    public void test2() {
        //创建Spring工厂
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //通过工厂获得UserService类
        UserService userService = (UserService) applicationContext.getBean("userService");
        userService.sayHello();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值