1.术语了解

1.1组件/框架设计
 侵入式设计

引入了框架,对现有的类的结构有影响;即需要实现或继承某些特定类。
例如: Struts框架
非侵入式设计
引入了框架,对现有的类结构没有影响。
例如:Hibernate框架 / Spring框架
控制反转:
Inversion on Control , 控制反转 IOC
对象的创建交给外部容器完成,这个就做控制反转.

依赖注入:

        dependency injection

处理对象的依赖关系

区别:
         控制反转, 解决对象创建的问题 【对象创建交给别人】
依赖注入,在创建完对象后, 对象的关系的处理就是依赖注入 【通过set方法依赖注入】
AOP
面向切面编程。切面,简单来说来可以理解为一个类,由很多重复代码形成的类。

切面举例:事务、日志、权限;

1.2 Spring框架

Spring框架,可以解决对象创建以及对象之间依赖关系的一种框架。
且可以和其他框架一起使用;Spring与Struts,  Spring与hibernate
(起到整合(粘合)作用的一个框架)
Spring提供了一站式解决方案:(Spring六大模块)
1) Spring Core  spring的核心功能: IOC容器, 解决对象创建及依赖关系
2) Spring Web  Spring对web模块的支持。
- 可以与struts整合,让struts的action创建交给spring
    - spring mvc模式
3) Spring DAO  Spring 对jdbc操作的支持  【JdbcTemplate模板工具类】
4) Spring ORM  spring对orm(对象关系映射)的支持: 
 既可以与hibernate整合,【session】
 也可以使用spring的对hibernate操作的封装
5)Spring AOP  切面编程
6)SpringEE   spring 对javaEE其他模块的支持
spring各个版本中:
在3.0以下的版本,源码有spring中相关的所有包【spring功能 + 依赖包】
如2.5版本;
在3.0以上的版本,源码中只有spring的核心功能包【没有依赖包】
(如果要用依赖包,需要单独下载!)

1) 源码, jar文件:spring-framework-3.2.5.RELEASE
commons-logging-1.1.3.jar           日志
spring-beans-3.2.5.RELEASE.jar        bean节点
spring-context-3.2.5.RELEASE.jar       spring上下文节点
spring-core-3.2.5.RELEASE.jar         spring核心功能
spring-expression-3.2.5.RELEASE.jar    spring表达式相关表

以上是必须引入的5个jar文件,在项目中可以用户库管理!

2) 核心配置文件: applicationContext.xml

Spring配置文件:applicationContext.xml / bean.xml

2.开发步骤

首先新建项目,引入spring的架包,
项目包下新建xml文件,一般命名为applicationContext.xml或者bean.xml,这里以bean.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: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">
</beans>

Spring的一个特点就是将对象的创建写在配置文件中(即bean.xml中),在创建对象时只需要引用即可,这样的
好处就是增加了开发的灵活性

例子1: 使用配置文件的方法创建对象

实体类User

public class User {
private int id;
private String name; /////////通过容器注入属性值
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
} /////////
public int getId() {
return id;
}
public String getName() {
return name;
} public User() {
System.out.println("无参构造器");
} public User(int id, String name) {
super();
System.out.println("有参构造器");
this.id = id;
this.name = name;
} @Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
} public void init_user() {
System.out.println("init");
}
public void destroy_user() {
System.out.println("destroy");
} }

bean.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: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"> <!--
各属性对应的含义 可以使用不同的属性实验效果
* (1)对象创建:单例/多例
* scope="singleton",默认值,默认单例 【service/dao】
* scope="prototype" 多例 用于action
*
* (2)什么时候创建?
* scope="singleton"单例 在启动(容器初始化前)就创建对象,只创建这一次
* scope="prototype" 多例 在容器创建完后在使用到对象时才创建对象
*
* (3)是否延迟创建(只对单例模式有效)
* lazy-init="false" 默认为false,不延迟创建,即在启动时就创建对象
* lazy-init="true" 延迟初始化,在用到对象时才创建对象
*
* (4)创建对象后,初始化/销毁
* init-method="init_user" 【对应对象的init_user方法,在对象创建之后执行】
* destroy-method="destroy_user" 【在调用容器对象的destroy后执行】
*
*/ -->
<bean id = "user" class="com.th.test1.User" scope="singleton"
lazy-init="true" init-method="init_user" destroy-method="destroy_user"></bean> </beans>

