Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
目前是JavaEE开发的灵魂框架。他可以简化JavaEE开发,可以非常方便整合其他框架,无侵入的进行功能增强。
Spring的核心就是 控制反转(IoC)和面向切面(AOP) 。
控制反转,之前对象的控制权在类手上,现在反转后到了Spring手上。
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.1.9.RELEASEversion>
dependency>
<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="helloworld" class="com.lx.pojo.helloWorld">bean>
beans>
public class helloWorldTest {
@Test
public void testHelloWorld(){
//获取IOC容器
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取IOC容器中的bean
helloWorld helloworld = (helloWorld)ioc.getBean("helloworld");
helloworld.sayHello();
}
}
依赖注入可以理解成IoC的一种应用场景,反转的是对象间依赖关系维护权。依赖注入就是IOC的具体实现
在要注入属性的bean标签中进行配置。前提是该类有提供属性对应的set方法。
public class Student {
private Integer id;
private String name;
private Integer age;
private String sex;
private String[] hobby;
private Clazz clazz;
private Map<String,Teacher> map;
public Student(){
System.out.println("IOC通过无参构造创建对象");
}
public Student(Integer id, String name, Integer age, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public Clazz getClazz() {
return clazz;
}
public void setClazz(Clazz clazz) {
this.clazz = clazz;
}
public Map<String, Teacher> getMap() {
return map;
}
public void setMap(Map<String, Teacher> map) {
this.map = map;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", hobby=" + Arrays.toString(hobby) +
", clazz=" + clazz +
", map=" + map +
'}';
}
}
<bean id="studentTwo" class="com.lx.pojo.Student">
<property name="id" value="001">property>
<property name="name" value="张三">property>
<property name="age" value="19">property>
<property name="sex" value="男">property>
bean>
<bean id="studentThree" class="com.lx.pojo.Student">
<constructor-arg value="002">constructor-arg>
<constructor-arg value="李四">constructor-arg>
<constructor-arg value="30">constructor-arg>
<constructor-arg value="女" name="sex" index="3" type="java.lang.String">constructor-arg>
bean>
配置如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<bean id="studentFour" class="com.lx.pojo.Student">
<property name="id" value="001">property>
<property name="name">
<value>]]>value>
property>
<property name="age"><null/>property>
<property name="sex" value="男">property>
bean>
<bean id="studentFive" class="com.lx.pojo.Student">
<property name="id" value="004">property>
<property name="name" value="王五">property>
<property name="age" value="19">property>
<property name="sex" value="男">property>
<property name="clazz">
<bean id="clazzInner" class="com.lx.pojo.Clazz">
<property name="cid" value="006">property>
<property name="cname" value="大数据">property>
bean>
property>
<property name="hobby">
<array>
<value>抽烟value>
<value>喝酒value>
<value>烫头value>
array>
property>
<property name="map">
<map>
<entry key="1" value-ref="teacherOne">entry>
<entry key="2" value-ref="teacherOne">entry>
map>
property>
bean>
<bean id="studentSix" class="com.lx.pojo.Student"
p:id="1009" p:age="18" p:name="张三">bean>
<bean id="teacherOne" class="com.lx.pojo.Teacher">
<property name="tid" value="1">property>
<property name="tname" value="张老师">property>
bean>
<bean id="clazzOne" class="com.lx.pojo.Clazz">
<property name="cid" value="1">property>
<property name="cname" value="rj1class">property>
bean>
<bean id="clazzTwo" class="com.lx.pojo.Clazz">
<property name="cid" value="1">property>
<property name="cname" value="rj1class">property>
<property name="students" ref="studentList">property>
bean>
<util:list id="studentList">
<ref bean="studentTwo">ref>
<ref bean="studentThree">ref>
<ref bean="studentFour">ref>
util:list>
beans>
lombok作用 作用:简化用户创建实体对象(POJO)的过程,由插件自动的完成实体对象中常用方法的构建(get/set/toString/构造等)
1.导入依赖
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.16version>
dependency>
2.添加注解
@Data //根据属性生成set,get方法
@NoArgsConstructor //生成空参构造
@AllArgsConstructor //生成全参构造
public class Phone {
private double price;
private String name;
private String password;
private String path;
}
我们可以让Spring读取properties文件中的key/value,然后使用其中的值。
在Spring配置文件中加入如下标签:指定要读取的文件的路径。
<context:property-placeholder location="classpath:filename.properties">
其中的classpath表示类加载路径下。
我们也会用到如下写法:classpath:*.properties 其中的 * 表示文件名任意。
注意:context命名空间的引入是否正确
<context:property-placeholder location="jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}">property>
<property name="url" value="${jdbc.url}">property>
<property name="username" value="${jdbc.username}">property>
<property name="password" value="${jdbc.password}">property>
bean>
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
private String name;
private int id;
private int age;
//初始化方法
public void init(){
System.out.println("对学生对象进行初始化操作");
}
}
<bean class="com.lx.domain.Student" id="student" init-method="init">bean>
注意:配置的初始化方法只能是空参的。@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
private String name;
private int id;
private int age;
public void init(){
System.out.println("对学生对象进行初始化操作");
}
public void close(){
System.out.println("对象销毁之前调用,用于释放资源");
}
}
<bean class="com.lx.domain.Student" id="student" destroy-method="close">bean>
当我们需要让Spring容器使用工厂类来创建对象放入Spring容器的时候可以使用factory-bean和factory-method属性。
1.配置实例工厂创建对象
<bean class="com.lx.factory.CarFactory" id="carFactory">bean>
<bean factory-bean="carFactory" factory-method="getCar" id="car">bean>
测试
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取car对象
Car c = (Car) app.getBean("car");
System.out.println(c);
2.配置静态工厂创建对象
<bean class="com.lx.factory.CarStaticFactory" factory-method="getCar" id="car2">bean>
3.也可以通过实现FactoryBean接口创建工厂
public class UserFactory implements FactoryBean<User> {
@Override
public User getObject() throws Exception {
return new User();
}
@Override
public Class<?> getObjectType() {
return User.class;
}
}
<bean id="userFactory" class="com.lx.factory.UserFactory">bean>
测试类
/*
* FactoryBean是一个接口,需要定义一个类实现该接口
* getObject():通过一个对象交给IOC容器管理
* getObjectType():设置所提供对象的类型
* isSingleton():是否单例
*
* */
public class UserFactoryTest {
@Test
public void testUserFactory(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-factory.xml");
User user = context.getBean(User.class);
System.out.println(user);
}
}
public class LifeCycleTest {
/*
* 生命周期:
* 1.实例化
* 2.依赖注入
* 3.后置处理器postProcessBeforeInitialization:在初始化之前执行 (实现BeanPostProcessor接口)
* 4.初始化 通过init-method指定初始方法
* 5.后置处理器postProcessAfterInitialization:在初始化之后执行 (实现BeanPostProcessor接口)
* 6.销毁 通过destroy-method指定销毁方法
* */
@Test
public void test(){
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("spring-lifecycle.xml");
User user = context.getBean(User.class);
System.out.println(user);
context.close();
}
}
<bean id="user" class="com.lx.pojo.User" init-method="initMethod" destroy-method="destoryMethod">
<property name="id" value="12">property>
<property name="username" value="admin">property>
<property name="password" value="123456">property>
bean>
<bean id="myBeanPostProcessor" class="com.lx.process.MyBeanPostProcessor">bean>
后置处理器
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
//在bean的生命周期初始化之前执行
System.out.println("MyBeanPostProcessor--->后置处理器postProcessBeforeInitialization");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
//在bean的生命周期初始化之后执行
System.out.println("MyBeanPostProcessor--->后置处理器postProcessAfterInitialization");
return bean;
}
}