【spring】自动装配

山顶洞人方法:aotowire

UserDao.java

代码实现:

public class UserDao {
	private DBUtil dbu;

	public DBUtil getDbu() {
		return dbu;
	}

	public void setDbu(DBUtil dbu) {
		this.dbu = dbu;
	}
	
	public void test() {
		System.out.println(dbu);
	}
}

DBUtil.java

代码实现:

算了,不实现了,DBUtil.java有就行了

 

配置文件:

代码实现:

    <bean id="dbutil" class="springboottest.ioc.speltest.DBUtil"></bean>
    <bean id="userdao" class="springboottest.ioc.speltest.UserDao" autowire="byType">

注:

此时可能出现一个问题:

当要自动装配一个接口时,该接口有两个实现类,此时计算机就不知道选哪个,所以会报错

错码实现:

DBservice.java

public class DBService {
    
    private TwoSonTest ts;


	public TwoSonTest getTs() {
		return ts;
	}

	public void setTs(TwoSonTest ts) {
		this.ts = ts;
	}
    
	public void test() {
		System.out.println("=======>" + ts);
	}
}

配置文件:

    <bean id="firstsontest" class="springboottest.ioc.FirstSon" ></bean>
    <bean id="secondSon" class="springboottest.ioc.SecondSon" ></bean>
    <bean id="DBService" class="springboottest.ioc.spel.DBService" autowire="byType"></bean>

注:firstsontest.java/secondsontest.java是接口twosontest.java的实现类

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'DBService' defined in class path resource [config/applicationContext.xml]: Unsatisfied dependency expressed through bean property 'ts'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'springboottest.ioc.TwoSonTest' available: expected single matching bean but found 2: firstsontest,secondSon

 

解决方法:

使用byName的方法

代码实现:

    <bean id="ts" class="springboottest.ioc.FirstSon" ></bean>
    <bean id="secondSon" class="springboottest.ioc.SecondSon" ></bean>
    <bean id="DBService" class="springboottest.ioc.spel.DBService" autowire="byName">

注:

(1)使用byName的方法必须将DBService.java中的变量名和一个bean的名称一致

(2)使用byName的方法不会报错,但是如果将对象打印出来为null则说明没有找到名称一致的bean对象

 

扫描方法自动装配

DBUtil.java

代码实现:

package springboottest.ioc.auto;

import org.springframework.stereotype.Repository;

@Repository
public class DButil {
	private String constant;
	

}

 

UserDao.java

代码实现:

package springboottest.ioc.auto;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class UserDao {
	@Autowired
	private DButil dbu;
	
	public void test() {
		System.out.println(dbu);
	}
}

 

配置文件

代码实现:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
        <context:component-scan base-package="springboottest.ioc.auto"></context:component-scan>
 </beans>       

使用步骤:

1、在配置文件中添加如上述代码,在base-package的那一项添加文件所在的jar包名称

2、在需要扫描的文件添加注解

3、如果想要在类里添加自定义对象

在方法上或者在变量上添加@Autowired注解,如果是一个接口且该接口有多个实现类,则需要

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值