前言
Spring框架是由于软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅仅限于服务器端的开发。从简单性、可测试性和松耦合性角度而言,绝大部分Java应用都可以从Spring中受益。
组成及拓展
SpringBoot
Springboot 基于Spring可以快速的开发单个微服务
约定大于配置
SpringCloud
基于SpringBoot实现
IOC理论推导
使用set方法,将主动权交给用户,被动的接收对象,不用去管理对象的创建,只需专注于业务的拓展及实现,系统耦合性降低
建立第一个Spring项目
1.创建普通的maven项目
2.新建类
3.建立xml文件,将类托管给Spring容器
类通过bean标签建立联系,类的属性可以通过property来赋值,若属性是引用类型,则通过ref来赋值,基本类型则使用value即可
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- services -->
<bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
<property name="accountDao" ref="accountDao"/>
<property name="itemDao" ref="itemDao"/>
<!-- additional collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions for services go here -->
<bean id="accountDao"
class="org.springframework.samples.jpetstore.dao.jpa.JpaAccountDao">
<!-- additional collaborators and configuration for this bean go here -->
</bean>
<bean id="itemDao" class="org.springframework.samples.jpetstore.dao.jpa.JpaItemDao">
<!-- additional collaborators and configuration for this bean go here -->
</bean>
</beans>
4.有参构造注入
通过类型
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg type="int" value="7500000"/>
<constructor-arg type="java.lang.String" value="42"/>
</bean>
通过下标
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg index="0" value="7500000"/>
<constructor-arg index="1" value="42"/>
</bean>
请记住,要立即使用该功能,必须在启用调试标志的情况下编译代码,以便Spring可以从构造函数中查找参数名称。如果您不能或不想使用debug标志编译代码,则可以使用 @ConstructorProperties
JDK注释显式命名构造函数参数。然后,样本类必须如下所示:
package examples;
public class ExampleBean {
// Fields omitted
@ConstructorProperties({"years", "ultimateAnswer"})
public ExampleBean(int years, String ultimateAnswer) {
this.years = years;
this.ultimateAnswer = ultimateAnswer;
}
}
5.别名
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 使用Spring来创建对象,在Spring中这些对象被称为Bean-->
<bean id="hello" class="com.cheng.pojo.Hello">
<property name="string" value="Spring"/>
</bean>
<bean id="userDaoImp" class="com.cheng.dao.UserDaoImp"></bean>
<bean id="userDaoJdbcImp" class="com.cheng.dao.UserDaoJdbcImp"></bean>
<bean id="userServiceImp" class="com.cheng.service.UserServiceImp">
<property name="userDao" ref="userDaoJdbcImp"></property>
</bean>
<!-- 别名-->
<alias name="userServiceImp" alias="abcde"/>
</beans>
可以通过此别名取得相应的类的实例
6.复杂类型注入
当类的属性为数组,map,list,或其他类型时,需要使用不同的标签
p命名空间,c命名空间
<?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:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.cheng.pojo.Address">
<property name="address" value="xxxx"/>
</bean>
<bean id="student" class="com.cheng.pojo.Student">
<!-- 普通值注入-->
<property name="name" value="xx"/>
<!-- Bean注入,ref-->
<property name="address" ref="address"/>
<!--数组注入-->
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
<!-- list注入-->
<property name="hobbies">
<list>
<value>唱</value>
<value>跳</value>
<value>rap</value>
</list>
</property>
<!-- map注入-->
<property name="cards">
<map>
<entry key="1" value="A"/>
<entry key="2" value="B"/>
<entry key="3" value="C"/>
<entry key="4" value="D"/>
</map>
</property>
<!-- set注入-->
<property name="games">
<set>
<value>lol</value>
<value>nz</value>
<value>cf</value>
</set>
</property>
<property name="wife">
<null/>
</property>
<!-- properties注入-->
<property name="info">
<props>
<prop key="driver">Driver</prop>
<prop key="url">smbms</prop>
<prop key="username">root</prop>
<prop key="password">999</prop>
</props>
</property>
</bean>
<bean id="user" class="com.cheng.pojo.User" p:name="chenglong" p:password="123456"/>
<bean id="user2" class="com.cheng.pojo.User" c:name="longcheng" c:password="654321"/>
</beans>
7.注解注入
这种方法减少在xml中配置bean的代码量
需要先在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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:annotation-config/>
<!--指定要扫描的,这个包下的注解就会生效-->
<context:component-scan base-package="com.cheng.pojo"/>
</beans>
类的配置
package com.cheng.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class User {
private String name;
@Value("jjk")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
8.使用java的方式实现配置
注解为@Configuration
9.AOP
使用Spring的AOP
,需要实现AfterReturningAdvice
的方法
package com.cheng.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
@Override
//o:返回结果
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("执行了"+method.getName()+"方法,返回结果为"+o);
}
}
通过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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.cheng.service.UserServiceImp"/>
<bean id="log" class="com.cheng.log.Log"/>
<bean id="afterLog" class="com.cheng.log.AfterLog"/>
<!--配置AOP,导入aop的约束-->
<aop:config>
<!-- 切入点;expression:表达式,execution(要执行的位置! * * * *)-->
<aop:pointcut id="pointcut" expression="execution(* com.cheng.service.UserServiceImp.*(..))"/>
<!-- 执行环绕增加!-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>