Spring(一)--作用、IOC容器细节、搭配环境、Spring实验
1.生态体系庞大,全能型选手!【springmvc是其一个子模块,jdbcTemplate能直接操作数据库!】
2.将其他组件粘合在一起
比如将SpringMVC和Mybaits连在一起
3.包括:IOC容器和AOP【面向切面编程】
Spring的IOC机制(控制反转和依赖注入)正是用在此处。
Spring的IOC(控制反转和依赖注入)
控制反转[IOC]:就是由容器控制程序之间的(依赖)关系,而非传统实现中,由程序代码直接操控。
控制反转是一种思想,其具体实现就是依赖注入!
1.使用IOC容器创建对象
2.使用IOC容器在创建对象的同时,给对象的属性赋值
1.导入IOC容器需要的jar包
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
<!-- class指定类对象的全类名,交给服务器创建一个对象 id是一个唯一标识,在IOC只能出现一个id为book的bean对象 -->
<bean id="book" class="com.neuedu.spring.bean.Book">
<property name="bookName" value="JAVA"></property>
<property name="author" value="you"></property>
<property name="price" value="123.123"></property>
</bean>
public class TestIOC {
@Test
public void test() {
//1.获取IOC容器本身这个对象
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从IOC容器中获取bean对象
Object bean = ioc.getBean("book");
System.out.println(bean);
}
}
①IOC容器本身对象创建时,会将配置文件中配置好的bean先创建出来
②默认是单实例的,只创建bean的一个对象
③如果设置bean的scope属性为prototype,那么创建bean的对象就是多实例的,在获取的时候创建,每次获取对象都会创建新的
④.从IOC容器中获取对象
①根据bean的id获取
1.指的是在bean的初始化方法前后执行操作的专门的对象。
2.自定义的后置处理器:
1) 需要实现接口:org.springframework.beans.factory.config.BeanPostProcessor .
2) 做相应的配置就好!
public class TestIOC {
@Test
public void test() {
//1.获取IOC容器本身这个对象
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从IOC容器中获取bean对象
Book bean = ioc.getBean(Book.class);
System.out.println(bean.getBookName()+"---"+bean.getAuthor()+"---"+bean.getPrice());
}
}
要注意,创建实体类的时候要有无参构造器,没有无参的话会默认有一个无参构造器,但是这时要是有一个有参构造器,就构建不成对象,获取不到bean实例。
<bean id="book01" class="com.neuedu.spring.bean.Book">
<constructor-arg value="you" index="1"></constructor-arg>
<constructor-arg value="C++" index="0"></constructor-arg>
<constructor-arg value="22.22" index="2" type="double"></constructor-arg>
</bean>

