1. IOC创建对象的方式
我们平时创建对象的方式有有参构造和无参构造 2种。
Spring创建对象的方式:
- 通过有参构造
- 通过下标
- 通过参数名 【推荐】
- 通过参数类型
- 通过无参构造
- 默认会用无参构造
注意点:一定要有一个无参构造方法
实体类
package org.xiao.pojo;
public class Hello {
private String name;
private String sex;
private int age;
public Hello() {
System.out.println("无参构造");
}
public Hello(String name) {
this.name = name;
System.out.println("有参构造1");
}
public Hello(String name, int age) {
this.name = name;
this.age = age;
System.out.println("有参构造2");
}
public Hello(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
System.out.println("有参构造3");
}
public void setName(String name) {
this.name = name;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setAge(int age) {
this.age = age;
}
public void show(){
System.out.println("Hello,"+name);
}
@Override
public String toString() {
return "Hello{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
'}';
}
}
Spring核心配置文件
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="hello" class="org.xiao.pojo.Hello">
<property name="name" value="Spring"/>
<!-- collaborators and configuration for this bean go here -->
</bean>
<!--//通过名字赋值-->
<bean id="hello2" class="org.xiao.pojo.Hello">
<constructor-arg name="name" value="xiaoming"/>
<constructor-arg name="age" value="18"/>
</bean>
<!--通过下标赋值-->
<bean id="hello3" class="org.xiao.pojo.Hello">
<constructor-arg index="0" value="lili"/>
<constructor-arg index="1" value="women"/>
<constructor-arg index="2" value="18"/>
</bean>
<!--通过类型进行赋值-->
<bean id="user4" class="org.xiao.pojo.Hello">
<constructor-arg type="java.lang.String" value="xiaoming"/>
<constructor-arg type="java.lang.Integer" value="18"/>
<constructor-arg type="java.lang.String" value="男"/>
</bean>
</beans>
测试类
package org.xiao.dao;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.xiao.pojo.Hello;
public class HelloTest {
@Test
public void test(){
//解析bean.xml配置文件
ApplicationContext context =
new ClassPathXmlApplicationContext("beans.xml");
Hello hello = (Hello) context.getBean("hello");
hello.show();
}
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Hello hello2 = (Hello) context.getBean("hello2");
System.out.println(hello2.toString());
}
@Test
public void test3(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Hello hello3 = (Hello) context.getBean("hello3");
System.out.println(hello3.toString());
}
}
2. Spring配置文件
bean
<!--bean讲解:
1.没有id,没有name,我们依旧可以获取这个对象,但是不建议使用、需要使用类的class对象获取;
User bean = context.getBean(User.class);
2.id 就是对象的名字
3.class 就是要实例化的类
4.name就是别名
有id的时候,name是别名 , 没有id的时候,name就是对象的名字
别名可以起多个
-->
<bean id="user" name="userTwo,user3" class="com.kuang.pojo.User">
<property name="name" value="Spring"/>
</bean>
alias
<!--alias讲解:
1. name : 就是bean对象的id
2,alias : 对象的别名
-->
<alias name="user" alias="user4"/>
import
作用:导入另外一个资源,把另外配置文件装进来
classpath*: 他会去所有地方寻找你的文件。【效率低】
classpath: 只会在classpath中寻找文件,找不到就报错;【建议使用】
file:填写文件路径url【不建议使用】
http:填写网络路径url【不建议使用】
<import resource="classpath*:userBeans.xml"/>
<import resource="classpath:userBeans.xml"/>
<import resource="file:"/>
<import resource="http:"/>
import一般在团队项目中会使用,每个人开发各自的beans.xml,最后用一个总的文件拼装起来。
3. 依赖注入方式
DI :Dependency Injection
依赖:指bean对象的创建依赖于Spring容器。bean对象依赖的资源
注入:指Bean对象所依赖的资源,由容器来设置和装配。
依赖注入的方式有:构造器注入和setter注入
3.1 构造器注入
- 有参
- 无参
3.2 setter注入
要求被注入的属性,必须有set方法。set方法的名字需要规范
set+属性名(属性名字母大写);
1) 常量注入
<!--普通字段-->
<property name="name" value="小明"/>
2)Bean注入
<!--引用其他bean使用ref-->
<property name="address" ref="addr"/>
3) 数组注入
<!--数组的注入-->
<property name="books">
<array>
<value>西游记</value>
<value>水浒传</value>
<value>红楼梦</value>
<value>三国演义</value>
</array>
</property>
4) List注入
<!--List注入-->
<property name="hobbys">
<list>
<value>代码</value>
<value>电影</value>
<value>音乐</value>
</list>
</property>
5) Map注入
标签:entry
键:使用key
值: 使用value
<!--Map的注入-->
<property name="card">
<map>
<entry key="IdCard" value="666666888888884444"/>
<entry key="银行卡" value="111122223333444"/>
</map>
</property>
6) Set注入
<!--Set注入-->
<property name="games">
<set>
<value>王者荣耀</value>
<value>LOL</value>
<value>绝地求生</value>
</set>
</property>
7) 空值注入
<!--Null空值注入-->
<property name="girlFriend">
<null/>
</property>
8) Properties注入
props标签:
键使用key。值在标签中间;
<!--Properties注入-->
<property name="info">
<props>
<prop key="学号">20180609</prop>
<prop key="性别">女</prop>
<prop key="姓名">小仙女</prop>
</props>
</property>
拓展1:p命名空间注入
属性的注入
<!--p:property属性,命名空间注入-->
<bean id="user1" class="org.xiao.pojo.User" p:name="小李" p:age="18"/>
拓展2:c命名空间注入
构造器的注入
<!--c:constructor构造器:命名空间注入-->
<bean id="user2" class="org.xiao.pojo.User" c:name="小明" c:age="19"/>
Spring就是一个粘合剂,托管所有的对象;
4、Bean作用域
bean就是由IOC容器初始化、装配及管理的对象。
配置文件中定义 Bean 时,我们不但可以配置 Bean 的属性值以及相互之间的依赖关系,还可以定义 Bean 的作用域 。作用域会对 Bean 的生命周期和创建方式产生影响 。
Bean 的作用域类型:
类型 | 说明 |
---|---|
singleton | 在 Spring 容器中仅存在一个 Bean 实例, Bean 以单例的形式存在,默认值。 |
prototype | 每次从容器中调用 Bean 时,都会返回一个新的实例,即相当于执行 new XxxBean() 的实例化操作。 |
request | 每次 http 请求都会创建一个新的 Bean , 仅用于 WebApplicationContext 环境。request.setAttribute("","") |
session | 同一个 http Session 共享一个 Bean ,不同的 http Session 使用不同的 Bean,仅用于 WebApplicationContext 环境。session.setAttribute("","") |
globalSession | 同一个全局 Session 共享一个 bean, 用于 Porlet, 仅用于 WebApplication 环境。application.setAttribute("","") |
在五种作用域中,request、session和globalSession三种作用域仅在web的应用中使用。
单例模式:
单例模式是:在单个Spring容器中为特定类定义一个bean,则Spring容器将创建该bean所定义的类的一个且仅一个实例。
单例模式是Spring中的默认范围。
【核心:对象在内存中只有一份】