一、springIOC

控制反转和依赖注入

​ 简单的说就是将对象的创建,属性的的设置交给spring容器进行管理,而不再由用户自己创建,当用户需要使用该接口或者类的时候,直接注入就可以了,spring容器会自动帮助用户创建对象。

1.创建maven应用程序

【pom.xml】

​ 1.引入spring依赖,junit依赖

​ 2.引入maven插件——java编译插件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itcloud</groupId>
<artifactId>resource</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>resource</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.15.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>

该依赖会下载下面jar

<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
  • org.springframework:spring-aop:4.3.15.RELEASE
  • org.springframework:spring-beans:4.3.15.RELEASE
  • org.springframework:spring-core:4.3.15.RELEASE
  • org.springframework:spring-expression:4.3.15.RELEASE

2.springIOC基础

基本概念:springIOC主要作用是用来管理javaBean的容器,将java对象以及对象和对象之间的关系交由Spring容器管理。

在没有spring容器之前对接口或者类进行实例化的时候都需要使用new关键字,来进行对象的创建,那么自从有了spring,那么这些事情就交给了spring来做了。

2.1.了解spring的几种注入方式

【Teacher.java】

​ getter和setter方法在这里都会被省略。

一个老师对应多个学生,老师pojo类中包含setter注入,和List集合注入

public class Teacher implements Serializable {
private Long id;
private String name;
private Integer age;
private List<Student> students;
}

【Student.java】

一个学生对应一个老师,学生包含多种注入方式,有setter,Properties类注入,map注入以及构造方法注入

注意点,

​ 1.如果添加了有参构造方法(没有参构造),那么在进行注入的时候必须要进行构造方法的注入

​ 2.如果既有有参构造和无参构造可以不进行构造方法的注入

public class Student implements Serializable {
private Long id;
private String name;
private Teacher teacher; private Properties pro; private Map<String,Object> map;
public Student(){}
public Student(Long id, String name){
this.id = id;
this.name = name;
}
}

【applicationContext.xml】**创建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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="student" class="com.itcloud.pojo.Student">
<property name="id" value="1"/>
<property name="name" value="小明"/>
</bean> <bean id="student2" class="com.itcloud.pojo.Student">
<!-- 构造方法注入 -->
<!--每一个标签都代表一个构造方法的属性,按照参数在构造方法中的顺序进行注入-->
<constructor-arg value="2"/>
<constructor-arg>
<value>张三</value>
</constructor-arg> <property name="teacher" ref="teacher" />
<!-- map注入 -->
<property name="map">
<map>
<entry key="1" value="语文" />
<entry key="2" value="数学" />
</map>
</property>
<!-- Properties注入 -->
<property name="pro">
<props>
<prop key="身高">1.8</prop>
<prop key="体重">70kg</prop>
</props>
</property>
</bean> <bean id="teacher" class="com.itcloud.pojo.Teacher"> <property name="id" value="100023" />
<property name="name" value="王老师" />
<property name="age" value="30" />
<!-- list集合注入 -->
<property name="students">
<list>
<ref bean="student2" />
<ref bean="student" />
</list>
</property>
</bean>
</beans>

【TestIOC.java】进行数据的测试,debug观察数据

package com.itcloud.pojo;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestIOC { //加载spring容器
private ApplicationContext context =
new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); @Test
public void testStudent() {
//context.getBean()获取spring容器中管理的bean,参数为Id
Student stu1 = (Student) context.getBean("student"); Student stu2 = context.getBean("student2", Student.class); Teacher teacher = context.getBean("teacher", Teacher.class); System.out.println("---------------------");
}
}
2.2.p标签和c标签的注入方式

​ p代表的就是属性,c代表的就是构造方法。

添加标签头,引入p和c

xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"

实例

c:id="3" c:name="smith"就是构造方法中的两个参数 id和name,p:teacher-ref="teacher"为类中中的属性teacher

<bean id="student3" class="com.itcloud.pojo.Student" c:id="3" c:name="smith" p:teacher-ref="teacher"/>
2.3.bean之间的继承关系

两个关键字段parentabstract

1.此时student3会继承student中的id,和name属性,这个是parent的作用,非必须标签

2.abstract表示student这个bean无法被实例化,即无法再代码中获取这个bean,也无法被外部所引用,非必须标签

​ 例如:ref="student"是错的

<bean id="student" class="com.itcloud.pojo.Student" abstract="true">
<property name="id" value="1"/>
<property name="name" value="小明"/>
</bean>
<bean id="student3" class="com.itcloud.pojo.Student" parent="student" p:teacher-ref="teacher"/>
2.4.bean的作用域

概述

作用域 描述
单例(singleton) (默认)每一个Spring IoC容器都拥有唯一的一个实例对象
原型(prototype) 一个Bean定义,任意多个对象