public class StudentController {
private StudentService studentService;
public StudentService getStudentService() {
return studentService;
}
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
@Override
public String toString() {
return "StudentController [studentService=" + studentService + "]";
}
}
public class StudentService {
private StudentDao studentDao;
public StudentDao getStudentDao() {
return studentDao;
}
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public String toString() {
return "StudentService [studentDao=" + studentDao + "]";
}
}
public class StudentDao {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "StudentDao [username=" + username + "]";
}
}
<!-- 给bean的级联属性赋值 -->
<bean id="studentDao" class="com.neuedu.spring.bean.StudentDao"></bean>
<bean id="studentService" class="com.neuedu.spring.bean.StudentService"></bean>
<bean id="studentController" class="com.neuedu.spring.bean.StudentController">
<property name="studentService" ref="studentService"></property>
<property name="studentService.studentDao" ref="studentDao"></property>
<property name="studentService.studentDao.username" value="zhangsan"></property>
</bean>
public class TestIOC {
@Test
public void test() {
//1.获取IOC容器本身这个对象
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从IOC容器中获取bean对象
Object bean = ioc.getBean("studentController");
System.out.println(bean);
}
}
<!-- 通过p名称空间为bean赋值 -->
<bean id="student" class="com.neuedu.spring.bean.Student"
p:name="zhangsan"
p:gender="1"
p:address="China"
>
</bean>
<!-- 测试bean的作用域,分别创建单实例和多实例的bean -->
<bean id="book2" scope="singleton" class="com.neuedu.spring.bean.Book"></bean>
<bean id="student2" scope="prototype" class="com.neuedu.spring.bean.Student"></bean>
显示 一个book ,三个 student
public class TestIOC {
//1.获取IOC容器本身这个对象
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void test() {
ioc.getBean("book");
ioc.getBean("book");
ioc.getBean("book");
ioc.getBean("student");
ioc.getBean("student");
ioc.getBean("student");
}
}
<bean id="book" class="com.neuedu.spring.bean.Book"></bean>
<bean id="student" class="com.neuedu.spring.bean.Student"></bean>
public class TestIOC {
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void test() {
}
}
<bean id="teacher" class="com.neuedu.spring.bean.Teacher" init-method="teacherinit" destroy-method="destroy"></bean>
但是调用不了 destroy 方法,通过 ApplicationContext 的子类的 close 方法调用 destroy 方法
public class TestIOC {
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void test() {
ConfigurableApplicationContext cac = (ConfigurableApplicationContext) ioc;
ioc.getBean("teacher");
cac.close();
}
}
<bean id="book" class="com.neuedu.spring.bean.Book" depends-on="student"></bean>
<bean id="student" class="com.neuedu.spring.bean.Student" ></bean>
<bean id="teacher" class="com.neuedu.spring.bean.Teacher" init-method="teacherinit" destroy-method="destroy"></bean>
<bean id="book" class="com.neuedu.spring.bean.Book">
<property name="bookName" value="JAVA"></property>
<property name="author" value="you"></property>
<property name="price" value="43.43"></property>
<property name="isbn" value="home"></property>
</bean> <bean id="book1" class="com.neuedu.spring.bean.Book" parent="book">
<property name="price" value="34.23"></property>
<property name="isbn" value="house"></property>
</bean>
<bean id="book" class="com.neuedu.spring.bean.Book" abstract="true">
<property name="bookName" value="JAVA"></property>
<property name="author" value="you"></property>
<property name="price" value="43.43"></property>
<property name="isbn" value="home"></property>
</bean> <bean id="book1" parent="book">
<property name="price" value="43.43"></property>
<property name="isbn" value="home"></property>
</bean>
<bean id="book" class="com.neuedu.spring.bean.Book">
<property name="bookName" value="java"></property>
<property name="price" value="33.5"></property>
<property name="isbn" value="house"></property>
</bean>
也可以这么写,将<null/> 写进<property> 中
<bean id="book" class="com.neuedu.spring.bean.Book">
<property name="bookName" value="java"></property>
<property name="author">
<null/>
</property>
<property name="price" value="33.5"></property>
<property name="isbn" value="house"></property>
</bean>
<bean id="book" class="com.neuedu.spring.bean.Book">
<property name="bookName" value="java"></property>
<property name="isbn" value="house"></property>
</bean>
<bean id="bookShop" class="com.neuedu.spring.bean.BookShop">
<property name="address" value="河北"></property>
<property name="book" ref="book"></property>
</bean>
<bean id="bookShop" class="com.neuedu.spring.bean.BookShop">
<property name="address" value="河北"></property>
<property name="book">
<bean class="com.neuedu.spring.bean.Book">
<property name="bookName" value="java"></property>
<property name="author" value="you"></property>
<property name="price" value="43.32"></property>
<property name="isbn" value="house"></property>
</bean>
</property>
</bean>
<bean id="book1" class="com.neuedu.spring.bean.Book">
<property name="bookName" value="java"></property>
</bean>
<bean id="book2" class="com.neuedu.spring.bean.Book">
<property name="author" value="you"></property>
</bean>
<bean id="book3" class="com.neuedu.spring.bean.Book">
<property name="price" value="43.43"></property>
</bean>
<bean id="book4" class="com.neuedu.spring.bean.Book">
<property name="isbn" value="home"></property>
</bean>
<bean id="bookShop" class="com.neuedu.spring.bean.BookShop">
<property name="bookList">
<list>
<ref bean="book1"/>
<ref bean="book2"/>
<ref bean="book3"/>
<ref bean="book4"/>
</list>
</property>
</bean>
public class TestIOC {
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void test() {
BookShop bean = ioc.getBean(BookShop.class);
List<Book> bookList = bean.getBookList();
for(Book book : bookList){
System.out.println(book);
}
}
}

