一、Spring的相关配置

1.1 Bean元素

  • class属性:被管理对象的完整类名
  • name属性:给Bean起个名字,能重复,能使用特殊字符.后来属性
  • id属性:给Bean起个名字,不能重复,不能使用特殊字符.早期属性
  • scope属性
    • singleton:默认值,单例对象.项目一启动就创建对象,而且容器只创建一次
    • prototype:多例原型.被标识为多例的对象,每次在获得才会创建.每次创建都是新的对象(整合struts2时,ActionBean必须配置为多例的.)
    • request(了解):web项目中,Spring创建一个Bean的对象,将对象存入到request域中。.对象与request生命周期一致.
    • session(了解):web项目中,Spring创建一个Bean的对象,将对象存入到session域中。,对象与session生命周期一致.
  • 生命周期属性(了解)
    • init-method:指出初始化方法,spring会在对象创建之后立即调用
    • destory-method:指出销毁方法,spring在关闭并销毁所有容器中的对象之前调用.

1.2 spring创建对象的方式

【方式一:无参数的构造方法】

  

  spring容器启动后就创建User对象。如果对象中提供了带参构造,而没有提供无参构造,这样讲无法创建对象,程序会报错:

警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'user' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [cn.itcast.domain.User]: No default constructor found; nested exception is java.lang.NoSuchMethodException: cn.itcast.domain.User.<init>()

【方式二:静态工厂创建】(了解)

  

【方式三:实例工厂创建】(了解)

  

1.3 Spring的分模块配置

<!-- 导入其他spring配置文件 -->
<import resource="cn/itcast/demo/applicationContext.xml"/>

1.4 管理容器在项目中的生命周期

【让spring容器随项目的启动而创建,随项目的关闭而销毁】

在web.xml中配置

<!-- 可以让spring容器随项目的启动而创建,随项目的关闭而销毁 -->
<listener>
<!-- 注意:引入此类前,要导入spring-web-4.2.4.RELEASE.jar -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定加载spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

 【在Action中获得容器中的Service对象】

  public String execute() throws Exception {
// 获得spring容器=>从Application域获得即可
// 1.获得servletContext对象
ServletContext servletContext = ServletActionContext.getServletContext();
// 2.从servletContext中获得applicationContext容器
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
// 3.从容器中获得CustomerService
CustomerService service = (CustomerService)applicationContext.getBean("customerService");
return NONE;
}

错误示例:(这样会导致每次请求都会创建新的容器)

// 创建容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerService service = (CustomerService)applicationContext.getBean("customerService"); 

二、属性注入

2.1 注入方式

【set方法注入】

<!-- set方式注入: -->
<bean name="user" class="cn.itcast.bean.User" >
<!--值类型注入: 为User对象中名为name的属性注入tom作为值 -->
<property name="name" value="tom" ></property>
<property name="age" value="18" ></property>
<!-- 引用类型注入: 为car属性注入下方配置的car对象 -->
<property name="car" ref="car" ></property>
</bean> <!-- 将car对象配置到容器中 -->
<bean name="car" class="cn.itcast.bean.Car" >
<property name="name" value="兰博基尼" ></property>
<property name="color" value="黄色" ></property>
</bean>

【构造函数注入】

  

 【p名称空间注入】(了解)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "> <!-- p名称空间注入, 走set方法
1.导入P名称空间 xmlns:p="http://www.springframework.org/schema/p"
2.使用p:属性完成注入
|-值类型: p:属性名="值"
|-对象类型: p:属性名-ref="bean名称"
-->
<bean name="user3" class="cn.itcast.bean.User" p:name="jack" p:age="20" p:car-ref="car" > </bean>
</beans>

【spel注入】(了解)

<!--
spel注入: spring Expression Language sping表达式语言
-->
<bean name="user4" class="cn.itcast.bean.User" >
<property name="name" value="#{user.name}" ></property>
<property name="age" value="#{user3.age}" ></property>
<property name="car" ref="car" ></property>
</bean>

2.2 复杂类型的注入

【数组】

<!-- 复杂类型注入 -->
<bean name="cb" class="cn.itcast.c_injection.CollectionBean" >
<!-- 如果数组中只准备注入一个值(对象),直接使用value|ref即可
<property name="arr" value="tom" ></property>
-->
<!-- array注入,多个元素注入 -->
<property name="arr">
<array>
<value>tom</value>
<value>jerry</value>
<ref bean="user4" />
</array>
</property>
</bean>

【list】

<bean name="cb" class="cn.itcast.c_injection.CollectionBean" >
<!-- 如果List中只准备注入一个值(对象),直接使用value|ref即可
<property name="list" value="jack" ></property>-->
<property name="list" >
<list>
<value>jack</value>
<value>rose</value>
<ref bean="user3" />
</list>
</property>
</bean>

【map】

<bean name="cb" class="cn.itcast.c_injection.CollectionBean" >
<!-- map类型注入 -->
<property name="map" >
<map>
<entry key="url" value="jdbc:mysql:///crm" ></entry>
<entry key="user" value-ref="user4" ></entry>
<entry key-ref="user3" value-ref="user2" ></entry>
</map>
</property>
</bean>

【Properties】

