Spring学习笔记--Spring IOC
沿着我们上一篇的学习笔记,我们继续通过代码学习IOC这一设计思想.
6.Hello类
第一步:首先创建一个类Hello
package cn.sxt.bean;
public class Hello {
private String name;
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("hello,"+name);
}
}
第二步:创建配置文件beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- bean就是java对象,由spring容器来创建和管理 -->
<bean name="hello" class="cn.sxt.bean.Hello">
<property name="name" value="张三"></property>
</bean>
</beans>
第三步:编写测试类Test
package cn.sxt.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.sxt.bean.Hello; public class Test {
public static void main(String[] args) {
//解析beans.xml文件生成管理相应的bean对象
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
Hello hello=(Hello)context.getBean("hello");
hello.show();
}
}
我们需要导入相关Jar包(在上一篇笔记Spring主要内容中显示的那些核心jar包)

此时运行Test程序,会触发异常:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
我们需要添加commons-logging.jar文件

步骤总结:
1)导入相关jar包
2)编写spring配置文件(名称可以自定义)
思考?
Hello对象是谁创建的?
我们在Hello类中添加一个构造函数,可以确定Hello对象确定被创建:
package cn.sxt.bean;
public class Hello {
public Hello() {
System.out.println("hello 被创建");
}
private String name;
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("hello,"+name);
}
}
运行Test测试类结果显示:
hello 被创建
hello,张三
由此可以得知,Hello对象是由spring容器来创建的:bean工厂,可以包含多个bean,创建不同类的对象
<bean name="hello" class="cn.sxt.bean.Hello">
<property name="name" value="张三"></property>
</bean>
Hello对象的属性是怎样设置的?
Hello对象的属性是由spring容器来设置的;
这个过程就叫做控制反转:
控制的内容:指的是谁来控制对象的创建;传统的应用程序,对象的创建是由程序本身来控制,使用Spring以后是由spring来创建对象的。
反转:有反转就有正转,正转指程序来创建对象,反转指程序本身不去创建对象,而变为被动的接收容器给我们创建的对象
总结:以前对象是由程序本身来创建,使用spring后,程序变为了被动接收spring创建好的对象;
控制反转有一个别名--依赖注入(DI-dependency injection)
DI:比如在我们的Hello类中,我们的类Hello就依赖于name属性,以来的这个name属性是由spring容器来设置的,name值的设置过程就叫做依赖注入(通过setName方法进行的依赖注入)
Ioc--是一种编程思想,由主动编程变为别动接收;
Ioc的实现是通过Ioc容器(Bean工厂)来实现的。Ioc容器--BeanFactory
在第一篇学习笔记中的UserDao和UserDaoService的例子,我们在这里就可以使用spring配置文件的方式来管理对象的生命周期以及依赖对象的注入;
beanx.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- bean就是java对象,由spring容器来创建和管理 -->
<bean id="mysqlDao" class="cn.sxt.dao.impl.UserDaoMySqlImpl"></bean>
<bean id="oracleDao" class="cn.sxt.dao.impl.UserDaoOracleImpl"></bean>
<bean id="service" class="cn.sxt.service.impl.UserServiceImpl">
<!-- ref引用对象(对象是由spring来创建的) -->
<property name="userDao" ref="mysqlDao"></property>
</bean>
<!-- property如何设置:name="setUserDao(去除set,并将剩余的UserDao首字母小写)" -->
</beans>
当我们需要替换具体的实现时,就可以直接在配置文件中进行修改,例如将ref="mysqlDao"修改为ref="oracleDao";
在测试类中我们就可以这样来组织代码:
package cn.sxt.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.sxt.service.UserService; public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
UserService us=(UserService)ac.getBean("service");
us.getUser();
}
}
使用IOC来创建对象的方式:3种方式
1)通过无参的构造方法来创建;
User.java:
package cn.sxt.vo;
public class User {
public User(){
System.out.println("user的无参构造方法");
}
private String name;
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("name="+name);
}
}
beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="cn.sxt.vo.User">
<property name="name" value="张三"></property>
</bean>
</beans>
Test:
package cn.sxt.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.sxt.vo.User; public class Test { public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
User user=(User)ac.getBean("user");
user.show();
} }
2)通过有参构造方法来创建;
User.java:
package cn.sxt.vo;
public class User {
private String name;
public User(String name) {
super();
this.name = name;
}
public void show(){
System.out.println("name="+name);
}
}
beans.xml配置(有三种情况):
第一种:根据参数的下标(index)来设置;
<?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="user" class="cn.sxt.vo.User">
<!-- index指的是构造方法参数下标,从0开始 -->
<constructor-arg index="0" value="李四"></constructor-arg>
</bean>
</beans>
第二种:根据参数名称(name)来设置;
<bean id="user" class="cn.sxt.vo.User">
<!-- name指的是属性值 -->
<constructor-arg name="name" value="王五"></constructor-arg>
</bean>
第三种:根据参数类型(type)来设置;
<bean id="user" class="cn.sxt.vo.User">
<constructor-arg type="java.lang.String" value="徐六"></constructor-arg>
</bean>
3)通过工厂方法来创建对象(有两种);
第一种:静态工厂来创建;
UserFactory.java:
package cn.sxt.factory;
import cn.sxt.vo.User;
public class UserFactory {
public static User newInstance(String name){
return new User(name);
}
}
beans.xml配置:
<bean id="user" class="cn.sxt.factory.UserFactory" factory-method="newInstance">
<constructor-arg index="0" value="任七"></constructor-arg>
</bean>
第二种:动态工厂来创建
UserDynamicFacory.java:
package cn.sxt.factory;
import cn.sxt.vo.User;
public class UserDynamicFactory {
public User newInstance(String name){
return new User(name);
}
}
beans.xml:
<bean id="userFacotry" class="cn.sxt.factory.UserDynamicFactory"/>
<bean id="user" factory-bean="userFacotry" factory-method="newInstance">
<constructor-arg index="0" value="王五"/>
</bean>
Spring学习笔记--Spring IOC的更多相关文章
- Spring学习笔记--spring+mybatis集成
前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...
- Spring学习笔记--Spring配置文件和依赖注入
Spring配置文件 1.alias:设置别名,为bean设置别名,并且可以设置多个别名; <!-- 设置别名 --> <alias name="user" al ...
- Spring学习笔记—Spring之旅
1.Spring简介 Spring是一个开源框架,最早由Rod Johnson创建,并在<Expert One-on-One:J2EE Design and Development> ...
- Spring学习笔记1——IOC: 尽量使用注解以及java代码(转)
在实战中学习Spring,本系列的最终目的是完成一个实现用户注册登录功能的项目. 预想的基本流程如下: 1.用户网站注册,填写用户名.密码.email.手机号信息,后台存入数据库后返回ok.(学习IO ...
- Spring学习笔记1——IOC: 尽量使用注解以及java代码
在实战中学习Spring,本系列的最终目的是完成一个实现用户注册登录功能的项目. 预想的基本流程如下: 1.用户网站注册,填写用户名.密码.email.手机号信息,后台存入数据库后返回ok.(学习IO ...
- spring学习笔记之---IOC和DI
IOC和DI (一)IOC (1) 概念 IOC (Inverse of Control) 反转控制,就是将原本在程序中手动创建对象的控制权,交给spring框架管理.简单的说,就是创建对象控制权被反 ...
- Spring学习笔记——Spring中的BeanFactory与FactoryBean
BeanFactory BeanFactory是Spring的org.springframework.beans.factory下的一个接口,是Spring IOC所遵守的基本编程规范.他的实现类有D ...
- Spring学习笔记--Spring简介
1.spring:给软件行业带来了春天; 2.spring的理念:spring框架的初衷是使的现有的更加实用,spring不是创造轮子(技术或框架),而是使现有的轮子更好的运转;spring本身是一个 ...
- Spring学习笔记——Spring中lazy-init与abstract具体解释
Spring的懒载入的作用是为了避免无谓的性能开销,就是当真正须要数据的时候才去运行数据的载入操作.不只在Spring中.我们在实际的编码过程中也应该借鉴这种思想,来提高我们程序的效率. 首先我们看一 ...
随机推荐
- iframe实现Ajax文件上传效果示例
<!doctype html> <html> <head> <meta charset=utf-8> <head> <title> ...
- 理解 .NET 2015
去年跟着BUILD之后,我发了一篇文章Exciting Times for .NET 并从那以后我已经很荣幸地能够与.NET团队并肩作战,这其中包括了运行时.框架.语言和编译器.虽然去年我的重心已经更 ...
- AutoMapperExtension
using System; using System.Collections.Generic; using System.Linq; using AutoMapper; using System.Co ...
- SCWS 中文分词_测试成功
地址: http://www.xunsearch.com/scws/index.php
- 分析各种Android设备屏幕分辨率与适配 - 使用大量真实安卓设备采集真实数据统计
一. 数据采集 源码GitHub地址 : -- SSH : git@github.com:han1202012/DisplayTest.git; -- HTTP : https://github.co ...
- Eclipse环境安装Python插件PyDev
转载自:http://blog.csdn.net/typa01_kk/article/details/49251247 clipse环境安装Python插件PyDev 软件准备,下载地址,先看安装,再 ...
- MySQL累积求和
有如下表 id money 1 10 2 20 3 30 4 40 求出如下数据 id money cum 1 10 10 2 20 30 3 30 60 4 40 100 建表 CREA ...
- linux每日命令(32):gzip命令
减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间.gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用.gzip不仅可以用 ...
- GNU make使用(二)
[时间:2017-06] [状态:Open] [关键词:makefile,gcc,编译,shell命令,目标文件] 0 引言及目标 之前使用Makefile都是把源文件和目标文件放到同一个目录编译.近 ...
- 【转】css3实现文字闪烁,改变透明度
<style> @-webkit-keyframes shake{ 0%{ opacity: 1; } 50%{ opacity: 0.5; } 100%{ opacity: 1; } } ...