1 实例化Spring容器

新建springday01项目
1.F盘jar/Spring/first/五个jar包拷贝到lib下,复制xml文件到项目first包下
2.First.java测试如何启动容器

First.java:

public static void main(String[] args) {
//因为配置文件放在src下的first包下(文件路径可以随便,但是代码中也要改变)
String cfg = "first/applicationContext.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg);
System.out.println(ctx);
}

注:参考jar文件和xml文件在F:\Code\jar\Spring\first文件夹下。

2 使用Spring容器创建bean

1.复制xml文件到container/instance下,分别使用构造器(重点掌握)、静态工厂(了解)、实例工厂(了解)实例化bean
2.ExampleBean必须实现序列化接口,以及无参构造器
3.分别进行测试

代码如下:

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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- id要唯一,类名要写完整 -->
<bean id="eb1" class="container.instance.ExampleBean"/>
<!-- 虽然这个类我们自己没写,但是它一定有一个无参构造器 -->
<bean id="cal1" class="java.util.GregorianCalendar"/>
<!-- 此方法做了解,Calendar是一个抽象类,它没有无参构造器,第三句代表调用容器的静态方法 -->
<!-- 静态工厂方式创建一个对象,其中,factory-method属性用来指定静态方法名 -->
<bean id="cal2" class="java.util.Calendar"
factory-method="getInstance"/>
<!-- 使用实例工厂方式创建一个对象,第二个参数为别的bean的id,第三个参数表示
调用第二个bean的getTime方法 -->
<bean id="date1" factory-bean="cal1"
factory-method="getTime"/>
</beans>

ExampleBean.class:

//演示容器如何创建序列化对象
public class ExampleBean implements Serializable {

public ExampleBean(){
System.out.println("ExampleBean的无参构造器");
}
}

TestCase.class:

package container.instance;

import java.util.Calendar;
import java.util.Date;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/*测试类*/
public class TestCase {

@Test
public void test1(){
//启动容器
String cfg = "container/instance/applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(cfg);
//让容器创建一个对象,如果不写第二个参数,则返回Object,所以,我们加上第二个参数,即这个
//Bean所属的类
ExampleBean eb = ac.getBean("eb1",ExampleBean.class);
System.out.println(eb);

Calendar cal1 = ac.getBean("cal1", Calendar.class);
System.out.println(cal1);

Calendar cal2 = ac.getBean("cal2",Calendar.class);
System.out.println(cal2);

Date date1 = ac.getBean("date1",Date.class);
System.out.println(date1);
 }
}

运行结果:

3 bean的作用域

1.复制xml文件到container/other下,使用构造器实例化bean
2.MessageBean实现序列化接口,无参构造器,初始化函数,销毁函数,调用函数
3.test1:
测试验证容器创建的bean默认是单例,即不管实例化多少次,都是同一个,但若xml中scope为prototype,则为多例
test2:
xml中配置init-method,创建对象时,会调用构造函数以及初始化函数
配置destroy-method,close容器时,会调用销毁函数,容器关闭只在单例singleton模式下进行
test3:
测试即使不创建bean,容器也会实现预创建好,如果想不提前创建bean对象,则lazy-init=“true”延迟实例化
4.stu包为类似的作业,练习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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- prototype:原型,其作用是让容器创建多个实例,
而singleton只创建一个实例 scope="prototype"-->

<bean id="mb1" class="container.other.MessageBean"
/>

<!-- init-method属性:指定初始化方法,注意,销毁方法只针对单例,此处若
scope为原型,则销毁方法不执行 -->

<bean id="mb2"
class="container.other.MessageBean"
init-method="init"
destroy-method="destroy"
/>

<!--lazy-init属性如果为true,表示延迟实例化 lazy-init="true"-->
<bean id="mb3"
class="container.other.MessageBean"
init-method="init"
destroy-method="destroy"
/>

</beans>

MessageBean.class代码:

package container.other;

import java.io.Serializable;

public class MessageBean implements Serializable{

public MessageBean(){
System.out.println("MessageBean的无参构造器");
}
//初始化方法
public void init(){
System.out.println("初始化...");
}

public void sendMsg(){
System.out.println("发送消息...");
}
//销毁方法
public void destroy(){
System.out.println("销毁资源");
}
}

TestCase代码:

