spring实例参考02-一个有基本框架雏形的实例

目录

1 前言

2 bean类

3 dao层

4 dao层实现

4.1 实现1

4.2 实现2

5 service层

6 service层实现

7 测试


系列文章

1. Spring实例参考01-一个简单实例

2. spring实例参考02-一个有基本框架雏形的实例

3. Spring实例参考03-通过构造方法创建对象

4. Spring实例参考04-通过工厂创建对象

5. Spring实例参考05-导入其他文件:import

6. Spring实例参考06-setter注入的10种方式

7. Spring实例参考07-bean的作用域

8. Spring实例参考08-bean的自动装配

9. Spring实例参考09-静态代理

10. Spring实例参考10-动态代理

11. Spring实例参考11-API实现AOP前置/后置通知

12. Spring实例参考12-自定义类实现AOP​​​​​​​

13. Spring实例参考13-注解实现AOP


1 前言

 该文供以下文章的参考内容:

Spring笔记https://blog.csdn.net/qq_34378496/article/details/128095043

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="mysqlDao" class="com.evenif.dao.impl.IndexMysqlDaoImpl"/>
    <bean id="oracleDao" class="com.evenif.dao.impl.IndexOralceDaoImpl"/>
    <!-- 该配置是indexService中通过setIndexDao(..)方法自动注入,indexDao自动映射
         indexService.setIndexDao(..)方法,如果名称不一致一定报错!这是其中的一种
         注入方式:通过setter方法注入-->
    <bean id="indexService" class="com.evenif.service.impl.IndexServiceImpl">
        <property name="indexDao" ref="mysqlDao"></property>
    </bean>
</beans>

2 bean类

package com.evenif.bean;

public class Index {

    private int id;
    private String name;

    public Index() {
        System.out.println("create Index class");
    }

    public Index(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

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

3 dao层

package com.evenif.dao;
import com.evenif.bean.Index;
public interface IndexDao {
    public Index getIndex();
}

4 dao层实现

4.1 实现1

package com.evenif.dao.impl;

import com.evenif.bean.Index;
import com.evenif.dao.IndexDao;

public class IndexMysqlDaoImpl implements IndexDao {
    @Override
    public Index getIndex() {
        // 假装是从数据库获取的Index对象
        return new Index(2,"mysql02");
    }
}

4.2 实现2

package com.evenif.dao.impl;

import com.evenif.bean.Index;
import com.evenif.dao.IndexDao;

public class IndexOralceDaoImpl implements IndexDao {
    @Override
    public Index getIndex() {
        // 假装是从数据库获取的Index对象
        return new Index(3,"oralce03");
    }
}

5 service层

package com.evenif.service;

import com.evenif.bean.Index;

public interface IndexService {
    public Index getIndex();
}

6 service层实现

package com.evenif.service.impl;

import com.evenif.bean.Index;
import com.evenif.dao.IndexDao;
import com.evenif.service.IndexService;

public class IndexServiceImpl implements IndexService {
    private IndexDao indexDao;

    public void setIndexDao(IndexDao indexDao) {
        this.indexDao = indexDao;
    }

    @Override
    public Index getIndex() {
        return indexDao.getIndex();
    }
}

7 测试

package com.evenif;

import com.evenif.service.IndexService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
        //注意这个xml文件位置要放对
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        IndexService indexService = (IndexService) context.getBean("indexService");
        System.out.println(indexService.getIndex());
    }
}
//执行结果:
//Index{id=2, name='mysql02'}


版权声明:本文为qq_34378496原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。