diff --git a/Demo.java b/Demo.java new file mode 100644 index 0000000000000000000000000000000000000000..fe5171e20d966c4b73bf9d6bd1504b63a9004387 --- /dev/null +++ b/Demo.java @@ -0,0 +1,85 @@ +package com.tedu.spring.day01; + +import com.tedu.spring.demo.Person; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * Spring IOC容器---了解细节 + * 类多次测试,两种方案:main主方法输出 只想一次 + * junit单元测试输出 执行多次 + * junit单元测试 + * 1.导入junit外置包,建议maven--创建maven互联网自带junit + * 2.使用注解 --语法 : @Test 注解首字符必须大写 + * 导入包:import org.junit.Test; + * 3.空参的方法--类似于主方法 + */ +public class Demo { + //junit写法 + @Test + public void test01(){ + System.out.println("hello World111"); + } + //Spring IOC容器细节问题 + //①.默认情况下,多次获取同一个id的bean将获取“同一个对象” + //为什么? 创建容器的时候,以Map形式保存key(bean标签信息id属性)以及value(bean标签class属性) + // Map特点:key不允许重复 + @Test + public void test02(){ + //1.创建IOC容器,完成3项:①加载bean(类)信息②反射创建空构造器对象③将id以map形式进行保存 + ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml"); + //2.获取容器中对象 Person + Person p1=(Person)ac.getBean("person1"); + System.out.println("获取第一个人类对象:"+p1); + Person p2=(Person)ac.getBean("person3"); + System.out.println("获取第二个人类对象:"+p2); + } + /** SpringIOC容器细节 1 获取对象的问题 + * ①.默认情况下,多次获取同一个id的bean将获取“同一个对象” + * ②.“不可以”配置多个id相同的bean + * ③.配置多个id不同,但是class相同的bean + * + */ + /** 叫法:“句柄” 书:Java核心编程思想 “构造方法 构造函数” + * java类---->①“构造器” ---作用:创建对象默认存在 写法:public 类名(){} + * 分为两种:无参数构造器----创建对象默认存在 + * 有参数构造器----结合属性进行指定 + * ②方法 ---作用:实现功能 写法:public 返回值 方法名(){} + * ③属性 ---作用:数据变量 写法:private String name; + * 16点15分继续 + * SpringIOC容器细节 2 构造器问题 创建对象 Person p=new Person(); + * ①springIoc容器获取默认调用无参数构造器--- + * ② + */ + @Test + public void test03(){ + ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml"); + Location l=(Location)ac.getBean("location"); + System.out.println(l); //打印16进制地址值 + } + + /** + * SpringIOC容器细节 3 方法的问题 + * Java支持23设计模式:静态工厂模式 、实例工厂模式、spring工厂模式 单例模式、多例模式 + * + */ + @Test + public void test04(){ + //传统创建对象方式 + NetConnection nc=new NetConnection(); + nc.load();nc.ping();nc.conn();nc.sendData(); + //使用工厂模式:静态工厂 把重复事情交给工厂操作 + } + + + + + + + + + + + +} diff --git a/Location.java b/Location.java new file mode 100644 index 0000000000000000000000000000000000000000..a37e92a92b289377ec434030926daace51fb0f6a --- /dev/null +++ b/Location.java @@ -0,0 +1,13 @@ +package com.tedu.spring.day01; + +/** + *Location 类 + * 构造器 + */ +public class Location { + + //创建有参数构造器 + public Location(String name,char a){ + System.out.println("有参数的构造器"+name+" "+a); + } +} diff --git a/beans.xml b/beans.xml new file mode 100644 index 0000000000000000000000000000000000000000..d745cc5f0ba6535e795b7395256dc5c58f203584 --- /dev/null +++ b/beans.xml @@ -0,0 +1,25 @@ +<?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"> +<!--1.1 保存对象 --> + <bean id="person1" class="com.tedu.spring.demo.Person"/> + <bean id="person2" class="com.tedu.spring.demo.Person"/> + <bean id="person3" class="com.tedu.spring.demo.Person"/> +<!-- 无参构造器问题 --> + <!-- <bean id="location" class="com.tedu.spring.day01.Location"/>--> +<!-- 有参构造器问题 --> + <bean id="location" class="com.tedu.spring.day01.Location"> +<!-- constructor 构造器 arg 参数 清楚一个构造器参数 + <constructor-arg></constructor-arg> 表示一个构造器参数 + index属性:第几个参数,从0开始 + name属性:对应参数的引用名 + value属性:对应参数的值 char字符 'a' String字符串 区别???? + type属性:对应参数类型 +--> + <constructor-arg index="0" name="name" value="广州" type="java.lang.String"></constructor-arg> + <constructor-arg index="1" name="a" value='粤' type="char"></constructor-arg> + + </bean> + +</beans>