package container.other;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestCase {

@Test
public void test1(){
String cfg = "container/other/applicationContext.xml";
ApplicationContext ac =
new ClassPathXmlApplicationContext(cfg);
MessageBean mb1 = ac.getBean("mb1",MessageBean.class);
MessageBean mb2 = ac.getBean("mb1",MessageBean.class);
System.out.println(mb1==mb2);
//true 说明多次实例化,但实际值实例话一次,修改scope为prototype,则
//每getBean一次,则实例化一次,所以为false
}

@Test
public void test2(){
String cfg = "container/other/applicationContext.xml";
//ApplicationContext ac =
//关闭容易应该使用AbstractApplicationContext
AbstractApplicationContext ac =
new ClassPathXmlApplicationContext(cfg);
MessageBean mb2 = ac.getBean("mb2",MessageBean.class);
mb2.sendMsg();
//关闭容器
ac.close();
}

@Test
public void test3(){
String cfg = "container/other/applicationContext.xml";
ApplicationContext ac =
new ClassPathXmlApplicationContext(cfg);
//此处即使没有getBean,也会实例化,因此使用lazy-init会延迟实例化。
MessageBean mb3 = ac.getBean("mb3",MessageBean.class);
}
}

测试结果:

test1:

test2:

test3:

对于init-lazy此处显示结果不是很正确,具体参考tmooc文档说明。

5 Setter注入

1.复制xml文件到包setter下
2.Computer实现序列化接口,构造函数以及set/get方法
3.xml中使用setter注入声明bean
4.测试,输出bean属性
注意,此处只涉及了一个bean,下面的一个例子涉及到三个bean

Computer.class:

package setter;

import java.io.Serializable;

public class Computer implements Serializable {
private String mainboard;
private String hdd;
private String ram;

public Computer() {
System.out.println("Computer()........");
}
public String getMainboard() {
return mainboard;
}
public void setMainboard(String mainboard) {
this.mainboard = mainboard;
}
public String getHdd() {
return hdd;
}
public void setHdd(String hdd) {
this.hdd = hdd;
}
public String getRam() {
return ram;
}
public void setRam(String ram) {
this.ram = ram;
}

}

在applicationContext.xml中声明这个bean,追加代码如下:

<bean id="computer" class="setter.Computer">
<property name="mainboard" value="技嘉"></property>
<property name="hdd" value="希捷"></property>
<property name="ram" value="金士顿"></property>
</bean>

TestCase 测试代码:

package setter;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestCase {

@Test
public void test1(){
String cfg = "setter/applicationContext.xml";
ApplicationContext ac =
new ClassPathXmlApplicationContext(cfg);
Computer computer = ac.getBean("computer",Computer.class);
System.out.println(computer.getMainboard());
System.out.println(computer.getHdd());
System.out.println(computer.getRam());
}
}

运行结果:

6 构造器注入

步骤一:创建bean

创建一个手机类MobilePhone,代码如下:

 
  1. package com.tarena.bean;
  2. import java.io.Serializable;
  3. public class MobilePhone implements Serializable {
  4. private String cpu;
  5. private String ram;
  6. public MobilePhone(String cpu, String ram) {
  7. this.cpu = cpu;
  8. this.ram = ram;
  9. }
  10. public String getCpu() {
  11. return cpu;
  12. }
  13. public void setCpu(String cpu) {
  14. this.cpu = cpu;
  15. }
  16. public String getRam() {
  17. return ram;
  18. }
  19. public void setRam(String ram) {
  20. this.ram = ram;
  21. }
  22. }

步骤二:声明bean

在applicationContext.xml中声明这个bean,追加代码如下:

 
  1. <!--构造器注入 -->
  2. <bean id="phone" class="com.tarena.bean.MobilePhone">
  3. <constructor-arg index="0" value="ARM"/>
  4. <constructor-arg index="1" value="2G"/>
  5. </bean>

步骤三:写测试代码

在TestCase中增加测试方法test6,追加代码如下:

 
  1. /**
  2. * 构造器注入
  3. */
  4. @Test
  5. public void test6() throws SQLException {
  6. String cfg = "applicationContext.xml";
  7. AbstractApplicationContext ctx =
  8. new ClassPathXmlApplicationContext(cfg);
  9. MobilePhone phone =
  10. ctx.getBean("phone", MobilePhone.class);
  11. System.out.println(phone.getCpu());
  12. System.out.println(phone.getRam());
  13. }

步骤四:执行测试

执行测试方法test6,效果如下图:

