layout: post
title: spring
subtitle: 搭建、配置、属性注入
date: 2018-06-04
author: ZL
header-img: img/20180604.jpg
catalog: true
tags:
- spring
- 属性注入
介绍
三层架构中spring位置
spring一站式框架
- 正是因为spring框架性质是属于容器性质的.
- 容器中装什么对象就有什么功能.所以可以一站式.
- 不仅不排斥其他框架,还能帮其他框架管理对象.
- aop支持
- ioc思想
- spring jdbc
- aop 事务
- junit 测试支持
例子搭建
导包
基础包
日志包
可选日志包(老版本需要,新版本不需要)
创建一个对象
public class User {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
配置文件applicationContext.xml(需要在src目录下,命名可以随意)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<!-- 将User对象交给spring容器管理 -->
<!-- Bean元素:使用该元素描述需要spring容器管理的对象
class属性:被管理对象的完整类名.
name属性:给被管理的对象起个名字.获得对象时根据该名称获得对象.
可以重复.可以使用特殊字符.
id属性: 与name属性一模一样.
名称不可重复.不能使用特殊字符.
结论: 尽量使用name属性.
-->
<bean name="user" class = "zl.bean.User"></bean>
</beans>
代码测试
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import zl.bean.User;
public class Demo {
@Test
public void test() {
//1.创建容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.向容器要User对象
User u = (User) ac.getBean("user");
//3.打印user对象
System.out.println(u);
}
}
打印结果
zl.bean.User@670b40af
spring理论
IOC:就是对象的创建不需要new了,而是从spring中去取。
DI:就是说A对象里面用到了B对象,会把B对象注入到A对象里面
applicationContext&BeanFactory(了解)
applicationContext和BeanFactory功能差不多,在以前电脑配置差的时候使用BeanFactory,现在都用applicationContext了。主要区别就是对内存的需求大小不一样,
BeanFactory接口实现类的容器.特点是每次在获得对象时才会创建对象
applicationContext每次容器启动时就会创建容器中配置的所有对象.并提供更多功能
applicationContext使用:
public void test() {
//1.创建容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.向容器要User对象
User u = (User) ac.getBean("user");
//3.打印user对象
System.out.println(u);
}
BeanFactory使用:
public void test() {
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
User u = (User) factory .getBean("user");
System.out.println(u);
}
applicationContext加载配置文件
丛类路径下加载配置文件:ClassPathXmlApplicationContext
从硬盘绝对路径下加载配置文件:FileSystemXmlApplicationContext("d:/xxx/yyy/xxx")
配置详解
bean
<!-- 将User对象交给spring容器管理 -->
<!-- Bean元素:使用该元素描述需要spring容器管理的对象
class属性:被管理对象的完整类名.
name属性:给被管理的对象起个名字.获得对象时根据该名称获得对象.
可以重复.可以使用特殊字符.
id属性: 与name属性一模一样.
名称不可重复.不能使用特殊字符.
结论: 尽量使用name属性.
-->
<bean name="user" class = "zl.bean.User"></bean>
scope属性
位置在applicationContext.xml:
<bean name="user1" class = "zl.bean.User" scope=""></bean>
取值:
- singleton(默认值):单例对象.被标识为单例的对象在spring容器中只会存在一个实例
public void fun4(){
ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/b_create/applicationContext.xml");
User u = (User) ac.getBean("user");
User u2 = (User) ac.getBean("user");
User u3 = (User) ac.getBean("user");
User u4 = (User) ac.getBean("user");
System.out.println(u2==u4);//单例:true
}
- prototype:多例原型.被标识为多例的对象,每次在获取对象时才会创建.每次创建都是新的对象.整合struts2时,ActionBean必须配置为多例的.
public void fun4(){
ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/b_create/applicationContext.xml");
User u = (User) ac.getBean("user");
User u2 = (User) ac.getBean("user");
User u3 = (User) ac.getBean("user");
User u4 = (User) ac.getBean("user");
System.out.println(u2==u4);//多例:false
}
- request:web环境下.对象与request生命周期一致.(没用)
- session:web环境下,对象与session生命周期一致.(没用)
lazy-init="true"
延迟加载,启动时不实例化,而是第一次被请求时才创建实例。不设置的话,默认false,启动时就创建实例。
ps:当lazy-init="false",而scope的值是prototype时,依然是获取对象时才会被实例化。
<!-- A bean definition with lazy init set on -->
<bean id="..." class="..." lazy-init="true">
<!-- collaborators and configuration for this bean go here -->
</bean>
生命周期属性
配置位置在applicationContext.xml:
<bean
name="user" class="cn.itcast.bean.User" init-method="init" destroy-method="destory" >
</bean>
- init-method:配置一个方法作为生命周期初始化方法.spring会在对象创建之后立即调用.
- destory-method:配置一个方法作为生命周期的销毁方法.spring容器在关闭并销毁所有容器中的对象之前调用.
方法所在位置:
public class User {
public User() {
System.out.println("User对象空参构造方法!!!!");
}
private String name;
private Integer age;
private Car car;
public User(String name, Car car) {
System.out.println("User(String name, Car car)!!");
this.name = name;
this.car = car;
}
public User(Car car,String name) {
System.out.println("User(Car car,String name)!!");
this.name = name;
this.car = car;
}
public User(Integer name, Car car) {
System.out.println("User(Integer name, Car car)!!");
this.name = name+"";
this.car = car;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
//============================
public void init(){
System.out.println("我是初始化方法!");
}
public void destory(){
System.out.println("我是销毁方法!");
}
//=============================
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
}
}
ps:还可以配置全局的init和destory:
Spring Bean后置处理器(2018.8.10添加关于后置处理器的内容)
在bean被实例化的前后分别有两个回调方法,有需要的话可以在这两个方法中作其他逻辑处理。
bean类:
package com.tutorialspoint;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
public void init(){
System.out.println("Bean is going through init.");
}
public void destroy(){
System.out.println("Bean will destroy now.");
}
}
后置处理器类
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.BeansException;
public class InitHelloWorld implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("BeforeInitialization : " + beanName);
return bean; // you can return any other object as well
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("AfterInitialization : " + beanName);
return bean; // you can return any other object as well
}
}
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-3.0.xsd">
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld"
init-method="init" destroy-method="destroy">
<property name="message" value="Hello World!"/>
</bean>
<bean class="com.tutorialspoint.InitHelloWorld" />
</beans>
调用
package com.tutorialspoint;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
context.registerShutdownHook();
}
}
运行结果:
BeforeInitialization : helloWorld(后置处理器的before方法)
Bean is going through init.(bean的init方法被调用)
AfterInitialization : helloWorld(后置处理器的after方法)
Your Message : Hello World!
Bean will destroy now.(bean的destroy方法被调用)
1.可以看到后置处理器的两个方法运行的位置是在bean类的init方法的前后
2.后置处理器的配置<bean class="com.tutorialspoint.InitHelloWorld" />,就像普通bean类一样在xml文件中申明就可以了
3.后置处理器申明了以后是没有和某个bean类绑定的,它是对所有的bean类都起作用的
4.另外,是可以配置多个后置处理器的,并且这多个后置处理器都可以起作用,还可以指定后置处理器的调用顺序,需要实现接口Ordered,这个这里不详细说明,可以自行查阅其他资料
Spring Bean定义继承(2019.8.10添加关于bean定义继承的内容)
这个作用不大,而且也是一看就懂系列,其他代码内容就不放了,直接放xml中bean的代码
- 方式一:
<?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-3.0.xsd">
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
<property name="message1" value="Hello World!"/>
<property name="message2" value="Hello Second World!"/>
</bean>
<!--parent="helloWorld"指明helloworld是它的父亲-->
<bean id="helloIndia" class="com.tutorialspoint.HelloIndia" parent="helloWorld">
<property name="message1" value="Hello India!"/>
<property name="message3" value="Namaste India!"/>
</bean>
</beans>
方式二:专门搞一个bean的模板,父bean是abstract,不能被实例化,只是给其他bean作父亲用的
<?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-3.0.xsd">
<bean id="beanTeamplate" abstract="true">
<property name="message1" value="Hello World!"/>
<property name="message2" value="Hello Second World!"/>
<property name="message3" value="Namaste India!"/>
</bean>
<bean id="helloIndia" class="com.tutorialspoint.HelloIndia" parent="beanTeamplate">
<property name="message1" value="Hello India!"/>
<property name="message3" value="Namaste India!"/>
</bean>
</beans>
spring创建Bean对象方式
方式一:空参构造方式(最常用)
applicationContext.xml:
<bean name="user1" class = "zl.bean.User"></bean>
java调用:
@Test
public void test() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User u = (User) ac.getBean("user1");
System.out.println(u);
}
方式二:静态工厂创建(了解)
applicationContext.xml:
<bean name="user2" class="zl.factory.UserFactory" factory-method="createUser"></bean>
UserFactory:
public class UserFactory {
//注意是static
public static User createUser() {
System.out.println("静态工厂创建User");
return new User();
}
}
java调用:
@Test
public void test() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User u2 = (User) ac.getBean("user2");
System.out.println(u2);
}
方式三:实例工厂创建(了解)
applicationContext.xml:
<bean name="user3" factory-bean="userFactory" factory-method="createUser2"></bean>
<bean name="userFactory" class="zl.factory.UserFactory"></bean>
UserFactory:
public class UserFactory {
//注意不是static
public User createUser2() {
System.out.println("实例工厂创建User");
return new User();
}
}
java调用:
@Test
public void test() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User u3 = (User) ac.getBean("user3");
System.out.println(u3);
}
spring分模块配置
public class User {
public User() {
System.out.println("User对象空参构造方法!!!!");
}
private String name;
private Integer age;
private Car car;
public User(String name, Car car) {
System.out.println("User(String name, Car car)!!");
this.name = name;
this.car = car;
}
public User(Car car,String name) {
System.out.println("User(Car car,String name)!!");
this.name = name;
this.car = car;
}
public User(Integer name, Car car) {
System.out.println("User(Integer name, Car car)!!");
this.name = name+"";
this.car = car;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public void init(){
System.out.println("我是初始化方法!");
}
public void destory(){
System.out.println("我是销毁方法!");
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
}
}
Car
package zl.bean;
public class Car {
private String name;
private String color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Car [name=" + name + ", color=" + color + "]";
}
}
set注入
配置
<bean name="user1" class = "zl.bean.User">
<!-- 值类型的set注入 -->
<property name="name" value = "tom"></property>
<property name="age" value = "18"></property>
<!-- 引用类型的set注入 -->
<property name="car" ref = "car"></property>
</bean>
<!-- 想要引用类型的set注入,必须先申明引用类型 -->
<bean name="car" class="zl.bean.Car">
<property name="name" value="兰博基尼" ></property>
<property name="color" value="黄色" ></property>
</bean>
调用
@Test
public void fun1() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("zl/injection/applicationContext.xml");
User user = (User) applicationContext.getBean("user1");
System.out.println(user);
}
运行结果
User [name=tom, age=18, car=Car [name=兰博基尼, color=黄色]]
构造函数注入
配置
<!-- name属性: 构造函数的参数名 -->
<!-- index属性: 构造函数的参数索引(第几个参数)(可以不写) -->
<!-- type属性: 构造函数的参数类型(可以不写)-->
<bean name="user2" class="zl.bean.User">
<constructor-arg name="name" index="0" type="java.lang.String" value="999"></constructor-arg>
<constructor-arg name="car" ref="car" index="1"></constructor-arg>
</bean>
<!-- 想要引用类型的set注入,必须先申明引用类型 -->
<bean name="car" class="zl.bean.Car">
<property name="name" value="兰博基尼" ></property>
<property name="color" value="黄色" ></property>
</bean>
调用
@Test
public void fun2() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("zl/injection/applicationContext.xml");
User user = (User) applicationContext.getBean("user2");
System.out.println(user);
}
运行结果
User对象空参构造方法!!!!
User(Integer name, Car car)!!
User [name=999, age=null, car=Car [name=兰博基尼, color=黄色]]
p名称控件注入(不掌握)
配置
<!--必须加上这个约束-->
xmlns:p="http://www.springframework.org/schema/p"
<bean name="user3" class="zl.bean.User" p:name = "tom" p:age = "29" p:car-ref="car"></bean>
<!-- 想要引用类型的set注入,必须先申明引用类型 -->
<bean name="car" class="zl.bean.Car">
<property name="name" value="兰博基尼" ></property>
<property name="color" value="黄色" ></property>
</bean>
调用
@Test
public void fun3() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("zl/injection/applicationContext.xml");
User user = (User) applicationContext.getBean("user3");
System.out.println(user);
}
运行结果
User对象空参构造方法!!!!
User(String name, Car car)!!
User对象空参构造方法!!!!
User [name=tom, age=29, car=Car [name=兰博基尼, color=黄色]]
spel注入(spring Expression Language sping表达式语言)(不掌握)
配置
<!--需要配置好其他的user1、user4、car等-->
<bean name="user4" class = "zl.bean.User">
<property name="name" value = "#{user1.name}"></property>
<property name="age" value = "#{user3.age}"></property>
<property name="car" ref = "car"></property>
</bean>
调用
@Test
public void fun4() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("zl/injection/applicationContext.xml");
User user = (User) applicationContext.getBean("user4");
System.out.println(user);
}
运行结果
User对象空参构造方法!!!!
User(String name, Car car)!!
User对象空参构造方法!!!!
User对象空参构造方法!!!!
User [name=user的nametom, age=29, car=Car [name=兰博基尼, color=黄色]]
spring复杂类型属性注入
bean类准备
package zl.injection;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class CollectionBean {
private Object[] arr;//数组类型注入
private List list;//list/set 类型注入
private Map map;//map类型注入
private Properties prop;//properties类型注入
public Object[] getArr() {
return arr;
}
public void setArr(Object[] arr) {
this.arr = arr;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Properties getProp() {
return prop;
}
public void setProp(Properties prop) {
this.prop = prop;
}
@Override
public String toString() {
return "CollectionBean [arr=" + Arrays.toString(arr) + ", list=" + list + ", map=" + map + ", prop=" + prop
+ "]";
}
}
配置
<bean name="cb" class="zl.injection.CollectionBean">
<property name="arr">
<array>
<value>tom</value>
<value>jack</value>
<!--往数组里面存对象类型-->
<ref bean="user4"/>
</array>
</property>
<property name="list" >
<list>
<value>jack</value>
<value>rose</value>
<ref bean="user3" />
</list>
</property>
<property name="map" >
<map>
<entry key="url" value="jdbc:mysql:///crm" ></entry>
<entry key="user" value-ref="user4" ></entry>
<entry key-ref="user3" value-ref="user2" ></entry>
</map>
</property>
<property name="prop" >
<props>
<prop key="driverClass">com.jdbc.mysql.Driver</prop>
<prop key="userName">root</prop>
<prop key="password">1234</prop>
</props>
</property>
</bean>
调用
@Test
public void fun5() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("zl/injection/applicationContext.xml");
CollectionBean cb = (CollectionBean) applicationContext.getBean("cb");
System.out.println(cb);
}
结果
CollectionBean [arr=[tom, jack, User [name=user的nametom, age=29, car=Car [name=兰博基尼, color=黄色]]], list=[jack, rose, User [name=tom, age=29, car=Car [name=兰博基尼, color=黄色]]], map={url=jdbc:mysql:///crm, user=User [name=user的nametom, age=29, car=Car [name=兰博基尼, color=黄色]], User [name=tom, age=29, car=Car [name=兰博基尼, color=黄色]]=User [name=999, age=null, car=Car [name=兰博基尼, color=黄色]]}, prop={driverClass=com.jdbc.mysql.Driver, password=1234, userName=root}]
注入空值或者null(2018.8.10添加注入控制和null)
注入空值:
<bean id="..." class="exampleBean">
<property name="email" value=""/>
</bean>
--------------------------------------
注入null:
<bean id="..." class="exampleBean">
<property name="email"><null/></property>
</bean>