最后一部分,spring

spring的配置过程还算顺利

不过遇到了个奇怪的问题,我会在最下面贴出来

文件路径

文件路径

配置步骤

1.建立一个配置文件Spring-Config.xml

<?xml version="1.0" encoding="UTF8" ?>
<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">

    <!--调用其他配置文件-->
    <!--<import resource="{path}/beans.xml"/>-->

    <!--设置别名:在获取Bean的时候可以使用别名获取-->
    <!--<alias name="user" alias="userNew"/>-->

    <!--bean就是java对象,由Spring创建和管理-->

    <!--
       id 是bean的标识符,要唯一,如果没有配置id,name就是默认标识符
       如果配置id,又配置了name,那么name是别名
       name可以设置多个别名,可以用逗号,分号,空格隔开
       如果不配置id和name,可以根据applicationContext.getBean(.class)获取对象;

    class是bean的全限定名=包名+类名
    -->
    <bean id="MysqlImpl" class="top.moesy.dao.impl.UserDaoMySqlImpl"/>
    <bean id="ServiceImpl" class="top.moesy.service.impl.UserServiceImpl">
        <property name="userDao" ref="MysqlImpl"/>
    </bean>
    <bean id="hello" class="top.moesy.model.Hello">
        <constructor-arg index="0" value="Spring"/>
    </bean>


</beans>

2.建立Dao中的接口与实现类

package top.moesy.dao;

public interface UserDao {
    public void getUser();
}
package top.moesy.dao.impl;

import top.moesy.dao.UserDao;

public class UserDaoMySqlImpl implements UserDao {
    @Override
    public void getUser(){
        System.out.println("MySql获取用户数据");
    }
}

3.建立一个实体类

package top.moesy.model;

public class Hello {
    private String name;

    public Hello(String name) {
        this.name=name;
    }

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

    public void show(){
        System.out.println("Hello,"+ name );
    }
}

4.建立Service中的接口与实现类

package top.moesy.service;

public interface UserService {
    public void getUser();
}
package top.moesy.service.impl;

import top.moesy.dao.UserDao;
import top.moesy.service.UserService;

public class UserServiceImpl implements UserService {
    private UserDao userDao;
    // 利用set实现
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

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

5.建立一个测试类

package top.moesy.test;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.moesy.Utils.MybatisUtils;
import top.moesy.dao.mapper.UserMapper;
import top.moesy.model.Hello;
import top.moesy.model.User;
import top.moesy.service.impl.UserServiceImpl;

import java.util.List;

public class test {

    @Test
    public void springTest1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Config.xml");
        UserServiceImpl serviceImpl = (UserServiceImpl) context.getBean("ServiceImpl");
        serviceImpl.getUser();
    }

    @Test
    public void springTest2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Config.xml");
        Hello hello = (Hello) context.getBean("hello");
        hello.show();
    }
}

可能遇到的问题

第二天把其他两个配好后运行test,出现了一个报错:

1 字节的 UTF-8 序列的字节 1 无效。

把spring配置文件中的UTF-8改成UTF8即可

Last modification:November 25th, 2020 at 01:58 pm