Spring day01的更多相关文章

  1. Spring day01笔记

    struts:web层,比较简单(ValueStack值栈,拦截器) hibernate:dao层,知识点杂 spring:service层,重要,讲多少用多少 --> [了解]   sprin ...

  2. 开放源代码的设计层面框架Spring——day01

    spring第一天     一.Spring概述         1.1spring概述             1.1.1spring介绍                 Spring是分层的Jav ...

  3. spring框架入门day01

    struts:web层,比较简单(ValueStack值栈,拦截器) hibernate:dao层,知识点杂 spring:service层,重要,讲多少用多少  --> [了解] spring ...

  4. Spring day02笔记

    spring day01回顾 编写流程(基于xml) 1.导入jar包:4+1 --> beans/core/context/expression | commons-logging 2.编写目 ...

  5. 05_ssm基础(三)之Spring基础

    11.spring入门引导 12.spring_HelloWord程序 实现步骤: 0.找到spring压缩包,并解压 1.拷贝jar包 2.添加主配置文件(官方文档约28页) 3.在测试中使用 13 ...

  6. spring框架入门day02

    0. 绪论 在第二天的学习中的学习JdbcTemplate之前,想要下载最新版本的mysql server8.0.11 结果在将所有的东西配置好后,发现报错, · Establishing SSL c ...

  7. 05_ssm基础(六)之SpringMVC

    36.springMVC之入门 1.springMVC简介 Spring MVC是基于MVC模式的一个Web框架,它解决WEB开发中常见的问题(参数接收.文件上传.表单验证.国际化.等等),而且使用简 ...

  8. [刘阳Java]_Spring相关配置介绍_第5讲

    这一节我们介绍一下Spring框架的相关常用配置 Spring依赖注入的两种方式(构造方法注入和setter方式注入) p-namespace方式配置 properties属性文件配置方式 集合对象配 ...

  9. [刘阳Java]_Spring IOC程序代码如何编写_第3讲

    第2讲我们介绍了Spring IOC的基本原理,这篇文章告诉大家Spring IOC程序代码是如何编写的,从而可以更好的理解IOC和DI的概念(所有的Java类的初始化工作扔给Spring框架,一个J ...

随机推荐

  1. 使用Reveal

    添加Reveal.framework,设置Other link flags 添加Debug为 -ObjC,添加 libz 库 这里介绍 Reveal UI 分析工具的简单使用,至于使用他分析手机 Ap ...

  2. FW:: ehcache memcache redis 三大缓存男高音

    最近项目组有用到这三个缓存,去各自的官方看了下,觉得还真的各有千秋!今天特意归纳下各个缓存的优缺点,仅供参考!  Ehcache 在java项目广泛的使用.它是一个开源的.设计于提高在数据从RDBMS ...

  3. php安装xcache (5.4)

    安装环境centOS6.3APACHE:apache-2.4.4PHP:5.4.13 1.安装xchache: 代码如下: # wget http://xcache.lighttpd.net/pub/ ...

  4. php--yii框架表单验证

    在视图层利用表单小部件生成表单时,field只能是数据库中存在的, 例如: use yii\helpers\Html; use yii\widgets\ActiveForm; use yii\capt ...

  5. .Net程序员安卓学习之路3:Post数据给网络API

    本例我们实现一次真正的网络交互,将数据POST到API,然后接收服务器的返回值进行处理,同时引入自定义类型和传说中阿里的FastJson. 实现思路如: 1. 在API端接收客户POST的数据还原成对 ...

  6. MVC从服务器端返回js到客户端的方法(总结)

    1.利用ViewBag,从服务器端创建一个显示js开关的ViewBag,然后到View中去做判断. Controller端 [HttpPost] public ActionResult Index(h ...

  7. Nmap 網路診斷工具基本使用技巧與教學

    Nmap 是一個開放原始碼的網路掃描與探測工具,可以讓網路管理者掃描整個子網域或主機的連接埠等,功能非常強大. Nmap(Network Mapper)是一個開放原始碼的網路檢測工具,它的功能非常強大 ...

  8. Java学习-030-JSON 之四 -- 判断 JSONObject 是否包含键值对

    前文对获取 JSON 数据封装方法,使之可通过类似于 cssSelector 的方法获取 JSON 数据,使获取数据变得简单.敬请参阅:模仿 cssSelector 封装读取 JSON 数据方法. 在 ...

  9. gulp教程

    1. http://www.tuicool.com/articles/FJVNZf 2.http://www.ydcss.com/archives/18 3.手动创建package.json: 如:c ...

  10. 转载:如何运用VI编辑器进行查找替换

    使用vi编辑器编辑长文件时,常常是头昏眼花,也找不到需要更改的内容. 这时,使用查找功能尤为重要. 方法如下: 1.命令模式下输入“/字符串”,例如“/Section 3”. 2.如果查找下一个,按“ ...