三.进行Spring的简单使用

1.导入maven的pom.xml

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.0.RELEASE</version>
</dependency>
后期还得用到  和数据库的整合
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.0.RELEASE</version>
</dependency>

2.spring-config.xml

将上一随笔中的类都加入到spring的配置文件中,交给它管理

 

 

 

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

    <!--    使用spring创建对象 spring中都叫bean 绑定类-->
    <bean class="com.why.dao.UserDaoByMysql" id="mysql"/>
    <bean class="com.why.dao.UserDaoByOrcle" id="orcle"/>
    <bean class="com.why.service.ServiceIml" id="serviceIml">
<!--        给serviceiml中的属性dao赋值(必须有set方法)-->
        <property name="dao" ref="mysql"/>
    </bean>
</beans>

使用:

import com.why.service.ServiceIml;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @program: Spring
 * @description:
 * @author: @why
 * @create: 2020-08-30 15:57
 **/
public class Test {
    public static void main(String[] args) {
//使用这个类加载配置文件,加载对应的类 ClassPathXmlApplicationContext context
= new ClassPathXmlApplicationContext("spring-config.xml"); ServiceIml serviceIml = context.getBean("serviceIml", ServiceIml.class); serviceIml.UserUse(); } }

 

posted @ 2020-08-30 16:06  why666  阅读(177)  评论(0编辑  收藏  举报