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中.我们在实际的编码过程中也应该借鉴这种思想,来提高我们程序的效率. 首先我们看一 ...
随机推荐
- Struts2漏洞拉响网站安全红色警报以及把Struts2更新为最新版本Struts2.3.15.1步骤
360网站安全检测平台今日发布红色警报称,广泛应用在国内大型网站系统的Struts2框架正在遭到黑客猛烈攻击.利用Struts2“命令执行漏洞”,黑客可轻易获得网站服务器ROOT权限.执行任意命令,从 ...
- EasyUI使用技巧总结
combobox组件 一.禁用combobox里面的输入框 $("选择器").combo('textbox').attr("readonly", "r ...
- 浅谈压缩感知(十七):测量矩阵之有限等距常数RIC的计算
有限等距常数(RestrictedIsometry Constant, RIC)是与有限等距性质(Restricted IsometryProperty, RIP)紧密结合在一起的一个参数. 一.RI ...
- wifipineapple执行dnsspoof
ssh连接到wifipineapple: 输入连接信息:ssh root@172.16.42.1 输入密码:pineapplesareyummy 安装依赖基本环境: opkg update opkg ...
- NPM慢怎么办 - nrm切换资源镜像
1. 直接配置为taobao镜像 npm config set registry https://registry.npm.taobao.org 1. 使用NRM管理镜像 npm isntall -g ...
- MySQL 5.6新特性 -- Multi-Range Read
如果基表很大,数据没有被缓存,在二级索引上使用范围扫描读取行可能会导致大量的随机磁盘访问.使用Multi-Range Read新特性,mysql可以减少对磁盘的随机读的次数:首先,mysql只是扫描索 ...
- oracle登陆提示“ora-01031 insufficient privileges”
本机装了服务端的oracle11.2g,一直没用,中间换过系统的登陆用户.今早发现登陆数据库时发现老提示“ora-01031 insufficient privileges”,以为监听没配置好,试过了 ...
- IOS项目目录结构和开发流程
网上相关的资源不多,开源的且质量还不错的iOS项目也是少之又少,最近正好跟同事合作了一个iOS项目,来说说自己的一些想法. 目录结构 AppDelegate Models Macro Genera ...
- MySql事务select for update及数据的一致性处理讲解
MySQL中的事务,默认是自动提交的,即autocommit = 1: 但是这样的话,在某些情形中就会出现问题:比如: 如果你想一次性插入了1000条数据,mysql会commit1000次的, 如果 ...
- fiddler4 使用教程
Fiddler是最强大最好用的Web调试工具之一,它能记录所有客户端和服务器的http和https请求,允许你监视,设置断点,甚至修改输入输出数据,Fiddler包含了一个强大的基于事件脚本的子系统, ...