scope="singleton"默认值,只会产生一个实例化对象

scope="prototype"原型,每次获取bean的时候都会获取一个新的实例化对象

<bean id="student4" class="com.itcloud.pojo.Student" parent="student" p:teacher-ref="teacher" scope="singleton"/>
2.6.bean的生命周期

两个关键点:

​ 1.<bean/>标签中的字段:init-method=""表示bean初始化(构造方法)之后调用的方法 destroy-method=""容器关闭之后调用的方法.

​ 2. bean的后置处理器,需要实现方法,BeanPostProcessor这个类,两个方法:

postProcessBeforeInitialization():在每个bean初始化后(构造方法)调用一次(在init-method方法之前被调用)。

postProcessAfterInitialization():在init-method之后被调用,destroy-method之前被调用

实现案例0001

【LifeCycle.java】

package com.itcloud.pojo;

public class LifeCycle {

    public LifeCycle(){
System.out.println("构造方法初始化..........");
} public void init(){
System.out.println("init()初始化方法.......");
} public void destory(){
System.out.println("destory()销毁方法.......");
}
}

【applicationContext.xml】

init-method="init" destroy-method="destory"

<bean id="lifeCycle" class="com.itcloud.pojo.LifeCycle" init-method="init" destroy-method="destory"></bean>

【TestIOC.java】测试

public class TestIOC {

    //加载spring容器
private ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); @Test
public void testStudent() { LifeCycle lifeCycle = context.getBean("lifeCycle", LifeCycle.class);
context.close();
//测试结果
/*
信息: Loading XML bean definitions from class path resource [spring/applicationContext.xml]
构造方法初始化..........
init()初始化方法....... 四月 08, 2018 10:09:34 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@27ddd392: startup date [Sun Apr 08 10:09:33 CST 2018]; root of context hierarchy
destroy()销毁方法.......
*/
}
}

第二个关键点实现案例0002

【CycleProcessor.java】

package com.itcloud.pojo;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component; public class CycleProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
System.out.println("CycleProcessor start....." + name);
return bean;
} @Override
public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
System.out.println("CycleProcessor end....." + name);
return bean;
}
}

【TestIOC.java】测试类不变,测试结果:

/*
构造方法初始化..........
CycleProcessor start.....lifeCycle
init()初始化方法.......
CycleProcessor end.....lifeCycle
四月 08, 2018 10:13:31 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@58c1670b: startup date [Sun Apr 08 10:13:00 CST 2018]; root of context hierarchy
destroy()销毁方法.......
*/
2.7.工厂注入(了解即可)

注意点,如果工厂方法有参数,通过<constructor-arg value="xxx"></constructor-arg>进行参数匹配

静态工厂注入

public class StudentFactory {

    public static Student getInstance(){
Student stu = new Student();
stu.setId(10L);
stu.setName("小十");
return stu;
}
}
<bean id="student6" class="com.itcloud.pojo.StudentFactory" factory-method="getInstance"></bean>

测试

Student stu = context.getBean("student6", Student.class);

实例工厂注入

package com.itcloud.pojo;

public class TeacherFactory {
public Teacher getInstance(Long id, String name){
Teacher teacher = new Teacher();
teacher.setId(id);
teacher.setName(name);
return teacher;
}
}
<!-- 实例工厂必须单独配置一个bean -->
<bean id="teacherFactory" class="com.itcloud.pojo.TeacherFactory"/>
<bean id="teacher2" factory-bean="teacherFactory" factory-method="getInstance">
<constructor-arg>
<value>222</value>
</constructor-arg>
<constructor-arg name="name" value="张老师" /> </bean>

测试

Teacher teacher = context.getBean("teacher2", Teacher.class);

FactoryBean配置

跳转标志

package com.itcloud.pojo;

import org.springframework.beans.factory.FactoryBean;

public class TeacherFactoryBean implements FactoryBean {

    private String name;

    @Override
public Object getObject() throws Exception {
Teacher teacher = new Teacher();
teacher.setName(name);
return teacher;
} @Override
public Class<?> getObjectType() {
return Teacher.class;
} @Override
public boolean isSingleton() {
return true;
} public void setName(String name) {
this.name = name;
}
}
<!--FactoryBean方法配置bean-->
<bean id="teacherBean" class="com.itcloud.pojo.TeacherFactoryBean">
<property name="name" value="李老师"/>
</bean>
Teacher teacher = context.getBean("teacherBean", Teacher.class);

3.spring注解注入

​ 三个注解将类注入到spring容器中,注意点:注解默认注入的Id为当前类的名称首字母小写

  • @Repository主要用于dao,数据访问层
  • @Service用于Service层,调用数据访问层的方法,进行逻辑操作
  • @Component用户普通类的注册,用户自己定义的组件