<bean id="bookShop" class="com.neuedu.spring.bean.BookShop">
<property name="bookMap">
<map>
<entry>
<key>
<value>book1</value>
</key>
<ref bean="book1"/>
</entry>
<entry>
<key>
<value>book2</value>
</key>
<ref bean="book2"/>
</entry>
<entry>
<key>
<value>book3</value>
</key>
<ref bean="book3"/>
</entry>
<entry>
<key>
<value>book4</value>
</key>
<ref bean="book4"/>
</entry>
</map>
</property>
</bean>
用 .entry 遍历 map 集合
public class TestIOC {
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void test() {
BookShop bean = ioc.getBean(BookShop.class);
Map<String, Book> bookMap = bean.getBookMap();
Set<Entry<String,Book>> entrySet = bookMap.entrySet();
for (Entry<String, Book> entry : entrySet) {
System.out.println(entry.getKey()+"==="+entry.getValue());
}
}
}
<bean id="bookShop" class="com.neuedu.spring.bean.BookShop">
<property name="p">
<props>
<prop key="book1">value1</prop>
<prop key="book2">value2</prop>
<prop key="book3">value3</prop>
<prop key="book4">value4</prop>
</props>
</property>
</bean>
public class TestIOC {
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void test() {
BookShop bean = ioc.getBean(BookShop.class);
java.util.Properties p = bean.getP();
System.out.println(p.get("book1"));
System.out.println(p.get("book2"));
System.out.println(p.get("book3"));
System.out.println(p.get("book4"));
}
}
Spring(一)--作用、IOC容器细节、搭配环境、Spring实验的更多相关文章
- Spring.NET的IoC容器(The IoC container)——简介(Introduction)
简介 这个章节介绍了Spring Framework的控制反转(Inversion of Control ,IoC)的实现原理. Spring.Core 程序集是Spring.NET的 IoC 容器实 ...
- Spring之一:IoC容器体系结构
温故而知心. Spring IoC概述 常说spring的控制反转(依赖反转),看看维基百科的解释: 如果合作对象的引用或依赖关系的管理要由具体对象来完成,会导致代码的高度耦合和可测试性降低,这对复杂 ...
- 比Spring简单的IoC容器
比Spring简单的IoC容器 Spring 虽然比起EJB轻量了许多,但是因为它需要兼容许多不同的类库,导致现在Spring还是相当的庞大的,动不动就上40MB的jar包, 而且想要理解Spring ...
- Ioc容器依赖注入-Spring 源码系列(2)
Ioc容器依赖注入-Spring 源码系列(2) 目录: Ioc容器beanDefinition-Spring 源码(1) Ioc容器依赖注入-Spring 源码(2) Ioc容器BeanPostPr ...
- 使用Spring.NET的IoC容器
使用Spring.NET的IoC容器 0. 辅助类库 using System; using System.Collections.Generic; using System.Linq; using ...
- 【Spring】Spring之向 IOC 容器注入对象的三种方式
关于Spring的搭建可参见:浅析Spring框架的搭建.在测试之前还是应该先将环境配置好,将相关Jar包导进来.Spring创建的对象,默认情况下都是单例模式,除非通过scope指定. 向IOC容器 ...
- Spring Framework之IoC容器
Spring IoC 概述 问题 1.什么是依赖倒置? 2.什么是控制反转? 3.什么是依赖注入? 4.它们之间的关系是怎样的? 5.优点有哪些? 依赖倒置原则 (Dependency Inversi ...
- Spring框架学习[IoC容器高级特性]
1.通过前面4篇文章对Spring IoC容器的源码分析,我们已经基本上了解了Spring IoC容器对Bean定义资源的定位.读入和解析过程,同时也清楚了当用户通过getBean方法向IoC容器获取 ...
- Spring框架及IOC容器
Spring是一个非常活跃的开源框架, 它是一个基于IOC和AOP来构架多层JavaEE系统的框架,它的主要目地是简化企业开发.Spring以一种非侵入式的方式来管理你的代码, Spring提倡”最少 ...
随机推荐
- Android 自定义 permission
Android 自定义 permission Android 添加自定义权限 permission-tree 权限的根节点,3个成员都要定义 name 一般来说需要2个".":比如 ...
- Longest Palindromic Substring - 字符串中最长的回文字段
需求:Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...
- Yii框架用ajax提交表单时候报错Bad Request (#400): Unable to verify your data submission.
提交表单报400错误,提示 "您提交的数据无法验证"原来是csrf验证的问题,因为表单是自己写的,在Yii框架中,为了防止csrf攻击,对post的表单数据封装了CSRF令牌验证. ...
- Spring-Framework 源码阅读之AnnotationBeanUtils
Java程序员,就是要学会一个名字叫做"春"的东西,这玩意运用的非常的广泛,现在如果你的业务系统或者软件没有在这个东西上开发,都不要意思拿出来.因为你更不上时代了.在平时的工作的中 ...
- MySql 求一段时间范围内的每一天,每一小时,每一分钟
平常经常会求一段时间内的每一天统计数据,或者每一时点的统计数据.但是mysql本身是没有直接获取时点列表的函数或表.下面是自己用到的一些方法,利用临时变量和一个已存在的比较多数据(这个需要根据实际情况 ...
- 64位linux系统通过编译安装apache+…
二.安装php 上传php压缩包 例如:php-5.2.3.tar.gz 移动 mv php-5.2.3.tar.gz /usr/local/src 进入 cd /usr/local/src 解压 t ...
- 在 overlay 中运行容器 - 每天5分钟玩转 Docker 容器技术(51)
上一节我们创建了 overlay 网络 ov_net1,今天将运行一个 busybox 容器并连接到 ov_net1: 查看容器的网络配置: bbox1 有两个网络接口 eth0 和 eth1.eth ...
- ReactiveSwift源码解析(十) Lifetime代码实现
为了之后博客的进行,本篇博客我们就来聊一下ReactiveSwift框架中的Lifetime类的具体实现.从Lifetime这个名字中我们就这道,就是生命周期.在ReactiveSwift中使用Lif ...
- 字符串匹配之KMP,C++实现
文字介绍KMP我就不讲了,相信大家看了不少别的文章也没看懂,我肯定也不能用文字表达清楚. 最好的办法就是看严老师的视频,讲的很清晰. 请百度 KMP 严蔚敏: 在这里我用C++实现一下: #inclu ...
- 从Javascript单线程谈Event Loop
假如面试回答js的运行机制时,你可能说出这么一段话:"Javascript的事件分同步任务和异步任务,遇到同步任务就放在执行栈中执行,而碰到异步任务就放到任务队列之中,等到执行栈执行完毕之后 ...