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

目录

前言

1 java bean

2 dao

3 daoImpl

 4 service

5 serviceImpl

6 配置

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


前言

本文供以下文章参考使用:

Spring基础回顾__evenif的博客-CSDN博客

1 java 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;
        System.out.println("create Index class with params");
    }
    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 + '\'' +
                '}';
    }
}

2 dao

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

3 daoImpl

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 service

package com.evenif.service;

import com.evenif.bean.Index;

public interface IndexService {
    public Index getIndex();
}

5 serviceImpl

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;
    }
    //当定义了使用构造器自动装配时,就必须添加一个显式的无参构造,因为byType和byName实际上都会调用无参构造来实例化本类
    public IndexServiceImpl() {

    }
    //用于使用构造器自动装配!
    public IndexServiceImpl(IndexDao indexDao) {
        this.indexDao = indexDao;
    }

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

6 配置

<?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" default-autowire="byName">

 <bean id="indexDao" class="com.evenif.dao.impl.IndexMysqlDaoImpl"/>
 <!-- 一般装配方式 -->
    <bean id="indexService" class="com.evenif.service.impl.IndexServiceImpl">
        <property name="indexDao" ref="indexDao"></property>
    </bean>
    <!--
      根据名称自动装配
      注意:IndexServiceImpl.setIndexDao(IndexDao indexDao) 和
      <bean id="indexDao" class="com.evenif.dao.impl.IndexMysqlDaoImpl"/>中
      “setIndexDao”必须与id="indexDao"是对应关系,与IndexMysqlDaoImpl 定义的变量名称 IndexDao xxx;没有关系!
    -->
    <bean id="indexService02" class="com.evenif.service.impl.IndexServiceImpl" />
    <!--
        根据类型自动装配,与bean的id没有关系,但是同一种类型的bean只能有一个,否则报错!
        例如再有一个:
        <bean id="indexDao02" class="com.evenif.dao.impl.IndexMysqlDaoImpl"/>
        使用byType一定报错!
    -->

     <bean id="indexService03" class="com.evenif.service.impl.IndexServiceImpl" autowire="byType"/>
    <!--
            根据构造方法自动装配,
            public IndexServiceImpl(IndexDao indexDao) {
                 this.indexDao = indexDao;
            }
    -->
    <bean id="indexService04" class="com.evenif.service.impl.IndexServiceImpl" autowire="constructor"/>
</beans>
<!-- 也可以在头文件中使用default-autowire定义,则bean的 autowire="xxx" 可以省略 ,如下所示:-->
        <!--
           <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" default-autowire="byName">
             -->
        <!--
      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" default-autowire="byType">
       -->
        <!--
     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" default-autowire="constructor">
     -->

7 测试主类

package com.evenif;

import com.evenif.bean.Person;
import com.evenif.bean.User;
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) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        for(int i=2;i<5;i++){
            IndexService service = (IndexService) context.getBean("indexService0"+i);
            System.out.println(service.getIndex());
        }
    }
}
//执行结果:
//      create Index class with params
//        Index{id=2, name='mysql02'}
//      create Index class with params
//        Index{id=2, name='mysql02'}
//      create Index class with params
//        Index{id=2, name='mysql02'}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_evenif

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值