我们知道Service层一定会调用dao层的相关方法,dao层已经被注册到Spring容器之中,这是后就需要使用Spring为我们提供的注解来引用对应的实例

  • @Autowired按照类型进行匹配,如果一个接口存在两个子类,可以配合@Qualifier注解来使用
  • @Resource按照名称进行匹配,
3.1简单应用应用案例

【applicationContext-annotation.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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c" 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">
<!-- 此配置表示com.itcloud包及其子包支持注解配置 -->
<context:component-scan base-package="com.itcloud"> </context:component-scan>
</beans>

【UserDAO.java】

package com.itcloud.dao;

public interface UserDAO {
//用户更新
int update();
}

【UserDAOImpl.java】注意点:@Repository的value的值默认是(userDAOImpl)

@Repository
public class UserDAOImpl implements UserDAO { @Override
public int update() {
System.out.println("进行数据库语句编写");
return 0;
}
}

【UserService.java】

package com.itcloud.service;

public interface UserService {
int doUpdate();
}

【UserServiceImpl.java】@Service的value的默认值是:userServiceImpl

ackage com.itcloud.service.impl;

import com.itcloud.dao.UserDAO;
import com.itcloud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDAO userDAO; @Override
public int doUpdate() {
System.out.println("UserServiceImpl update()方法开始....");
userDAO.update();
return 0;
}
}

测试

context.getBean("userDAOImpl");//结果:userDAOImpl@127 即:获取到UserDAO的对象

理解bean之间的相互调用

我们在UserServiceImpl这个类中可以看到如下这句话,这句话表示的意思就是引用外部bean,在没有Spring之前我们引入外部bean的过程是:private UserDAO userDAO = new UserDAOImpl(),在有了Spring之后,spring会自动帮我们进行对象的创建,以及维护对象之间的关系。

 	@Autowired
private UserDAO userDAO;

测试Autowired

 	@Test
public void testAnnotation(){
UserService userService = context.getBean(UserService.class);
userService.doUpdate();
}
//结果
/*
UserServiceImpl update()方法开始....
进行数据库语句编写
*/

前面提到,@Autowired是根据类型进行注入的,此时因为UserDAO只有一个子类,但是如果有两个子类要怎么书写呢:

	//方案一,官方推荐
@Autowired
@Qualifier("userDAOImpl")
private UserDAO userDAO;
//方案二
@Resource(name = "userDAOImpl")
private UserDAO userDAO;
//或者
@Resource
private UserDAO userDAOImpl;
3.2理解开启注解支持配置

【applicationContext-annotation.xml】

<context:component-scan base-package="com.itcloud">

</context:component-scan>

这里也可以添加子元素,对注解数据进行过滤

最常用的过滤方式

<context:component-scan base-package="com.itcloud" use-default-filters="false">
<!--只包含Service注解的bean,其他注解不会被扫描-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan>

use-default-filters="false"不使用默认过滤方式,如果为true的话,<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> 表示的意思只是将@Service包含进来,其他注解也会包含的

<!--表示不扫描Repository注解-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>

4.spring引用外部属性文件

4.1 applicationContext.xml文件配置bean并且注入属性文件中的内容

【value.properties】定义一个外部属性文件

value.driverName=com.mysql.jdbc.Driver
value.url=jdbc:mysql://localhost:3306/test
value.username=root
value.password=123456

【DataSource.java】定义属性类

package com.itcloud.value;

public class DataSource {

    private String driverName;

    private String username;

    private String password;

    private String url;
//getter setter方法略 }

【applicationContext-annotation.xml】在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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c" 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:properties/value.properties"/>
<bean id="dataSource" class="com.itcloud.value.DataSource">
<property name="username" value="${value.username}"/>
<property name="password" value="${value.password}"/>
<property name="driverName" value="${value.driverName}"/>
<property name="url" value="${value.url}"/>
</bean>
</beans>

此时,当spring容器加载的时候,DataSource.java 被实例化, value.properties属性文件中的内容会被注入到DataSource中。

4.2不通过配置文件的方式读取属性

【applicationContext-annotation.xml】开启注解配置

<context:component-scan base-package="com.itcloud"/>
<context:property-placeholder location="classpath:properties/value.properties"/>
<!--<bean id="dataSource" class="com.itcloud.value.DataSource">-->
<!--<property name="username" value="${value.username}"/>-->
<!--<property name="password" value="${value.password}"/>-->
<!--<property name="driverName" value="${value.driverName}"/>-->
<!--<property name="url" value="${value.url}"/>--> <!--</bean>-->

【DataSource.java】 此时可以没有setter方法

package com.itcloud.value;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component
public class DataSource { @Value("${value.driverName}")
private String driverName; @Value("${value.username}")
private String username; @Value("${value.password}")
private String password; @Value("${value.url}")
private String url;
}

三、spring成长之路——springIOC容器详解(上)的更多相关文章

