Spring依赖注入,自动扫描组件,自动装配,注解注入

Spring 依赖注入

Spring框架中,依赖注入(DI)的设计模式是用来定义对象彼此间的依赖。它主要有两种类型:
  • Setter方法注入
  • 构造器注入

<!-- 	创建一个 Spring bean 的配置文件,并在这里声明所有的Java对象的依赖。 -->
	<bean id="gradebeen" class="com.hst.spring.demo.Grade"  init-method="init">
		<property name="name" value="java班"></property>
	</bean>
<!-- 	set 设值注入 property ,been中必须要有set方法 -->	
<bean id="student" class="com.hst.spring.demo.Student">
		<property name="name" value="小明"></property>
		<property name="number" value="1001"></property>
		<property name="grade" ref="gradebeen"></property>
	</bean>
<!-- 	构造方法 constructor,必须要有默认的构造方法 index下标 value属性赋值 ref对象赋值 -->
	<bean id="user" class="com.hst.spring.demo.User">
		<constructor-arg index="0" value="teacher" type="String" />
		<constructor-arg index="1" ref="gradebeen"></constructor-arg>
	</bean>

自动装配

在Spring中,支持 5 自动装配模式。
  • no – 缺省情况下,自动配置是通过“ref”属性手动设定
  • byName – 根据属性名称自动装配。如果一个bean的名称和其他bean属性的名称是一样的,将会自装配它。
  • byType – 按数据类型自动装配。如果一个bean的数据类型是用其它bean属性的数据类型,兼容并自动装配它。
  • constructor – 在构造函数参数的byType方式。
  • autodetect – 如果找到默认的构造函数,使用“自动装配用构造”; 否则,使用“按类型自动装配”。
1. ref实例
<!-- 	创建一个 Spring bean 的配置文件,并在这里声明所有的Java对象的依赖。 -->
	<bean id="gradebeen" class="com.hst.spring.demo.Grade"  init-method="init">
		<property name="name" value="java班"></property>
	</bean>
<!-- 	set 设值注入 property ,been中必须要有set方法 -->	
<bean id="student" class="com.hst.spring.demo.Student">
		<property name="name" value="小明"></property>
		<property name="number" value="1001"></property>
		<property name="grade" ref="gradebeen"></property>      --------ref
	</bean>


2. byName 实例
<bean id="customer" class="com.hst.spring.Student" autowire="byName" />	
<bean id="person" class="com.hst.spring.Person" />
default-autowire="byName"




5. autodetect实例
xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" 
		default-autowire="autodetect">	

注解注入,自动扫描组件




<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"		---------新增项
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
              http://www.springframework.org/schema/context                     ---------
            http://www.springframework.org/schema/context/spring-context.xsd">  ---------
         <context:component-scan base-package="com.hst.integraimanager.server.impl" /> --------    
</beans>           
 * 自动组件扫描  
 * @Component – 指示自动扫描组件。
@Repository – 表示在持久层DAO组件。
@Service – 表示在业务层服务组件。
@Controller – 表示在表示层控制器组件。
@Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 
通过 @Autowired的使用来消除 set ,get方法。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值