bean的自动装配

手动装配

在这里插入图片描述

dao层
  • com.jepcc.demo.dao.UserDao
package com.jepcc.demo.dao;

public interface UserDao {
    void getUser();
}
  • com.jepcc.demo.dao.impl.MysqlUserDaoImpl
package com.jepcc.demo.dao.impl;

import com.jepcc.demo.dao.UserDao;

public class MysqlUserDaoImpl implements UserDao {
    @Override
    public void getUser() {
        System.out.println("Mysql user dao");
    }
}
  • com.jepcc.demo.dao.impl.OrcaleUserDaoImpl
package com.jepcc.demo.dao.impl;

import com.jepcc.demo.dao.UserDao;

public class OrcaleUserDaoImpl implements UserDao {
    @Override
    public void getUser() {
        System.out.println("Orcale user dao");
    }
}
service层
  • com.jepcc.demo.service.UserService
package com.jepcc.demo.service;

public interface UserService {
    void getUser();
}
  • com.jepcc.demo.service.impl.UserServiceImpl
package com.jepcc.demo.service.impl;

import com.jepcc.demo.dao.UserDao;
import com.jepcc.demo.service.UserService;

public class UserServiceImpl implements UserService {
    private UserDao userDao;
    public void setUserDao(UserDao userDao){
        this.userDao = userDao;
    }
    @Override
    public void getUser() {
        userDao.getUser();
    }
}
测试类Test
  • com.jepcc.demo.test.Test
package com.jepcc.demo.test;

import com.jepcc.demo.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
        UserService service = (UserService) context.getBean("service");
        service.getUser();
    }
}
bean配置文件config.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="mysqlUserDao" class="com.jepcc.demo.dao.impl.MysqlUserDaoImpl"></bean>
    <bean id="service" class="com.jepcc.demo.service.impl.UserServiceImpl">
        <property name="userDao" ref="mysqlUserDao"></property>
    </bean>
</beans>
测试验证

在这里插入图片描述

自动装配

bean的自动装配,是为了简化spring配置。
在这里插入图片描述

局部装配

通过bean的autowire属性来指定装配方式,autowire属性有四个值,分别是

  • no
  • byName
  • byType
  • constructor
no,默认值,即不使用自动装配
byName,通过名称实现自动装配
<?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.jepcc.demo.dao.impl.MysqlUserDaoImpl"></bean>
    <bean id="service" class="com.jepcc.demo.service.impl.UserServiceImpl" autowire="byName">
    </bean>
</beans>

在这里插入图片描述

byType,通过类型实现自动装配
<?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 class="com.jepcc.demo.dao.impl.MysqlUserDaoImpl"></bean>
    <bean id="service" class="com.jepcc.demo.service.impl.UserServiceImpl" autowire="byType">
    </bean>
</beans>

在这里插入图片描述

constructor,类似于byType,即通过构造器实例化bean对象。
  • com.jepcc.demo.service.impl.UserServiceImpl
package com.jepcc.demo.service.impl;

import com.jepcc.demo.dao.UserDao;
import com.jepcc.demo.service.UserService;

public class UserServiceImpl implements UserService {
    private UserDao userDao;

    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void getUser() {
        userDao.getUser();
    }
}
  • config.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 class="com.jepcc.demo.dao.impl.MysqlUserDaoImpl"></bean>
    <bean id="service" class="com.jepcc.demo.service.impl.UserServiceImpl" autowire="constructor">
    </bean>
</beans>

在这里插入图片描述

全局装配

通过头部的default-autowire属性来指定自动装配方式。

<?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" default-autowire="byName">
    <bean id="userDao" class="com.jepcc.demo.dao.impl.MysqlUserDaoImpl"></bean>
    <bean id="service" class="com.jepcc.demo.service.impl.UserServiceImpl">
    </bean>
</beans>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值