调用实例化实体类

public class App2Bean {

//得到IOC容器对象
private ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("com/th/test1/bean.xml"); @Test
public void testIOC() throws Exception {
System.out.println("容器创建");
//从容器中获取bean
User user1 = (User) ac.getBean("user"); //此处user就是在bean.xml中配置的id为user的对象
User user2 = (User) ac.getBean("user"); System.out.println(user1);
System.out.println(user2); //销毁容器
ac.destroy();
}
}

例子2:创建对象的几种方式
   1.调用无参构造器
   2.带参构造器
   3.工厂创建对象
         工厂类,静态方法创建对象
         工厂类,非静态方法创建对象

工厂类:

public class ObjectFactory {
//实例方法创建对象
public User getInstance(){
return new User(100,"调用实例方法");
} //静态方法创建对象
public static User getStaticInstance(){
return new User(100,"调用静态实例方法");
}
}

bean.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: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"> <!-- 1.默认无参构造器-->
<bean id = "user1" class="com.th.create_obj.User" ></bean> <!-- 2.带参构造起 type 基本类型直接写,引用类型写全路径-->
<bean id = "user2" class="com.th.create_obj.User" >
<constructor-arg value="100" index="0" type="int"></constructor-arg>
<constructor-arg value="jack" index="1" type="java.lang.String"></constructor-arg>
</bean> <!-- 3.定义一个字符串,值是jack String s = new String("jack") -->
<bean id="str" class="java.lang.String">
<constructor-arg value="jacks"></constructor-arg>
</bean>
<bean id = "user3" class="com.th.create_obj.User" >
<constructor-arg value="100" index="0" type="int"></constructor-arg>
<constructor-arg ref="str" index="1" type="java.lang.String"></constructor-arg>
</bean> <!-- 4.工厂类创建对象 -->
<!-- 先创建工厂 -->
<bean id="factory" class="com.th.create_obj.ObjectFactory"></bean>
<!-- 在创建user对象,用factory方的实例方法 -->
<bean id="user4" factory-bean="factory" factory-method="getInstance"></bean> <!-- 5.工厂类:静态方法 -->
<bean id="user5" class="com.th.create_obj.ObjectFactory" factory-method="getStaticInstance"></bean>
</beans>

对象依赖关系
Spring中,如何给对象的属性赋值
1.通过构造函数
2.通过set方法给属性注入值
3.p名称空间
三层构造Dao-->Service-->Action

UserDao代码:

public class UserDao {
public void save() {
System.out.println("DB:保存用户");
}
}

UserService代码:

public class UserService {
private UserDao userDao;
//IOC:对象的创建交给Spring的外部容器完成
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} public void save() {
userDao.save();
}
}

UserAction代码

public class UserAction {

	private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
} public String execute() {
userService.save();
return null;
}
}

bean.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: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"> <!-- 对象属性赋值-->
<!-- (1)通过构造函数 -->
<bean id="user" class="com.th.c_property.User">
<constructor-arg value="100"></constructor-arg>
<constructor-arg value="Tom"></constructor-arg>
</bean> <!-- 通过set方法给属性注入值 -->
<bean id="user1" class="com.th.c_property.User">
<property name="id" value="101"></property>
<property name="name" value="jack"></property>
</bean> <!-- 案例 action/service/dao -->
<!-- dao instance实例化 -->
<bean id="userDao" class="com.th.c_property.UserDao"></bean>
<!-- service instance实例化 依赖dao层-->
<bean id="userService" class="com.th.c_property.UserService">
<property name="userDao" ref="userDao"></property>
</bean> <!--action instance 依赖service层-->
<bean id="userAction" class="com.th.c_property.UserAction">
<property name="userService" ref="userService"></property>
</bean> </beans>

