本文主要是介绍【Spring篇】一.spring及springBean的配置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
IOC (控制反转)
-
将控制权交给Spring容器,要使用的时候找spring容器也获取依赖对象
-
应用程序本身不负责依赖对象的创建和维护,而是由外部容器负责创建和维护
-
DI (依赖注入) 控制反转的一种实现方式
-
ioc目的:创建对象且组装对象之间的关系。spring容器在初始化的时候会创建一系列对象,同时将对象之间的依赖关系通过注入方式住址起来
扩展
q:控制反转,哪些方面的控制被反转了?a:获得依赖对象的过程被反转了。获得对象的过程不是由我们来创建,而是通过spring容器来注入,即依赖注入:由spring容器在运行期间,动态地将某种依赖关系注入对象之中。
文解
房屋中介 -- ioc中介 -- 容器找房子 -- 返回对象入住 -- 使用对象
Bean配置
1. spring.xml配置 (也可以applicationContext.xml)
- java项目放src下
- maven项目放resources下
<?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"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!--<bean></bean>--><bean>
基础包org.springframework.beansorg.springframework.context
初始化
// 通过当前相对路径加载spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");// 通过当前绝对路径加载spring容器// ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
<bean id="student" class="com.wpj.bean.Student"></bean>
@Testpublic void showSpringBean(){// 通过当前相对路径加载spring容器ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");// 根据id获取beanStudent student = (Student) ac.getBean("student");System.out.println(student);}
2. Spring注入
在启动spring容器加载bean配置的时候,完成对变量的赋值行为
2.1 设值注入
2.1.1 javaBean
// 定义beanpublic class Student{//定义属性private String name;private int age;// 提供getter和setterpublic String getName() { return name; }public void setName(String name) { this.name = name; }public int getAge() { return age; }public void setAge(int age) { this.age = age; }}
2.1.2 spring.xml
<bean id="student" class="com.wpj.bean.student"><property name="name" value="jieKaMi"></property><property name="age" value="22"></property></bean>
2.1.3 test
@Testpublic void showSpringBean(){// 通过当前相对路径加载spring容器ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");// 根据id获取beanStudent student = (Student) ac.getBean("student");System.out.println(student);}
2.2 构造注入
2.2.1 javaBean
public class Student{//定义属性private String name;private int age;// 提供构造方法public Student(){}public Student(String name, int age){this.name = name;this.age = age;}}
2.2.2 spring.xml
<bean id="student" class="com.wpj.bean.student"><constructor-arg name="name" value="jieKaMi"></constructor-arg><constructor-arg name="age" value="18"></constructor-arg></bean>
2.2.3 test
@Testpublic void showSpringBean(){// 通过当前相对路径加载spring容器ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");// 根据id获取beanStudent student = (Student) ac.getBean("student");System.out.println(student);}
3 bean的配置项
- id bean的唯一标识
- class 实例对象的全路径类名 (必须有)
<bean id="student" class="com.wpj.bean.Student"></bean>
-scope 作用域注: 需要在同一个spring容器中常用:scope="prototype" 多例 如果scope=“prototype”则不生效scope="singleton" 单例 默认scope="request" 每次http请求创建一个实例且仅在当前request实例内scope="session" 每次http请求创建一个实例且仅在当前session实例scope="global session" 基于portlet的web中有效 (portlet定义了global session),如果是在web中,同session
<!-- spring容器在进行输出prototype的bean对象时,会每次都重新生成一个新的对象给请求方 --><bean id="student" class="com.wpj.bean.Student" scope="prototype"></bean><!-- singleton类型的bean定义从容器启动到第一次被请求而实例化开始,只要容器不销毁或退出,该类型的bean的单一实例就会一直存活 --><bean id="student" class="com.wpj.bean.Student" scope="singleton "></bean>
- constructor-arg 设置要设置全部
- property 要设哪个就设哪个
<!-- 构造方法注入:时效性最好 --><bean id="student" class="com.wpj.bean.Student"><constructor-arg name="name" value="jieKaMi"></constructor-arg><constructor-arg name="age" value="18"></constructor-arg></bean><!-- set方法注入:灵活性好 --><bean id="student" class="com.wpj.bean.Student"><property name="name" value="jieKaMi"></property><property name="age" value="22"></property></bean>
- lazy-init 懒加载lazy-init="true" 默认lazy-init="false"
<!-- 即时加载 容器启动立刻创建对象 消耗资源,有利于提前发现错误 --><bean id="student" class="com.wpj.bean.Student" lazy-init="false"></bean><!-- 懒加载 真正使用对象的时候才创建对象 节省资源,但不利于提前发现错误 --><bean id="student" class="com.wpj.bean.Student" lazy-init="true"></bean>
- 初始化与销毁方法init-method=""destroy-method=""
这篇关于【Spring篇】一.spring及springBean的配置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!