搜索
您的当前位置:首页正文

Spring-自动注入、加载配置文件、scope属性

来源:步旅网

1.Spring自动注入

先看两个实体类

public class People {
	private Teacher teacher;

	public People() {
		super();
		
	}

	public People(Teacher teacher) {
		super();
		this.teacher = teacher;
	}

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	@Override
	public String toString() {
		return "People [teacher=" + teacher + "]";
	}
	

}
public class Teacher {
	//@Value("${id}")
	private String id;
	//@Value("${name}")
	private String name;
	public Teacher() {
		super();
		
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "Teacher [id=" + id + ", name=" + name + "]";
	}
	

}

People类中封装的是Teacher类 ,以前我们都是通过以下配置将Teacher注入People

<bean id="teacher" class="cn.edu.pojo.Teacher" ></bean>
<bean id="people" class="cn.edu.pojo.People">
<property name="teacher" ref="teacher"></property>
</bean>

 缺点:过程繁琐

自动注入概述:

1.在 Spring 配置文件中对象名和 ref=”id”id 名相同使用自动注入,可以 不配置

2.两种配置办法

2.1 在中通过 autowire=”” 配置,只对这个生效

2.2 在中通过 default-autowire=””配置,表当当前文件中所 有都是全局配置内容

3.autowire=”” 可取值

3.1 default: 默认值,根据全局 default-autowire=””值.默认全局和局 部都没有配置情况下,相当于 no

3.2 no: 不自动注入

<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="no">
<!--默认不自动注入,需要手动注入-->

<bean id="teacher" class="cn.edu.pojo.Teacher" ></bean>
<bean id="people" class="cn.edu.pojo.People">
<property name="teacher" ref="teacher"></property>
</bean>
</beans>

3.3 byName: 通过名称自动注入.在 Spring 容器中找类的 Id

<bean id="teacher" class="cn.edu.pojo.Teacher"></bean>
<bean id="people" class="cn.edu.pojo.People" autowire="byName"></bean>

说明:通过byName注入时,必须保证配置文件中Teacher的bean的id与People实体类中的Teacher属性的变量名相同

即:private Teacher teacher  与<bean id="teacher" class="cn.edu.pojo.Teacher"></bean>

3.4 byType: 根据类型注入.

<bean id="teacher" class="cn.edu.pojo.Teacher"></bean>
<bean id="people" class="cn.edu.pojo.People" autowire="byType"></bean>

说明:通过byType注入,需要保证Spring容器已经管理People实体类的属性(Teacher)的对象 

3.4.1 spring 容器中不可以出现两个相同类型的

3.5 constructor: 根据构造方法注入.

<bean id="tea" class="cn.edu.pojo.Teacher"></bean>
<bean id="people" class="cn.edu.pojo.People" autowire="constructor"></bean>

3.5.1 提供对应参数的构造方法(构造方法参数中包含注入对 戏那个)

3.5.2 底层使用 byName, 构造方法参数名和bean的 id 相同.

即:

<bean id="tea" class="cn.edu.pojo.Teacher"></bean>与public People(Teacher tea) { super();
        this.teacher = tea; }

2.Spring加载配置文件

1. 在 src 下新建 xxx.properties 文件

jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/my?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=20182022

2. 在 spring 配置文件中先引入 xmlns:context,在下面添加

<context:property-placeholder location="classpath:db.properties"/>

说明:如果需要记载多个配置文件逗号分割 

<?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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/beans/spring-tx.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/beans/spring-aop.xsd
    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-4.0.xsd">
<context:property-placeholder location="classpath:db.properties"/>
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 <property name="driverClassName" value="${jdbc.driverClassName}"></property>
 <property name="url" value="${jdbc.url}"></property>
 <property name="username" value="${jdbc.username}"></property>
 <property name="password" value="${jdbc.password}"></property>
 </bean>
  <!-- 创建SqlSessionFactory对象 -->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"></property>
</bean>
<!--扫描器相当于mybatis.xml中 mappers下package标签,扫描com.bjsxt.mapper包后会给对应接口创建对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 要扫描哪个包 -->
    	<property name="basePackage" value="cn.edu.mapper"></property>
    	<!-- 和factory产生关系 -->
    	<property name="sqlSessionFactoryBeanName" value="factory"></property>
</bean>
<bean id="per" class="cn.edu.mapper.PersonMapperImpl">
<property name="person" ref="personMapper"></property>
   
</bean>
</beans>      

 

注意:SqlSessionFactoryBean 的 id 不能叫做 sqlSessionFactory;需要修改, 把原来通过ref引用替换成value赋值,自动注入只能影响 ref,不会影响 value 赋值

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.bjsxt.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="factory"></property>
</bean>

3. 在被Spring管理的类中通过@Value(“${key}”)取出properties中内容

在src下新建my.properties

id=123
name=zhangSan

1 添加注解扫描 并配置文件路径

<context:component-scan base-package="cn.edu.pojo.Teacher"></context:component-scan>

 

<context:property-placeholder location="classpath:my.properties"/>

2 在类中添加

    @Value("${id}")
	private String id;
	@Value("${name}")
	private String name;

2.1 key 和变量名可以不相同

2.2 变量类型任意,只要保证 key 对应的 value 能转换成这个 类型就可以.

Spring配置文件如下:

<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<context:property-placeholder location="classpath:my.properties"/>
<context:component-scan base-package="cn.edu.pojo.Teacher"></context:component-scan>
<bean id="tea" class="cn.edu.pojo.Teacher"></bean>
<bean id="people" class="cn.edu.pojo.People" autowire="constructor"></bean>
</beans>

三.scope 属性

1. bean的属性

2. 作用:控制对象有效范围(单例,多例等)

3. 标签对应的对象默认是单例的.

3.1 无论获取多少次,都是同一个对象

4. scope 可取值

4.1 singleton 默认值,单例

4.2 prototype 多例,每次获取重新实例化

4.3 request 每次请求重新实例化

4.4 session 每个会话对象内,对象是单例的.

4.5 application 在 application 对象内是单例 4.6 global session spring 推 出 的 一 个 对 象 , 依 赖 于 spring-webmvc-portlet ,类似于 session

因篇幅问题不能全部显示,请点此查看更多更全内容

Top