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提倡”最少 ...
随机推荐
- language-detection 语言检测工具 java版的应用demo
本文基本借鉴大佬文章:http://www.cnblogs.com/makemelaugh/archive/2012/09/26/2704802.html 在此基础上添加一些自己的补充,方便查阅. 提 ...
- 【Java IO流】File类的使用
File类的使用 Java中的File类是在java.io.File中,Java.IO.File类表示文件或目录. File类只用于表示文件(目录)的信息(名称.大小等),不能用于文件内容的访问. 一 ...
- anaconda 下多版本Python 安装说明
网上针对多版本的Python兼容安装的文章逐渐增多,都是大家在实践中总结的经验.本人的安装经过几次的反复实验还是觉得其中一种更为方便. 有人的安装方法是: 1. 先安装一个版本的python(一般先安 ...
- python爬虫从入门到放弃(二)之爬虫的原理
在上文中我们说了:爬虫就是请求网站并提取数据的自动化程序.其中请求,提取,自动化是爬虫的关键!下面我们分析爬虫的基本流程 爬虫的基本流程 发起请求通过HTTP库向目标站点发起请求,也就是发送一个Req ...
- HDOJ2003-求绝对值
Problem Description 求实数的绝对值. Input 输入数据有多组,每组占一行,每行包含一个实数. Output 对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果 ...
- JStorm与Storm源码分析(一)--nimbus-data
Nimbus里定义了一些共享数据结构,比如nimbus-data. nimbus-data结构里定义了很多公用的数据,请看下面代码: (defn nimbus-data [conf inimbus] ...
- Nlpir Parser敏感词搜索灵玖语义技术应用
近年来随着网络技术的飞速发展和用户的剧烈增长,网络传输数据量越来越大,网络用语越来越趋于多样化.如何快速的屏蔽用户的不当言论.过滤用户发表内容中的非法词汇已成为关键词匹配领域的一项重大难题. 目前主要 ...
- Java基础之String类
String类 字符串是不可变的,对其做的任何改变,会生成一个对象,不会改变有原有对象. ==和equals() String s1 = "good"; String s2 = & ...
- 【万能的搜索,用广搜来解决DP问题】ZZNU -2046 : 生化危机 / HDU 1260:Tickets
2046 : 生化危机 时间限制:1 Sec内存限制:128 MiB提交:19答案正确:8 题目描述 当致命的T病毒从Umbrella Corporation 逃出的时候,地球上大部分的人都死去了. ...
- jQuery 去空
//去左右空格; function trim(s){ return s.replace(/(^\s*)|(\s*$)/g, ""); } //去掉字符串中所有空格(包括中间 ...