Spring学习笔记二(Bean注入的几种方式)

 

 1.前言

上一篇博客从宏观上讲解了一下Spring的知识,下面这篇来着重讲解一下有关Bean注入的几种方式。


 2.Bean注入的几种方式

2.1 类构造器初始化

这也是默认的方式,在上一篇博客中也有所体现。直接在applicationContext.xml配置文件中,配置Bean标签即可

<span style="font-family:SimSun;font-size:18px;"><!-- 实例工厂初始化 -->
	<!-- 必须先创建实例工厂对应的Bean -->
	<bean id="Bean2Create" class="com.ioc.Bean2Create">
	</bean></span>


2.2 静态工厂初始化

工厂方法

<span style="font-family:SimSun;font-size:18px;">//静态工厂方法
public class Bean2Factory {
	//工厂方法
	public static Bean getBeanFactory(){
		return new Bean();
	}
}</span>

配置文件中配置

<span style="font-family:SimSun;font-size:18px;"><!-- 通过静态工厂进行注入,后面放置的是静态工厂的方法 -->
	<!-- class:静态工厂的类名 -->
	<!-- factory-method:静态工厂中用于创建对象的方法 -->
	<bean id="Bean2Factory" class="com.ioc.Bean2Factory" factory-method="getBeanFactory">
	</bean></span>

工厂获取Bean


<span style="font-family:SimSun;font-size:18px;">package com.junit;

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

import com.ioc.Bean;
import com.ioc.Bean3;

public class Bean2FactoryTest {

	@Test
	public void test() {
		
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		//通过工厂进行获取Bean
		Bean bean=ctx.getBean("Bean2Factory", Bean.class);
		bean.test();
		
		/*通过实例工厂进行初始化*/
		Bean3 Bean3=ctx.getBean("Bean3", Bean3.class);
		Bean3.test();
				
	}

}
</span>

2.3 实例工厂获取

实例工厂与静态工厂很类似,只不过静态工厂采取的静态方法而已。主要区别在配置文件上。必须先实例化实例工厂,然后才可以实例化工厂创建的对象

<span style="font-family:SimSun;font-size:18px;">	<!-- 实例工厂初始化 -->
	<!-- 必须先创建实例工厂对应的Bean -->
	<bean id="Bean2Create" class="com.ioc.Bean2Create">
	</bean>
	
	
	<bean id="Bean3" factory-bean="Bean2Create" factory-method="getBean">
		<property name="Name" value="#{'123张是哪'}"/>
		<property name="country">
			<list>
				<value>"sdfsadf"</value>
				<value>"sdfsadf"</value>
				<value>"sdfsadf"</value>
				<value>"sdfsadf"</value>
				<value>"sdfsadf"</value>
			</list>
		</property>
		
		<property name="names">
			<set>
				<value>"sdfsadf"</value>
				<value>"54545"</value>
				<value>"sdfsadf"</value>
				<value>"sdfsadf"</value>
				<value>"sdfsadf"</value>
			</set>
		</property>
	</bean></span>


 3.Bean的作用域和生命周期

3.1 作用域

Spring默认创建的对象是单例模式的对象

       设置Bean的作用域,通过bean元素的scope属性完成

       <beanid="bean4" class="cn.itcast.bean.xml.Bean1" scope="prototype">

       scope取值范围:

•    singleton:单例

•    prototype:非单例

•    Request:创建该Bean,并调用request.setAttribute(“beanId”,beanObj);

•    Session:创建该Bean,并调用request.getSession().setAttribute(“beanId”,beanObj);

•    globalSession:全局Session

分布式服务器


3.2 生命周期

Bean的初始化过程,已经被Spring完全包装完成,无法人工干预。但是Spring为我们预留了两个回调的方法。这也是Bean的两个生命周期的方法,可以在配置文件中进行定义。

<span style="font-family:SimSun;font-size:18px;"><bean id="bean5" class="cn.itcast.bean.xml.Bean1" 
		init-method="init" 
		destroy-method="destory">
	</bean>
</span>

或者也可以通过注解的形式进行定义:@PostConstruct和@PreDestroy


 4.属性和集合注入

4.1 属性注入

<span style="font-family:SimSun;font-size:18px;">   <!-- 构造器初始化 -->
   <!-- Bean的作用域,默认值为simple -->
	<bean id="userService" class="com.test.UserService" scope="prototype">
		<constructor-arg index="1" value="123"/>
		<constructor-arg index="0" value="女"/>
		<property name="id" value="123"/>
		<property name="name" value="xiaoming"/>
	</bean></span>


最常用的是属性注入

4.2  集合注入

常用的集合有list、数组、Set、Map、Properties等,注入的方式如下

<span style="font-family:SimSun;font-size:18px;"><!-- 集合注入 -->
	<bean id="bean10" class="cn.itcast.bean.xml.Bean10">
		<property name="country">
			<list>
				<value>中国</value>
				<value>美国</value>
				<value>韩国</value>
				<value>意大利</value>
			</list>
		</property>
		<property name="names">
			<set>
				<value>jock</value>
				<value>jockme</value>
				<value>zahc</value>
				<value>pkzahc</value>
			</set>
		</property>
		<property name="likes">
			<props>
				<prop key="BADU">百度</prop>
				<prop key="NQ">网秦</prop>
				<prop key="SINA">新浪</prop>
			</props>
		</property>
	</bean></span>


 5.注解开发

 如果需要使用注解的话,需要在配置文件中定义类的加载,Spring通过XML格式,定义加入扫描的路径。

<span style="font-family:SimSun;font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:p="http://www.springframework.org/schema/p"
       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.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context.xsd
		">
	<!-- 开启自动扫描路径 ,含子包路径-->
	<context:component-scan base-package="cn.itcast"/>
</beans>




</span>

常用的注解标签

1.@Component、@Repository、@Service、@Controller,这三种的注解功能完全相同,仅仅是名称上的区别而已。

2.@Autowired:自动装配注入属性

  @Qualifier:自动注入对象类型的属性

  @Value:为属性注入的简单类型的值  

 @Resource:与@Autowired功能类似,自动注入Bean属性

  @Scope:为当前Bean指定Scope参数

<span style="font-family:SimSun;font-size:18px;">package com.ioc;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("Bean4")
public class Bean4 {

	//自动注入值类型
	@Autowired
	@Value("haha")
	private String name;
	
/*	@Autowired()
	@Qualifier("Bean44")*/
	//自动注入引用类型
	@Resource(name="Bean44")
	private Bean5 bean5;
	public void test(){
		bean5.test();
		System.out.println("测试");
		System.out.println(name);
	}
}
</span>


 






评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值