Spring使用入门

Spring使用入门

  • 使用maven创建工程
  • 引入spring-framework依赖
  • 添加配置文件
  • 创建ClassPathApplicationContext容器
  • 使用容器对象获取对象

注意:默认创建的是单例对象,并且对象是在容器创建的时候一并创建的,而多例则相反


ApplicationContext和BeanFactory的对比

创建对象的时间点不一样。
ApplicationContext:只要一读取配置文件,默认情况下就会创建对象。
BeanFactory:什么使用什么时候创建对象。
ApplicationContext作为BeanFactory的子接口,具备更多的方法和功能


ApplicationContext实现类的介绍

  • ClassPathXmlApplicationContext:

​ 它是从类的根路径下加载配置文件 推荐使用这种

  • FileSystemXmlApplicationContext:

​ 它是从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置。

  • AnnotationConfigApplicationContext:

​ 当我们使用注解配置容器对象时,需要使用此类来创建spring容器。它用来读取注解。

bean标签介绍

作用:
	用于配置对象让spring来创建的。
	默认情况下它调用的是类中的无参构造函数。如果没有无参构造函数则不能创建成功。
属性:
	id:给对象在容器中提供一个唯一标识。用于获取对象。
	class:指定类的全限定类名。用于反射创建对象。默认情况下调用无参构造函数。
	scope:指定对象的作用范围。
			* singleton	:默认值,单例的.
			* prototype	:多例的.
			* request	:WEB项目中,Spring创建一个Bean的对象,将对象存入到request域中.
			* session	:WEB项目中,Spring创建一个Bean的对象,将对象存入到session域中.
			* global session	:WEB项目中,应用在Portlet环境.如果没有Portlet环境那么globalSession相当于session.

	init-method:指定类中的初始化方法名称。
	destroy-method:指定类中销毁方法名称。

bean的生命周期

  • 单例对象:scope=“singleton”

​ 一个应用只有一个对象的实例。它的作用范围就是整个引用。
生命周期:
对象出生:当应用加载,创建容器时,对象就被创建了。
对象活着:只要容器在,对象一直活着。
对象死亡:当应用卸载,销毁容器时,对象就被销毁了。

  • 多例对象:scope=“prototype”

​ 每次访问对象时,都会重新创建对象实例。
生命周期:
对象出生:当使用对象时,创建新的对象实例。
对象活着:只要对象在使用中,就一直活着。


实例化Bean的三种方式

  • 使用无参构造的方式

–>

  • 静态工厂(需要额外创建一个静态工厂类,比较麻烦)

/**

  • 模拟一个静态工厂,创建业务层实现类
    */
    ublic class StaticFactory {
    public static AccountService createAccountService(){
    return new AccountServiceImpl();
    }
    }
  • 实例工厂创建(需要额外创建一个工厂类,比较麻烦)

/**

  • 模拟一个实例工厂,创建业务层实现类
  • 此工厂创建对象,必须现有工厂实例对象,再调用方法
    */
    public class InstanceFactory {
    public AccountService createAccountService(){
    return new AccountServiceImpl();
    }
    }



依赖注入(DI)

  • 构造函数注入
  • set方法注入
  • p名称空间注入(set注入的变种)
构造函数注入
public class AccountServiceImpl implements AccountService {
	
	private String name;
	private Integer age;
	private Date birthday;
		
	public AccountServiceImpl(String name, Integer age, Date birthday) {
		this.name = name;
		this.age = age;
		this.birthday = birthday;
	}

	@Override
	public void saveAccount() {
		System.out.println(name+","+age+","+birthday);	
	}
}

<!-- 使用构造函数的方式,给service中的属性传值
	要求:
		类中需要提供一个对应参数列表的构造函数。
	涉及的标签:
		constructor-arg
			属性:
				index:指定参数在构造函数参数列表的索引位置
				type:指定参数在构造函数中的数据类型
				name:指定参数在构造函数中的名称	,用这个找给谁赋值
				
				=======上面三个都是找给谁赋值,下面两个指的是赋什么值的==============
				
				value:它能赋的值是基本数据类型和String类型
				ref:它能赋的值是其他bean类型,也就是说,必须得是在配置文件中配置过的bean