<!-- prperties 类型注入 -->
<property name="prop" >
<props>
<prop key="driverClass">com.jdbc.mysql.Driver</prop>
<prop key="userName">root</prop>
<prop key="password">1234</prop>
</props>
</property>

三、spring与Junit整合测试

  首先要导入Junit测试包:

  测试代码如下:

// 帮我们创建容器
@RunWith(SpringJUnit4ClassRunner.class)
// 指定创建容器时使用哪个配置文件
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
// 将名为user的对象注入到user变量中
@Resource(name="user")
private User user; @Test
public void fun1() throws Exception {
// 1.创建容器对象
// ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2.向容器"要"user对象
// User user = (User) applicationContext.getBean("user");
// 3.打印user对象
System.out.println(user);
}
}

Spring学习笔记(二)——Spring相关配置&属性注入&Junit整合的更多相关文章

  1. Spring4学习笔记二:Bean配置与注入相关

    一:Bean的配置形式 基于XML配置:在src目录下创建 applicationContext.xml  文件,在其中进行配置. 基于注解配置:在创建bean类时,通过注解来注入内容.(这个不好,因 ...

  2. Java框架spring 学习笔记(六):属性注入

    属性注入:创建对象的时候,向类里面的属性设置值. Java属性注入有三种方法: 使用set方法注入 有参数构造注入 使用接口注入 Spring框架里面的属性注入方式 有参数构造属性注入 set方法属性 ...

  3. spring学习笔记二:spring使用构造方法注入(set方式注入)

    项目目录树: 1.spring的依赖包配置 * SPRING_HOME/dist/spring.jar * SPRING_HOME/lib/log4j/log4j-1.2.14.jar * SPRIN ...

  4. Spring学习笔记之 Spring IOC容器(一)之 实例化容器,创建JavaBean对象,控制Bean实例化,setter方式注入,依赖属性的注入,自动装配功能实现自动属性注入

    本节主要内容:       1.实例化Spring容器示例    2.利用Spring容器创建JavaBean对象    3.如何控制Bean实例化    4.利用Spring实现bean属性sett ...

  5. spring学习笔记(一) Spring概述

    博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书.  强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...

  6. Java架构师之路 Spring学习笔记(一) Spring介绍

    前言 这是一篇原创的Spring学习笔记.主要记录我学习Spring4.0的过程.本人有四年的Java Web开发经验,最近在面试中遇到面试官总会问一些简单但我不会的Java问题,让我觉得有必要重新审 ...

  7. Springboot学习笔记(六)-配置化注入

    前言 前面写过一个Springboot学习笔记(一)-线程池的简化及使用,发现有个缺陷,打个比方,我这个线程池写在一个公用服务中,各项参数都定死了,现在有两个服务要调用它,一个服务的线程数通常很多,而 ...

  8. Spring学习记录(二)---容器和bean属性配置

    下载spring包,在eclipse搭建spring环境. 这步我在eclipse中无法导入包,看网上的: http://sishuok.(和谐)com/forum/blogPost/list/242 ...

  9. Spring学习笔记之 Spring IOC容器(二) 之注入参数值,自动组件扫描方式,控制Bean实例化方式,使用注解方式

     本节主要内容:    1. 给MessageBean注入参数值    2. 测试Spring自动组件扫描方式    3. 如何控制ExampleBean实例化方式    4. 使用注解方式重构Jdb ...

随机推荐

  1. java基础之对象当做参数传进方法的堆栈内存解析

    值类型当做参数传进方法: 引用类型对象当做参数传进方法: String字符串当做参数传进方法:

  2. Java Http 请求

    package zr.weixin.com.utils; import java.io.BufferedReader; import java.io.IOException; import java. ...

  3. Docker 学习笔记_安装和使用MongoDB

    一.准备 1.宿主机OS:Win10 64 2.虚拟机OS:Ubuntu18.04 3.账号:docker 二.安装 1.搜索MongoDB镜像                            ...

  4. Hyperledger Chaincode启动过程

    Chaincode 启动过程 简介 这里讲的 Chaincode 是用户链码(User Chaincode,UCC),对应用开发者来说十分重要,它提供了基于区块链分布式账本的状态处理逻辑,基于它可以开 ...

  5. 学习Vue.js需要了解的部分内容

    重要: 1.如果要通过js/模板引用 图片到项目,图片路径需要使用require. 2.$event: $event 等于$emit 抛出的值,还可以使用$event.target.value. $e ...

  6. c# 二分查找法(2分钟算法)

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  7. Oracle——SQL基础

    一.SQL语句分为以下三种类型: DML: Data Manipulation Language 数据操纵语言DDL: Data Definition Language 数据定义语言DCL: Data ...

  8. Responsive设计——meta标签

    media-queries.js(http://code.google.com/p/css3-mediaqueries-js/) respond.js(https://github.com/scott ...

  9. [GO]结构体及普通变量初始化

    结构体是一种聚合的数据类型,它是由一系列相同类型或者不同类型的数据构成的数据集合,每个数据称为结构体的成员 1.结构体的初始化 package main import "fmt" ...

  10. 对于网站,APP开发流程的理解

    • 明确产品目标用户,目标市场 • 明确将要开发的产品面世后是要解决什么样的问题 • 梳理产品有哪些功能点,功能点如何按照模块划分 • 站在用户角度思考用户怎样使用这款产品,以故事的情景讲述用户如何使 ...