P名称空间实现对象依赖

bean.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: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"> <!-- 给对象属性注入值
P名称空间给属性注入值,在3.0以上版本才能使用
p:userDao-ref="userDao" 引用userDao
-->
<bean id="userDao" class="com.th.c_property.UserDao"></bean>
<bean id="userService" class="com.th.c_property.UserService" p:userDao-ref="userDao"></bean>
<bean id="userAction" class="com.th.c_property.UserAction" p:userService-ref="userService"></bean> <!-- set注入改为p名称注入
<bean id="user1" class="com.th.c_property.User">
<property name="id" value="101"></property>
<property name="name" value="jack"></property>
</bean>
-->
<bean id="user" class="com.th.c_property.User" p:name="jack"></bean> </beans>

自动装配:
bean.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: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 <!-- 自动装配也可以定义到全局, 此处添加default-autowire="byName" 则下边bean节点中配置的autowire="byName可省去-->
http://www.springframework.org/schema/context/spring-context.xsd" default-autowire="byName">
<!--
自动装配其是指还是调用UserDao和UserService中的set方法进行设置值,不过不需要我们手动配置
autowire="byName" 根据名字自动装配,自动在ioc容器中寻找名字
autowire="byType" 根据类型自动装配:必须确保容器中只有一个该类型的对象,否则报错 -->
<!-- 自动装配 -->
<bean id="userDao" class="com.th.d_auto.UserDao"></bean>
<bean id="userService" class="com.th.d_auto.UserService" autowire="byName"></bean> <!-- 根据名称自动装配:userAction注入的属性 ,会去IOC容器中自动查找与属性同名的对象-->
<bean id="userAction" class="com.th.d_auto.UserAction" autowire="byName"></bean> </beans>

注解: 
注解方式可以简化spring的IOC容器的配置!
使用注解步骤:
1)先在bean.xml中引入context名称空间
xmlns:context="http://www.springframework.org/schema/context"
2)开启注解扫描
<context:component-scan base-package="cn.itcast.e_anno2"></context:component-scan>
3)使用注解
通过注解的方式,把对象加入ioc容器。
   创建对象以及处理对象依赖关系,相关的注解:
@Component   指定把一个对象加入IOC容器
@Repository   作用同@Component; 在持久层使用
@Service      作用同@Component; 在业务逻辑层使用
@Controller    作用同@Component; 在控制层使用 
@Resource     属性注入
总结:
1) 使用注解,可以简化配置,且可以把对象加入IOC容器,及处理依赖关系(DI)
2) 注解可以和XML配置一起使用。


