SSM框架(二)SpringIOC容器的使用

目录

 

1. SpringIOC参数值注入

1.1 通过value属性或 实现基本类型数据注入(Setter方式)

1.2 通过value属性或 实现基本类型数据注入(构造器方式)

1.3 通过和实现集合元素的注入

1.4 通过 可以实现对Properties类型参数值的注入

1.5 Spring表达式加载properties配置文件进行注入

1.6 Spring的util标签

2. Spring自动装配

3. Spring基于注解的组件扫描


1. SpringIOC参数值注入

在上一章文章:https://blog.csdn.net/chen_hao_181/article/details/98324341中我已经提到了SpringIOC的概念,它实际上是一种思想;以前我们在创建对象的时候都会采取new关键字进行创建,而SpringIOC将改变这种方式,不用采取new的方式进行创建,在后面的类容加上注解扫描配置,Spring就会帮我们管理JavaBean,自动创建。

Spring依赖注入除了能实现类与类的之间的依赖关系外,还可以实现基本数据类型、集合等的注入。一般可通过一下几种方式完成注入。

1.1 通过value属性或 <value/> 实现基本类型数据注入(Setter方式)

举个例子,假如我们有个User类,里面有id,name,和一个Cat对象,依然是先完成javaBean的编写

package com.chtw.entity;

public class User {
	private int id;
	private String name;
	private Cat cat;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Cat getCat() {
		return cat;
	}
	public void setCat(Cat cat) {
		this.cat = cat;
	}
	
	
}

接下来编写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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    
	<bean id="cat" class="com.chtw.entity.Cat"></bean>

    <!--通过value属性实现基本类型数据注入(Setter方式)-->
	<!--<bean id="user" class="com.chtw.entity.User">
		<property name="id" value="1001"/>
		<property name="name" value="Alice"/>
		<property name="cat" ref="cat" />
	</bean>-->
	
    <!--通过 <value/> 实现基本类型数据注入(Setter方式)-->
    <bean id="user" class="com.chtw.entity.User">
		<property name="id">
            <value>1002</value>
        </property>
		<property name="name">
            <value>Bob</value>
        </property>
		<property name="cat">
            <ref bean="cat"/>
        </property>
	</bean>

    <!--这两种方法有一种就行-->
</beans>

1.2 通过value属性或 <value/> 实现基本类型数据注入(构造器方式)

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    
	<bean id="cat" class="com.chtw.entity.Cat"></bean>

    <!--通过value或者<value/>属性实现基本类型数据注入(构造器方式)-->
	<!--<bean id="user" class="com.chtw.entity.User">
		<constructor-arg index="0" value="1002">
	    </constructor-arg>
        <constructor-arg index="1">
		    <value>Bob</value>
	    </constructor-arg>
        <constructor-arg index="0">
		    <ref bean="cat"/>
	    </constructor-arg>
	</bean>-->
</beans>

1.3 通过<list/> <set/>和 <map/>实现集合元素的注入

package com.chtw.entity;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 测试集合注入
 * @author CHTW
 *
 */
public class TestCollection {

