本文主要是介绍SSM之SpringIOC【篇二】——入门篇,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Spring的IOC入门-环境搭建
- (1)创建Project
以我自己写的项目springcode为例。
File–>New–>Project–>左侧选择maven,右侧勾上create from archetype–>选择maven-archetype-quickstart–>next–>命名springcode,选择路径,完成! - (2)创建模块module
在springcode项目里创建新模块,右击springcode项目–>new module–>继续下来同创建Project一样。我给新模块命名为day21_springioc - (3)配置依赖
在day21_springioc的pom.xml下配置依赖
<!--spring依赖 -->
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.9.RELEASE</version>
</dependency>
Spring的IOC入门-代码编写
- (1)定义Person类
public class Person {private String username;private String password;private int id;private String name;private int age;private Date birthday;//arr private String[] arr;//Listprivate List<String> list;//setprivate Set<String> set;//mapprivate Map<String,String> map;}
- (2)手动完成创建与赋值
- (3)由spring创建与赋值
》创建容器对象
》读配置文件
new ClassPathXmlApplicationContext(“applicationContext.xml”);
》从容器中查找getBean()
Test01SpringIoc
public class Test01SpringIoc {@Testpublic void test01(){//1:创建ioc 容器对象 暂时看成mapClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");//2:给定配置文件的名称 applicationContext.xml//3:调用容器的getBean方法获取id对应的对象Person person = (Person) context.getBean("person");System.out.println(person);}
}
applicationContext.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"><!-- 要让 spring容器给我创建一个person对象-->
<!-- 配置类名,用于反向创建对象-->
<!-- 同时给一个编号方便查找--><bean id="person" class="com.syy.domain.Person" />
</beans>
Spring的IOC入门-问题解答
- (1)方法区别
context.getBean("id值", 类型.class);//无需转型
context.getBean("id值");//需转型
- (2)bean标签的属性
id:bean标签的识别ID,理论上可以随便写
class:你要上Spring给你创建哪个类的对象,需要写上该类的全路径名
- (3)赋值的方式有两种
Person person1 = new Person();
//设置方法
person1.setId(1);
System.out.println(person1);
//构造方法赋值
Person person2 = new Person(1,"jack",20,new Date());
System.out.println(person2);
applicationContext.xml
name:成员变量的名字
value:成员变量的值
一个property标签最后会调一个对应的set方法
<bean id="person2" class="com.syy.domain.Person" ><property name="id" value="10"/><property name="name" value="rose"/><property name="age" value="20"/></bean>
Spring的IOC入门-通过构造方法创建对象
- (1)利用已经写过的Person类, 目的是使用无参构造创建他的对象
- (2)applicationContext.xml 中配置
<!-- Person person2 = new Person(1,"jack",20,new Date());-->
<!-- System.out.println(person2);--><bean id="date1" class="java.util.Date"/><bean id="person3" class="com.wzx.domain.Person" ><constructor-arg name="id" value="10"/><constructor-arg name="name" value="hello"/><constructor-arg name="age" value="20"/><constructor-arg name="birthday" ref="date1"/></bean>
- 配置构造方法的参数的constructor-arg 如果有四个,就表示调的一个四个参数的构造方法。
- value可以赋上基本类型数据与String,但是其他对象,要使用ref,表示在当前容器中查找一个已存在的对象
Spring的IOC入门-静态工厂造对象
- (1)什么是静态工厂
XxxFactory.get();
- (2)通过调用静态方法获取bean对象
public class PersonFactory {public static Person getBean() {return new Person();//静态方法返回创建的对象}
}
- (3)factory-method 调用方法获得bean对象
<!-- Person person1 = PersonFactory.getBean();-->
<bean class="com.syy.demo02.PersonFactory" factory-method="getBean" id="person4"/>
Spring的IOC入门-实例工厂造对象
- (1)什么是实例工厂
XxxFactory
- (2)通过工厂对象调用成员方法获得bean对象
- (3)factory-bean 创建工厂对象
- (4)factory-method 调用方法获得bean对象
<!-- PersonFactory2 factory2 = new PersonFactory2(); 创建工厂-->
<!-- Person person1 = factory2.getBean();调用工厂的方法--><bean class="com.syy.demo03.PersonFactory2" id="factory2"/><bean factory-bean="factory2" factory-method="getBean" id="person5"/>
Spring的IOC入门-单例和多例
- (1)问题: 每次获取对象的时候,spring是新创建一个对象还是始终给我们返回同一个对象.
》单例是什么?
内存中只有一个对象,每次获取到该对象的地址值一样。
》多实例是什么?
内存中的每个对象都是一个新的对象,他们的地址值都不同。 - (2)答案: spring默认的情况下创建的对象都是单例的.(每次返回的对象都是同一个)。
scope="singleton" 单例(默认值)
scope="prototype" 多例
scope="request" 创建的对象放到request域中
scope="session" 创建对象放到session对象
单实例
<bean id="person" class="com.syy.domain.Person" scope="singleton"/>
多实例
<bean id="person" class="com.syy.domain.Person" scope="prototype"/>
Spring的IOC入门-Spring生命周期
- (1)生命周期
创建方法int
销毁方法destory
普通方法service - (2)属性
init-method 当该对象初始化的时候该方法会自动执行
destroy-method 当该对象即将销毁的时候会自动调用该方法 - (3)测试
context.close()关闭容器
public class Person{public void init(){System.out.println("哇哇...");}public void eat(){System.out.println("吃食堂...");}public void destory(){System.out.println("呜呜...");}
}
<bean id="person6" class="com.wzx.domain.Person"init-method="init"destroy-method="destory"/>
下一篇:SMM之Spring【篇三】——依赖注入
这篇关于SSM之SpringIOC【篇二】——入门篇的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!