-->
<!--定义初始化一个Date的Bean-->
<bean id="date" class="java.util.Date"></bean>

name方式:
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
	<constructor-arg name="name" value="张三"></constructor-arg>
	<constructor-arg name="age" value="18"></constructor-arg>
	<constructor-arg name="birthday" ref="date"></constructor-arg>
</bean>

type方式:
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
  <constructor-arg type="java.lang.String" value="张三" />
  <constructor-arg type="java.lang.Integer" value="20" />
  <constructor-arg type="java.util.Date" ref="date" />
</bean>

index方式:
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
   <constructor-arg index="0" value="张三" />
   <constructor-arg index="1" value="20" />
   <constructor-arg index="2" ref="date" />
</bean>
set方法注入
public class AccountServiceImpl implements AccountService {
	
	private String name;
	private Integer age;
	private Date birthday;
	
	public void setName(String name) {
		this.name = name;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	@Override
	public void saveAccount() {
		System.out.println(name+","+age+","+birthday);	
	}
}

<!-- 通过配置文件给bean中的属性传值:使用set方法的方式
	涉及的标签:
		property
		属性:
			name:找的是类中set方法后面的部分
			ref:给属性赋值是其他bean类型的
			value:给属性赋值是基本数据类型和string类型的
	实际开发中,此种方式用的较多。
-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
		<property name="name" value="test"></property>
		<property name="age" value="21"></property>
		<property name="birthday" ref="date"></property>
</bean>
<bean id="date" class="java.util.Date"></bean>
P名称空间注入
public class AccountServiceImpl implements AccountService {
	
	private String name;
	private Integer age;
	private Date birthday;
	
	public void setName(String name) {
		this.name = name;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	@Override
	public void saveAccount() {
		System.out.println(name+","+age+","+birthday);	
	}
}

配置文件代码:
<?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">

	<bean id="date" class="java.util.Date" />

	<bean id="accountService" 
		  class="com.itheima.service.impl.AccountServiceImpl"
		  p:name="test" p:age="21" p:birthday-ref="date"/>
</beans>
注入集合属性
javaBean
public class AccountServiceImpl implements AccountService {
	
	private String[] myStrs;
	private List<String> myList;
	private Set<String> mySet;
	private Map<String,String> myMap;
	private Properties myProps;
	
	public void setMyStrs(String[] myStrs) {
		this.myStrs = myStrs;
	}
	public void setMyList(List<String> myList) {
		this.myList = myList;
	}
	public void setMySet(Set<String> mySet) {
		this.mySet = mySet;
	}
	public void setMyMap(Map<String, String> myMap) {
		this.myMap = myMap;
	}
	public void setMyProps(Properties myProps) {
		this.myProps = myProps;
	}

	@Override
	public void saveAccount() {
		System.out.println(Arrays.toString(myStrs));
		System.out.println(myList);
		System.out.println(mySet);
		System.out.println(myMap);
		System.out.println(myProps);
	}
}
<!-- 注入集合数据 
	 List结构的:
		array,list,set
	Map结构的
		map,entry,props,prop
-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
	<!-- 在注入集合数据时,只要结构相同,标签可以互换 -->
	<!-- 给数组注入数据 -->
	<property name="myStrs">
		<set>
			<value>AAA</value>
			<value>BBB</value>
			<value>CCC</value>
		</set>
	</property>
	<!-- 注入list集合数据 -->
	<property name="myList">
		<array>
			<value>AAA</value>
			<value>BBB</value>
			<value>CCC</value>
		</array>
	</property>
	<!-- 注入set集合数据 -->
	<property name="mySet">
		<list>
			<value>AAA</value>
			<value>BBB</value>
			<value>CCC</value>
		</list>
	</property>
	<!-- 注入Map数据 -->
	<property name="myMap">
		<props>
			<prop key="testA">aaa</prop>
			<prop key="testB">bbb</prop>
		</props>
	</property>
	<!-- 注入properties数据 -->
	<property name="myProps">
		<map>
			<entry key="testA" value="aaa"></entry>
			<entry key="testB">
				<value>bbb</value>
			</entry>
		</map>
	</property>
</bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值