	private List<String> list;
	private Set<String> set;
	private Map<String, String> map;
	
	
	public List<String> getList() {
		return list;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public Set<String> getSet() {
		return set;
	}
	public void setSet(Set<String> set) {
		this.set = set;
	}
	public Map<String, String> getMap() {
		return map;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	
	
}
<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/util">

    <!-- 测试setter注入 -->
	<bean id="cat" class="com.chtw.entity.Cat"></bean>
	<bean id="user" class="com.chtw.entity.User">
		<property name="id" value="1001"/>
		<property name="name" value="Alice"/>
		<property name="cat" ref="cat" />
	</bean>
	
	<!-- 测试集合框架注入 -->
	<bean id="testCollection" class="com.chtw.entity.TestCollection">
		<property name="list">
			<list>
				<value>四川</value>
				<value>北京</value>
				<value>上海</value>
			</list>
		</property>
		<property name="set">
			<set>
				<value>苹果</value>
				<value>香蕉</value>
				<value>葡萄</value>
			</set>
		</property>
		<property name="map">
			<map>
				<entry key="科目一" value="语文"></entry>
				<entry key="科目二" value="数学"></entry>
				<entry key="科目三" value="英语"></entry>
				<entry key="科目四" value="物理"></entry>
			</map>
		</property>
	</bean>
</beans>

1.4 通过<props/> 可以实现对Properties类型参数值的注入

package com.chtw.entity;

import java.util.Properties;

public class TestProp {

	public Properties porps;
	
	public TestProp(Properties porps) {
		this.porps = porps;
	}
}
<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/util">
	
	<!-- 对Properties类型参数值的注入-->
	<bean id="testProp" class="com.chtw.entity.TestProp">
		<props name="porps">
			<prop key="driver">com.mysql.jdbc.Driver</prop>
			<prop key="url">jdbc:mysql://localhost:3306/test_db</prop>
			<prop key="username">root</prop>
			<prop key="password">123456</prop>
		</props>
	</bean>
	
</beans>

1.5 Spring表达式加载properties配置文件进行注入

在<property>元素的value属性中使用#{}界定符将值装配到Bean的属性中,其实和jstl和el表达式差不多,可以结合起来理解

jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test_db
username=root
password=123456
<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	
	<!-- 加载配置文件 -->
	<util:properties id="jdbc" location="classpath:jdbc.properties"/>
	
	
	<!-- 测试配置文件 -->
	<bean id="testProp" class="com.chtw.entity.TestProp">
		<constructor-arg index="0" ref="jdbc"/>
	</bean>
	
</beans>

1.6 Spring的util标签

使用<list/>、<map/>、<set/>等元素定义集合的时候不能够在不同的受管Bean间进行复用,后来,<util/>命名空间被Spring 2.x引入,使得集合的定义变得简单 ,比如<util:list> <util:set> <util:map>、<util:properties>,上面这个例子里面应用文件的时候就应用了util标签。

需要注意的是如果要使用util标签,则需要在头文件添加两句配置:

 xmlns:util="http://www.springframework.org/schema/util"

和在xsi:schemaLocation里面添加:

"http://www.springframework.org/schema/util"               

"http://www.springframework.org/schema/util/spring-util-3.2.xsd"

2. Spring自动装配

Spring能自动装配Bean与Bean之间的依赖关系,不使用ref指定依赖Bean,由Spring容器检查XML配置文件内容。根据某种规则,为调用者Bean注入被依赖的Bean通过根标签<beans../>元素的default-autowire属性指定,该属性对配置文件中所有的Bean起作用;也可以通过<bean>元素的autowire属性指定,该属性只对当前bean起作用

spring的自动装配有5种方法

1. no:默认不进行装配,由ref元素完成bean依赖注入

2. byName:  Spring容器以属性名作为id来查找对应的bean,调用set方法来完成注入

3. byType :Spring容器查找与 class属性类型一致的bean,然后调用set方法来注入

4. constructor:  容器查找与class属性类型一致的bean,然后调用构造器里完成

5. autodect:  Spring容器根据Bean内部结构,后面被弃用了

3. Spring基于注解的组件扫描

注解和组件扫描可以说是spring最重要的一个特点了,它可以帮我省去很多bean的定义和配置,在后面的spring mvc框架中更加更够体现这一好处。

他的配置也很简单,在applicationContext.xml中添加一句<context:component-scan base-package="com.chtw"/>

然后需要导入jar工具包:

      commons-logging.jar                   

      spring-beans-4.2.4.RELEASE.jar

      spring-context-4.2.4.RELEASE.jar 

      spring-core-4.2.4.RELEASE.jar

      spring-expression-4.2.4.RELEASE.jar

      spring-aop-4.2.4.RELEASE.jar  

      spring-aspects-4.2.4.RELEASE.jar

常见bean实例创建的注解(有以下注解的类会被自动注册成bean):

  @Component    通用注解,是所有受Spring 管理组件的通用形式,一般来说不推荐使用该注解

  @ Service:           业务层注解,

  @ Repository :     数据持久层注解(DAO 

  @Autowire  放在set方法前,或者添加到属性前

本人联系方式2329095893,欢迎各位进行学习讨论

欢迎关注熊熊出没ING公众号,不定时跟新Java、python、信息安全等相关知识哦。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值