Spring的注入-从配置文件到注解

本文详细介绍了在Spring框架中使用对象依赖注入的过程,包括手动注入、构造器注入、自动装配以及自动检测等方法,并解释了每种方法的原理和应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        在Spring之前 在一个对象中调用另外一个类,需要在其内部实例化这个类,这样的结果是一旦类的数目增加代码的耦合度将变的非常高。为了降低耦合度,我们引入了Spring,Spring可以通过注入来降低代码的耦合度。即把生成对象的控制权交给容器,代码要做的只是使用这些对象进行逻辑或者持久化操作,这就是控制反转(IOC).


1.配置文件阶段。

1.1 手动注入

虽然解决了在代码中显示生成对象的操作,但是我们仍然要为每个bean配置它所依赖的其他bean。
1.1.1 设值注入
<bean id="injectionService" class="com.xyf.InjectionServiceImpl">
        <property name="injectionDAO" ref="injectionDao"></property>
</bean>
<bean id="injectionDao" class="com.xyf.InjectionDaoImpl"></bean>

  在Java文件中,需要添加一个setter函数

//设值注入
	public void setInjectionDAO(InjectionDAO injectionDAO) {
		this.injectionDAO = injectionDAO;
	}

1.1.2   构造器注入

    <bean id="injectionService" class="com.xyf.InjectionServiceImpl">
        <constructor-arg ref="injectionDao"></constructor-arg>
    </bean>
    <bean id="injectionDao" class="com.xyf.InjectionDaoImpl"></bean>

 同样的在相应的Java文件中要加上一个构造器


//构造器注入
	public InjectionServiceImpl(InjectionDAO injectionDAO1) {
		this.injectionDAO = injectionDAO1;
	}


1.2自动装配

1.2.1 在配置文件中自动装配,参见如下的配置文件

<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="constructor">
</beans>



    其中default-autowire有如下四种类型的自动装配。

  • byName

  • byType

  • constructor

  • autodetect


1.2.2 在Spring3.0中可以使用注解来代替编写XML的方式来进行自动装配。(@AutoWired)

为了能够检测到bean需要在配置文件加入如下内容。


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
 	<context:annotation-config/>
</beans>

在Java代码中庸@Autowired注解,应用中必须只能有一个Bean适合装配到@Autowired 注解所标注的属性或参数中,就像上面的脑图中的方式@Autowired+@Quality或者@inject+@Name


1.3自动检测

Java文件中要怎么编写才能让容器发现,有三种方式可以选择


  1. 默认情况下类被自动发现并注册bean的条件是使用@Componment,@Repository,@Service,@Controller或者使用自定义的注解

  2. 在配置文件中修改上面的行为。即过滤器包括include-filter,exclude-filter

  3. use-default-filters = "false" 禁用自动发现与注册

Java文件编写实例:


@Component
public class BeanInterfaceImpl implements Beaninterface {
}

@Component
public class BeanInvoker {
    @Autowired
    public List<Beaninterface> list;
    @Autowired
    public Map<String,Beaninterface> map;
//数据操作
...


默认情况下bean的名字是类名的第一个字母小写的名字,当然也可以自己定义bean的id。



后记。可能本文只记录了个这个过程中的大概,很多细节没有补上,比如bean的scope,以及init-mehod和destory-method,后面会继续添砖加瓦的。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值