  1. 四、spring成长之路——springIOC容器(下)

    目录 5.spring注解开发(Spring扩展知识) 5.1定义配置类:@Configuration 声明一个类为IOC容器 @Bean定义一个Bean 5.2.按照条件进行注入 5.3.@Impo ...

  2. Spring - SpringIOC容器详解

    一.什么是Spring IOC: Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想. 在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是 ...

  3. [Spring学习笔记 1 ] Spring 简介,初步知识--Ioc容器详解 基本原理。

    一.Spring Ioc容器详解(1) 20131105 1.一切都是Bean Bean可是一个字符串或者是数字,一般是一些业务组件. 粒度一般比较粗. 2.Bean的名称 xml配置文件中,id属性 ...

  4. Spring源码之九finishRefresh详解

    Spring源码之九finishRefresh详解 公众号搜索[程序员田同学],专职程序员兼业余写手,生活不止于写代码 Spring IoC 的核心内容要收尾了,本文将对最后一个方法 finishRe ...

  5. 【Spring】——声明式事务配置详解

    项目中用到了spring的事务: @Transactional(rollbackFor = Exception.class, transactionManager = "zebraTrans ...

  6. [js高手之路]深入浅出webpack教程系列2-配置文件webpack.config.js详解(上)

    [js高手之路]深入浅出webpack教程系列索引目录: [js高手之路]深入浅出webpack教程系列1-安装与基本打包用法和命令参数 [js高手之路]深入浅出webpack教程系列2-配置文件we ...

  7. 2017.2.13 开涛shiro教程-第十二章-与Spring集成(一)配置文件详解

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第十二章-与Spring集成(一)配置文件详解 1.pom.xml ...

  8. Spring Boot源码中模块详解

    Spring Boot源码中模块详解 一.源码 spring boot2.1版本源码地址:https://github.com/spring-projects/spring-boot/tree/2.1 ...

  9. TCP三次握手与四次挥手详解

    目录 TCP三次握手与四次挥手详解 1.TCP报文格式 2.TCP三次握手 3.TCP四次挥手 4.为什么建立连接需要三次握手? 5.为什么断开连接需要四次挥手? 6.为什么TIME_WAIT状态还需 ...

随机推荐

  1. 在Windows下为PHP5.6安装redis扩展和memcached扩展

    一.php安装redis扩展   1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本       2.根据PHP版本号,编译器版本号和CPU架构, 选择php_redis-2.2 ...

  2. PURGE 的用法说明

    PURGE PurposeUse the PURGE statement to remove a table or index from your recycle bin and release al ...

  3. ionic项目编译打包(android平台)

    ionic项目相关开发工作完成之后(建立ionic工程项目可以参考上一篇文章ionic项目工程建立),就可以进行项目的编译打包apk应用包. 打包编译需要在平台环境下,这里只记录下android平台打 ...

  4. 将eChart图片利用POI导出到Excel

    在使用POI进行将数据导出到Excel时, 若要将eChart在前端生成的统计图(如柱状图.折线图.饼图等)一并导出,使用POI在后台构建数据图比较复杂,因此我选择将eChart在前端的统计图的bas ...

  5. iTextSharp 使用详解(转)

    PDF文件是目前比较流行的电子文档格式,在办公自动化(OA)等软件的开发中,经常要用到该格式,但介绍如何制作PDF格式文件的资料非常少,在网上搜来搜去,都转贴的是同一段“暴力”破解的方法,代码片断如下 ...

  6. [翻译] HTKDragAndDropCollectionViewLayout

    HTKDragAndDropCollectionViewLayout Custom UICollectionViewLayout that works together with a custom U ...

  7. codeforces 497E Subsequences Return

    codeforces 497E Subsequences Return 想法 做完这题,学了一些东西. 1.求一个串不同子序列个数的两种方法.解一 解二 2.这道题 \(n\) 很大,很容易想到矩阵加 ...

  8. MySQL 触发器-更新字段时,status列会加一

    需求:当更新列中的字段时,列中的status字段,就会+1 表结构 CREATE TABLE `test_1` ( `id` int(11) DEFAULT NULL, `name` varchar( ...

  9. CF712D Memory and Scores

    题目分析 实际上两个人轮流取十分鸡肋,可以看作一个人取2t次. 考虑生成函数. 为了方便,我们对取的数向右偏移k位. 取2t次的生成函数为: \[ F(x)=(\sum_{i=0}^{2k}x_i)^ ...

  10. luogu P2742 【模板】二维凸包

    嘟嘟嘟 没错,我开始学凸包了. 其实挺简单的. 前置技能: 1.极坐标系 2.向量叉积 1.极坐标系 就是一种二维坐标系.只不过两个坐标分别表示向量和极轴的角度和自身的长度.对于不同的问题,极轴可以自 ...