Spring IOC 依赖注入的三种方式

依赖注入3种方式:

1.set注入:通过setXxx()赋值(一定要有对应的set方法)

         赋值,默认使用的是 set方法();
         依赖注入底层是通过反射实现的。
        <property...>


2.构造器注入:通过构造方法赋值
          <constructor-arg value="ls" type="String" index="0" name="name"></constructor-arg>
           需要注意:如果  <constructor-arg>的顺序 与构造方法参数的顺序不一致,则需要通过type或者index或name指定。

3.p命名空间注入
          引入p命名空间
            xmlns:p="http://www.springframework.org/schema/p"

        <bean id="course" class="org.lanqiao.entity.Course" p:courseHour="300" p:courseName="hadoop" p:teacher-ref="teacher">
 

详解:(三种方式都使用xml来配置,因此只需要更改xml文件即可。)

1.set注入。

        这是通过类中的set方法赋值的,因此当你想用此方法给某个成员变量赋值时,这个成员变量的set方法必须存在。最好直接用ide的生成器生成。

         可以选择xml配置。

         applicationContext.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">
<!-- 要写完整类名。在eclipse中,可以选择student后,点击右键,选择copy qualified name获得。 -->
<bean id="student" class="springProject.Student">
<!-- 通过property标签给各个成员变量赋值。
	name:成员变量名。
	value:数值类型的值。
	ref:引用类型的值。 -->
		<property name="stuNo" value="2"></property>
		<property name="stuName" value="ls"></property>
		<property name="stuAge" value="24"></property>
</bean>
</beans>

 

此时student已经放入ioc容器中去了。下面展示如何直接从容器中取student对象。

test.java:

package springProject;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
	public static void main(String[] args) {
		ApplicationContext conext = new ClassPathXmlApplicationContext("applicationContext.xml");
		//从ioc容器中取得了student对象。
		Student student=(Student)conext.getBean("student");
		System.out.println(student);
	}
}

执行结果:

2.构造器注入。

顾名思义,就是通过类的构造器方法注入。使用的标签为<constructor-arg>。

 applicationContext.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="student" class="springProject.Student">
<!-- index:构造方法中成员变量的顺序,从0开始。
	value、ref的含义同property。 -->
	<constructor-arg value="3" index="0" ></constructor-arg>
	<constructor-arg value="2" index="2" ></constructor-arg>
	<constructor-arg value="zs" index="1"></constructor-arg>
</bean>

</beans>

如下图,还有name、type等可以用来限定。

name:通过成员变量名来限定。

type:通过成员变量的类型来限定。

 

3.p命名空间注入

需要引入p命名空间
    xmlns:p="http://www.springframework.org/schema/p"。

然后在bean标签内使用p:成员变量名这种方式来赋值。

 applicationContext.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"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 
简单类型:
	p:属性名="属性值"
引用类型(除了String外):
	p:属性名-ref="引用的id"
注意多个 p赋值的时候 要有空格。 -->
<bean id="student" class="springProject.Student" p:stuAge="2" p:stuName="zz" p:stuNo="123">
</bean>

</beans>

 

补充:

自动装配:(至少可以在set注入的方式中使用)


<bean ... class="org.lanqiao.entity.Course"  autowire="temp" > 

           temp可在byName|byType|constructor|no中选取一个。  byName 本质是byId。


byName:  自动寻找:其他bean的id值=该Course类的属性名


byType:  其他bean的类型(class)  是否与 该Course类的ref属性类型一致  (注意,此种方式 必须满足:

                当前Ioc容器中 只能有一个Bean满足条件  )


constructor: 其他bean的类型(class)  是否与 该Course类的构造方法参数 的类型一致;此种方式的本质就是byType


可以在头文件中 一次性将该ioc容器的所有bean  统一设置成自动装配:


<beans xmlns="http://www.springframework.org/schema/beans"
...
default-autowire="byName">

自动装配虽然可以减少代码量,但是会降低程序的可读性,使用时需要谨慎。

赋特殊值:

给对象类型赋值null :
        <property name="name" >  
                <null/>       -->注意 没有<value>
        </property>
赋空值 ""  
        <property name="name" >  
                <value></value>  
        </property>
 

使用注解定义bean:通过注解的形式 将bean以及相应的属性值 放入ioc容器

<context:component-scan base-package="org.lanqiao.dao">
</context:component-scan>Spring在启动的时候,会根据base-package在 该包中扫描所有类,查找这些类是否有注解@Component("studentDao"),如果有,则将该类 加入spring Ioc容器。

@Component细化:(都可以用Component,为了清晰可以用以下的细化。)

dao层注解:@Repository
service层注解:@Service
控制器层注解:@Controller

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值