Spring的依赖注入

注入的方式有3种:
第一种:使用构造函数注入
第二种:使用set方法注入
第三种:使用注解注入
注入的数据类型有3类:
第一类:基本类型和string类型
第二类:其他bean类型(必须是spring的配置文件中出现过的bean)
第三类:复杂类型(复合类型)

构造函数注入

涉及的标签:

  • type:指定参数的类型
  • index:指定参数的索引位置,从0开始
  • name:指定参数的名称 一般用这个

上面三个属性是指定给哪个参数赋值的,下面两个属性是指定赋什么值


  • value:指定基本数据类型或String类型的数据
  • ref:指定其他bean类型数据

标签出现的位置:写在bean标签内部

代码示例如下

客户的业务层实现类

public class CustomerServiceImpl implements ICustomerService {
	private String driver;
	private Integer port;
	private Date today;

	public CustomerServiceImpl(String driver, Integer port, Date today) {
		super();
		this.driver = driver;
		this.port = port;
		this.today = today;
	}


	@Override
	public void saveCustomer() {
		System.out.println("业务层调用持久层"+driver+" "+port+" "+today);
	}
<bean id="customerService" class="com.service.impl.CustomerServiceImpl">
	  	<constructor-arg name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></constructor-arg>
	  	<constructor-arg name="port" value="1433"></constructor-arg>
	  	<constructor-arg name="today" ref="now"></constructor-arg>
</bean>
	  
<bean id="now" class="java.util.Date"></bean>
set方法注入

涉及的标签:property
标签的属性:

name:指定参数的set方法名称
value:指定基本数据类型或String类型的数据
ref:指定其他bean类型数据

标签出现的位置:写在bean标签内部

先在java代码里面设置好set方法,然后配置xml

1.基本数据类型

	  <bean id="customerService" class="com.service.impl.CustomerServiceImpl">
	  	<property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
	  	<property name="port" value="3307"></property>
	  	<property name="today" ref="now"></property>
	  </bean>
	  
	  <bean id="now" class="java.util.Date"></bean>

2.复杂数据类型

	  <bean id="customerService" class="com.service.impl.CustomerServiceImpl">
	  		<property name="myStrs">
	  			<array>
	  				<value>AAA</value>
	  				<value>BBB</value>
	  				<value>CCC</value>
	  			</array>
	  		</property>	
	  		
	  		<property name="myList">
	  			<list>
	  				<value>AAA</value>
	  				<value>BBB</value>
	  				<value>CCC</value>
	  			</list>
	  		</property>	
	  		
	  		<property name="mySet">
	  			<set>
	  				<value>AAA</value>
	  				<value>BBB</value>
	  				<value>CCC</value>
	  			</set>
	  		</property>	
	  		
	  		<property name="myMap">
	  			<map>
	  				<entry key="test1" value="AAA"></entry>
	  				<entry key="test2" value="BBB"></entry>
	  			</map>
	  		</property>	
	  		
	  		<property name="myProps">
				<props>
					<prop key="test1">123</prop>
					<prop key="test2">456</prop>
				</props>
	  		</property>	
	  </bean>
注解方法注入

注解分类:

  • 1.用于创建bean对象
    @Component

作用:就相当于配置了一个bean标签
它出现的位置:类上面
属性:value,指定bean的id,当不写时,有默认值当前类的短名首字母改小写

由此注解衍生的三个注解:
@Controller 一般用于表现的注解
@Service 一般用于业务层
@Repository 一般用于持久层
他们和@Component的作用及属性都是一模一样的,关系:继承

  • 2.用于注入数据的
    @Autowired

作用:自动按照类型注入,只要有唯一类型匹配就能注入。如果注入的bean在容器中类型不唯一时,它会把变量名称作为bean的id在容器中查找,找到后也能注入成功
当我们使用注解注入时,set方法就不是必须的

@Qualifier

作用:在自动按照类型注入的基础上,再按照bean的id注入,它在给类成员注入数据时,不能独立使用,但是在给方法的形参注入数据时,可以独立使用
属性:value:用于指定bean的id

@Resource(相当于上面两个结合起来)

作用:直接按照bean的id注入
属性:name:用于指定bean的id

以上三个注解都是用于注入其他bean类型,用于注入基本类型和String类型需要使用Value

@Value

作用:用于注入基本类型和String类型,它可以借助spring的el表达式读取properties文件中的配置
属性:value:用于指定要注入的数据

先配置xml

<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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    					http://www.springframework.org/schema/beans/spring-beans.xsd
    					http://www.springframework.org/schema/context
    					http://www.springframework.org/schema/context/spring-context.xsd">
	
	<!-- 告知spring在创建容器时要扫描的包,当配置了此标签之后,spring创建容器留会去指定的包及其子包下找对应的注解
			标签是在一个context的名称空间里,所以必须先导入context名称空间 -->
			
	<context:component-scan base-package="com"></context:component-scan>
	  
	  
</beans>

持久层

@Repository("customerDaoImpl")
public class CustomerDaoImpl implements ICustomerDao {

	public void saveCustomer(){
		System.out.println("持久层保存了客户");
	}
}

业务层

@Service("customerServiceImpl")
public class CustomerServiceImpl implements ICustomerService {
	@Value("xiaoming")
	private String name;
	
	@Autowired
	private ICustomerDao customerDao = null;
	
	public void saveCustomer(){
		System.out.println("业务层调用了持久层...."+name);
		customerDao.saveCustomer();
	}

}

表现层

public class Client {

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
		ICustomerService cs = (ICustomerService) ac.getBean("customerServiceImpl");
		cs.saveCustomer();

	}

}
  • 3.用于改变作用范围的

@Scope

作用:用于改变bean的作用范围
属性:value:用于指定范围的取值.
取值和xml中scope属性的取值一样的。singleton,prototype,request,session,globalsession

4.和生命周期相关的
5.spring的新注解

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值