spring之IOC容器创建对象的更多相关文章

  1. Spring的核心之IoC容器创建对象

    Spring的Ioc容器,是Spring的核心内容: 作用:对象的创建和处理对象的依赖关系. Spring容器创建对象有以下几种方式: 1:调用无参数的构造器 <!-- 默认无参的构造器 --& ...

  2. Spring框架IOC容器和AOP解析

    主要分析点: 一.Spring开源框架的简介  二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面向切面编程(AOP)和事务管理配置  一.S ...

  3. spring框架--IOC容器,依赖注入

    思考: 1. 对象创建创建能否写死? 2. 对象创建细节 对象数量 action  多个   [维护成员变量] service 一个   [不需要维护公共变量] dao     一个   [不需要维护 ...

  4. Spring的IoC容器

    Spring是一个轻量级的Java开发框架,其提供的两大基础功能为IoC和AOP,其中IoC为依赖反转(Inversion of Control).IOC容器的基本理念就是"为别人服务&qu ...

  5. Spring的IOC容器第一辑

    一.Spring的IOC容器概述 Spring的IOC的过程也被称为依赖注入(DI),那么对象可以通过构造函数参数,工厂方法的参数或在工厂方法构造或返回的对象实例上设置的属性来定义它们的依赖关系,然后 ...

  6. Spring框架IOC容器和AOP解析 非常 有用

    Spring框架IOC容器和AOP解析   主要分析点: 一.Spring开源框架的简介  二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面 ...

  7. 好莱坞原则—Spring的IOC容器

    IOC容器的概念,之前在学习SSH的时候,就有接触过.但那时候也仅仅是知道这么个概念,认为它非常难理解.事实上并非它难理解,而是我并没有停下来好好对它总结梳理过. IOC(Inversion of C ...

  8. Spring之IOC容器加载初始化的方式

    引言 我们知道IOC容器时Spring的核心,可是如果我们要依赖IOC容器对我们的Bean进行管理,那么我们就需要告诉IOC容易他需要管理哪些Bean而且这些Bean有什么要求,这些工作就是通过通过配 ...

  9. 在Servlet(或者Filter,或者Listener)中使用spring的IOC容器

    web.xml中的加载顺序为:listener > filter > servlet > spring. 其中filter的执行顺序是filter-mapping在web.xml中出 ...

随机推荐

  1. UML系列图--用例图(转)

    UML-Unified Model Language 统一建模语言,又称标准建模语言.是用来对软件密集系统进行可视化建模的一种语言. 在UML系统开发中有三个主要的模型:  功能模型: 从用户的角度展 ...

  2. Data_Structure02-线性表

    一.PTA实验作业 本周要求挑3道题目写设计思路.调试过程.设计思路用伪代码描述. 1.顺序表选择一题(6-2,6-3,7-1选一题),代码必须用顺序结构抽象数据类型封装 2.单链表选择一题(6-1不 ...

  3. python学习(二十二) String(上)

    str1 = "This is a 'test'" print(str1) str1 = 'This is a "test"' print(str1) str1 ...

  4. 【洛谷】P2694 接金币(排序)

    题目描述 在二维坐标系里,有N个金币,编号0至N-1.初始时,第i个金币的坐标是(Xi,Yi).所有的金币每秒向下垂直下降一个单位高度,例如有个金币当前坐标是(xf, yf),那么t秒后金币所在的位置 ...

  5. C和指针 第三章--数据

    简要概述: <C和指针>第三章对数据进行了描述. 其中主要讲解了---变量的三个属性:作用域.链接属性和存储类型. 这三个属性决定了该变量在“什么地方可以使用”以及“该变量的值能够保持多久 ...

  6. quartz报错 Couldn't retrieve job because the BLOB couldn't be deserialized: null

    今天线上添加定时任务之后 定时任务查询页面报出如上错误, 原因有两点 1.org.quartz.jobStore.useProperties = true 这个属性的意思存储的JobDataMaps是 ...

  7. 转 maven jetty 插件

    maven jetty 插件使用 本机环境 JDK 7 Maven 3.2 Jetty 9.2 Eclipse Luna pom.xml 配置 在你的 pom.xml 文件中添加 jetty 插件的描 ...

  8. 比XGBOOST更快--LightGBM介绍

    xgboost的出现,让数据民工们告别了传统的机器学习算法们:RF.GBM.SVM.LASSO.........现在,微软推出了一个新的boosting框架,想要挑战xgboost的江湖地位.笔者尝试 ...

  9. 实验楼HTML基础入门学习

    HTML基本介绍 HTML,一种描述网页的语言 结构 html head title script body ... 文档 <html> <head> <title> ...

  10. C#中的数据格式转换 (未完待更新)

    一.string to int int intA = 0;1.intA =int.Parse(str);2.int.TryParse(str, out intA);3.intA = Convert.T ...