Spring-HelloWorld

这篇博客介绍了如何在Spring框架中管理依赖,并通过XML配置文件进行服务层的改造。首先,展示了如何引入Spring WebMvc依赖并创建实体类。接着,通过XML配置文件实例化对象并测试,说明了配置文件的便捷性。然后,逐步展示了如何实现DAO和Service层的接口,以及使用ref属性进行依赖注入,使得代码更加松耦合。最后,提供了测试代码以验证改造后的功能。
摘要由CSDN通过智能技术生成

要写Spring的代码,一定需要spring的依赖,这个依赖是被maven管理的。

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.9</version>
</dependency>

下边我们还需要一个实体类

package com.zkw.pojo;

public class Hello {
    private String name;

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Hello{" +
                "name='" + name + '\'' +
                '}';
    }
}

然后我们需要在resource里边创建spring的xml文件,我这里命名为beans.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="hello" class="com.zkw.pojo.Hello">
        <property name="name" value="Spring"/>
        </bean>

</beans>

测试代码

import com.zkw.pojo.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
       ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
       Hello hello = (Hello) context.getBean("hello");
       System.out.println(hello.toString());
    }
}

结果如下

在这里插入图片描述

这样我们在需要进行改的时候就不需要去改代码了,只需要去该配置文件就可以了。


我们对第一个例子进行改造:使用ref进行依赖注入

首先UserDao

package com.zkw.dao;

public interface UserDao {
    void getUser();
}

实现UserDao接口:UserDaoImpl

package com.zkw.dao;

public class UserDaoImpl implements UserDao {
    public void getUser() {
        System.out.println("默认获取用户数据");
    }
}

Service层的接口:Userservice

package com.zkw.service;

public interface UserService {
    void getUser();
}

实现Service层的接口:UserServiceImpl

package com.zkw.service;

import com.zkw.dao.UserDao;

public class UserServiceImpl implements UserService {
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void getUser() {
        userDao.getUser();
    }
}

beans.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="userDao" class="com.zkw.dao.UserDaoImpl"/>

        <bean id="serviceImpl" class="com.zkw.service.UserServiceImpl">
            <property name="userDao" ref="userDao"/>
        </bean>

</beans>

测试代码


import com.zkw.dao.UserDaoImpl;
import com.zkw.dao.UserDaoSQLServiceImpl;
import com.zkw.service.UserService;
import com.zkw.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserService serviceImpl = context.getBean("serviceImpl", UserService.class);
        serviceImpl.getUser();
    }
}

结果如下

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值