文章内容
-
什么是Spring
-
Spring中Bean的配置
-
ApplicationContext
-
Spring支持四种依赖注入的方式
-
Spring中Bean的相互引用
-
Null值和级联属性
-
集合属性赋值
-
Bean自动装配
-
Bean的作用域
-
Bean之间的关系
-
Bean的使用外部属性文件
-
Spring表达式语言SpEL
-
Bean的生命周期
-
Spring AOP面向切面编程
-
Spring 支持JDBC操作数据库
-
Spring事务管理
一、什么是Spring
Spring是一个开源框架,是一个IOC(DI)(反转控制或依赖注入)和AOP容器框架
IOC:(反转控制)其思想是反转资源获取的方向,传统的资源查找方式要求组件向容器发起请求查找资源,作为回应,容器适时的返回资源。而应用了IOC之后,则是容器主动的将资源推送给它所管理的组件,组件所要做的仅是选择一种合适的方式来接受资源,这种行为也被称为查找的被动形式。
DI:IOC的另一种表述方式,即组件以一些预先定义好的方式接受来自如容器的资源注入,相对于IOC而言,这种表述更直接。
二、Spring中Bean的配置
创建HelloWorld类用于测试
public class HelloWorld {
private String name;
public void setName(String name){
this.name = name;
}
public void sayHello(){
System.out.println("Hello "+name);
}
}
新建一个Bean的xml,在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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置Bean class:bean的全类名,通过反射的方式在IOC容器中创建Bean,所以要求Bean中必须含有无参的构造方式id:Bean的名称,作为唯一标识,所以id必须是唯一的-->
<bean id="helloWorld" class="Spring.HelloWorld"><!--通过全类名来配置-->
<property name="name" value="Spring"/><!--设置初始值为Spring-->
</bean>
</beans>
通过ApplicationContext获取Bean实例并调用方法
public class Main {
public static void main(String[] args){
//1.创建Spring的IOC容器对象
ApplicationContext mContext = new ClassPathXmlApplicationContext("spring-config.xml");
//2.从IOC容器中获取Bean实例
HelloWorld helloWorld = (HelloWorld) mContext.getBean("helloWorld");//通过id读取Bean
//通过类型获取Bean HelloWorld helloWorld = mContext.getBean(HelloWord.class);缺点就是当有多个相同类型的Bean时无法返回
//3.调用方法
helloWorld.sayHello();
}
}
三、ApplicationContext是一个接口,其有两个主要的实现类:
ClassPathXmlApplicationContext:从类路径下加载配置文件,去新增了两个主要方法 refresh()和close(),让ApplicationContext有了启动,刷新和关闭上下文的功能。
FileSystemXmlApplicationContext:从文件系统中加载配置文件
四、Spring支持四种依赖注入的方式
属性注入:属性注入即通过setter方法注入Bean的属性值或依赖的对象 属性注入使用<property>元素,使用name属性指定Bean中属性名称,value属性或者<value>子节点指定属性值。
属性注释示例
<bean id="helloWorld" class="Spring.HelloWorld">
<property name="name" value="Spring"/>
</bean>
构造方法注入:通过构造方法注入Bean的属性值或依赖的对象,它保证了Bean示例在实例化后就可以使用,构造器注入在<constructor-arg>元素里声明属性,<constructor-arg>中没有name属性。
构造方法注入示例
public class Dog {
private String name;
private int age;
private String sex;
public Dog(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public 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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
}
<bean id="dog" class="Spring.Dog">
<constructor-arg value="Test" index="0" type="java.lang.String"/><!--如果没有index则按顺序注入--><!--当用参数个数相同的构造方法时我们还可以通过 type来进行区分-->
<constructor-arg value="12" index="1"/>
<constructor-arg index="2">
<value><![CDATA[<公^>]]></value><!--当value中包含特殊字符时我们可以使用<![CDATA[]]>来包裹内容-->
</constructor-arg>
</bean>
以p命名空间为属性赋值:xmlns:p="http://www.springframework.org/schema/p"
p命名空间赋值示例
<bean id="person" class="Spring.Person" p:age="21" p:dogs-ref="dogs" p:name="Db"/><!--Spring2.4以后引入了p命名空间-->
工厂方法注入(很少使用,不推荐)
五、Spring中Bean的相互引用
组成应用程序的Bean需要访问其他Bean时需要相互引用,我们可以通过<ref>元素或者ref属性来实现Bean之间的引用,也可以使用内部Bean的形式实现引用。
public class Person {
private String name;
private int age;
private Dog dog;
public Person(String name, int age, Dog dog) {
this.name = name;
this.age = age;
this.dog = dog;
}
public 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;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", dog=" + dog +
'}';
}
}
ref属性实现Bean的引用
<bean id="person" class="Spring.Person">
<property name="name" value="DB"/>
<property name="age" value="24"/>
<property name="dog" ref="dog"/><!--指向id为dog的Bean-->
</bean>
内部Bean实现引用(内部Bean不用指定Bean的Id,内部Bean不可被外部引用)
<bean id="person" class="Spring.Person">
<property name="name" value="DB"/>
<property name="age" value="24"/>
<property name="dog">
<bean class="Spring.Dog">
<constructor-arg value="Test" index="0" type="java.lang.String"/>
<constructor-arg value="12" index="1"/>
<constructor-arg value="公" index="2"/>
</bean>
</property>
</bean>
六、Null值和级联属性
通过<null/>给属性赋null值,其实也可以不写
<bean id="person" class="Spring.Person">
<property name="name" value="DB"/>
<property name="age" value="24"/>
<property name="dog">
<bean class="Spring.Dog">
<constructor-arg value="Test" index="0" type="java.lang.String"/>
<constructor-arg value="12" index="1"/>
<constructor-arg><null/></constructor-arg>
</bean>
</property>
</bean>
为级联属性赋值
<bean id="person" class="Spring.Person">
<constructor-arg value="Db"/>
<constructor-arg value="20"/>
<constructor-arg ref="dog"/>
<property name="dog.name" value="Test2"/><!--为级联属性赋值-->
</bean>
七、集合属性赋值
1、List集合属性
<bean id="person" class="Spring.Person">
<property name="name" value="Db"/>
<property name="age" value="21"/>
<property name="dogs">
<list>
<ref bean="dog"/>
<ref bean="dog"/>
<ref bean="dog"/>
<ref bean="dog"/>
</list>
</property>
</bean>
2.Map集合属性
<bean id="person" class="Spring.Person">
<property name="name" value="Db"/>
<property name="age" value="21"/>
<property name="dogs">
<map>
<entry key="1" value-ref="dog"/>
<entry key="2" value-ref="dog"/>
<entry key="3" value-ref="dog"/>
</map>
</property>
</bean>
3.配置Properties集合属性
public class DataSouce {
private Properties properties;
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "DataSouce{" +
"properties=" + properties +
'}';
}
}
<!--配置Properties属性值-->
<bean id="dataSource" class="Spring.DataSouce">
<property name="properties">
<props>
<prop key="name">DB</prop>
<prop key="age">21</prop>
</props>
</property>
</bean>
4.配置单例的集合以便多个Bean引用(以list为例)
<util:list id="dogs">
<ref bean="dog"/>
<ref bean="dog"/>
<ref bean="dog"/>
<ref bean="dog"/>
</util:list>
<bean id="person" class="Spring.Person">
<property name="name" value="Db"/>
<property name="age" value="21"/>
<property name="dogs" ref="dogs"/>
</bean>
八、Bean自动装配
Spring IOC容器可以自动装配Bean,需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式,自动装配不够灵活,很少使用。还可以通过扫描包,注解的方式来实现自动装配,后面会介绍到。
- byType (根据类型自动装配):若容器中有多个与目标Bean类型相同的Bean。在这种情况下,Spring将无法装配。
- byName(根据名称自动装配):必须将目标Bean的名称和属性名设置得完全相同。
- constructor (通过构造器自动装配):当Bean中存在多个构造器时,此种自动装配方式将会很复杂,不推荐使用。
<bean id="address" class="Spring.Address" p:city="ChengDu" p:street="KuangZhai"/>
<bean id="dog" class="Spring.Dog" p:name="Test" p:age="3" p:sex="公"/>
<bean id="person" class="Spring.Person" p:age="21" p:name="DB" autowire="byName"/><!--根据Bean的id 和setter中属性名字相同来配置,若有匹配则装配,若无匹配,则不装配-->
<bean id="address" class="Spring.Address" p:city="ChengDu" p:street="KuangZhai"/>
<bean id="dog" class="Spring.Dog" p:name="Test" p:age="3" p:sex="公"/>
<bean id="person" class="Spring.Person" p:age="21" p:name="DB" autowire="byType"/><!--根据Bean的类型和当前Bean属性的类型进行装配->
九、Bean之间的关系
1.Bean之间的继承
Bean之间的继承通过parent属性来制定其父类,子类配置了的属性按照子类属性的值配置,没配置的属性则按照父类配置来配置(即子类可以覆盖来自父类的继承)。若Bean的class没有指定则必须是一个抽象Bean。
<!--absract抽象Bean不可以被实例化,只可被继承 父类也可以不是抽象Bean-->
<bean id="address" class="Spring.Address" p:city="ChengDu" p:street="KuangZhai" abstract="true"/>
<bean id="address2" class="Spring.Address" p:street="ChunXi" parent="address"/>
2.Bean之间的依赖
Bean之间的依赖通过depends-on属性来设置,如果被依赖对象如果为设置,则程序会报错。前置依赖的Bean会在本Bean实例化之前创建好,如果需要依赖于多个Bean的话,则可以通过逗号或者空格隔开依赖Bean的Id即可。
<bean id="address" class="Spring.Address" p:city="ChengDu" p:street="KuangZhai" abstract="true"/>
<bean id="address2" class="Spring.Address" p:street="ChunXi" parent="address"/>
<bean id="dog" class="Spring.Dog" p:name="Test" p:age="3" p:sex="公"/>
<bean id="pearson" class="Spring.Person" depends-on="dog" p:age="21" p:address-ref="address2" p:name="DB"/>
十、Bean的作用域
所有的Bean在不指定scope时都是单例的(即IOC只会为Bean对象创建一个实例对象,每次获取实例都为一个对象)。
作用域类型:
-
prototype 原型的,每次获取实例都会产生一个新的实例对象
-
singleton 单例类型,默认类型
-
request
-
session
<bean id="address" class="Spring.Address" scope="prototype" p:city="ChengDu" p:street="KuangZhai" abstract="true"/>
十一、Bean的使用外部属性文件
1.新建一个properties文件
user = root
password = 1230
driverclass = com.mysql.jdbc.briver
jdbcurl = jdbc:mysql://test
2.在xml文件中使用外部属性文件
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="Spring.DateSource">
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
<property name="driverclass" value="${driverclass}"/>
<property name="jdbcurl" value="${jdbcurl}"/>
</bean>
注意:在使用context时添加命名空间的方法如下。
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd"<!--后面两条必须要交不然会报错-->
十二、Spring表达式语言SpEL
SpEL是一个支持运行时查询和操作对象图的强大的表达式语言,语法类似于EL,SpEL使用#{}作为界定符,所有在大括号中的字符都将认为是 SpEL。
SpEL作用:
- 通过bean的id对bean进行引用
- 调用方法以及引用对象的属性值
- 计算表达式的值
- 正则表达式的匹配
<!--引用其他对象的值-->
<property name="user" value="#{user}"/>
<!--引用其他对象的属性值-->
<property name="user" value="#{user.name}"/>
<!--调用其他对象的方法 支持链式调用-->
<property name="user" value="#{user.toString()}"/>
<!--支持逻辑运算符 and or not -->
<property name="isMan" value="not person.sex"/>
<!--支持三目运算 ?:-->
<property name="sex" value="person.isman ? "男":"女""/>
<!--运算符-->
<property name="age" value="#{person.age * 10}"/>
<!--正则表达式-->
<property name="email" value="#{admin.email matches '^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$'}"/>
十三、Bean的生命周期
Spring IOC容器对Bean的生命周期进行管理的过程:
- 通过构造器或工厂方法创建Bean实例
- 为Bean的属性设置值和对其他Bean的引用
- 调用Bean的初始化方法
- 使用Bean
- 当容器关闭时,调用Bean的销毁方法
在Bean的声明里设置init-method和destroy-method属性,为Bean指定初始化方法和销毁方法
<bean id="dog" class="Spring.Dog" p:name="Test" p:age="3" p:sex="公" init-method="init" destroy-method="destroy"/>
public class Dog {
private String name;
private int age;
private String sex;
private void init(){
System.out.println("Dog init"); //初始化方法
}
private void destroy(){
System.out.println("Dog destroy");//销毁方法
}
public 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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
创建Bean后置处理器:
它允许在调用初始化方法的前后对Bean进行额为的处理,Bean后置处理器对IOC容器里的所有Bean实例逐一处理,而非单一操作。其典型作用:检查Bean的属性的正确性或根据特定的标准更改Bean的属性。
public class MBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throw BeansException {
System.out.println("postProcessBeforeInitialization:"+beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization:"+beanName);
return bean;
}
}
在xml中配置Bean的后置处理器
<!--配置Bean的后置处理器-->
<bean class="Spring.MBeanPostProcessor"/>
十四、配置Bean的多种方式
-
xml中配置Bean(前面已经用过很多次了不做讲解了)
-
工厂方式配置Bean
-
FactoryBean配置Bean
-
注解方式配置Bean
1.静态工厂方法配置Bean
public class StaticDogFactory {
private static Map<String,Dog> dogs = new HashMap<>();
static {
dogs.put("Test1",new Dog("Test1",3,"公"));
dogs.put("Test2",new Dog("Test2",3,"公"));
dogs.put("Test3",new Dog("Test3",3,"公"));
dogs.put("Test4",new Dog("Test4",3,"公"));
}
//静态工厂方法
public static Dog getDog(String name){
return dogs.get(name);
}
}
<!--配置静态工厂方法来配置Bean-->
<bean id="dog1" class="Spring.StaticDogFactory" factory-method="getDog"><!--factory-method指向获取实例的方法-->
<constructor-arg value="Test1"/><!--获取实例方法需要传递的参数-->
</bean>
2.实例工厂方法配置Bean(需要先配置实例工厂对象本身)
public class InstanceDogFactory {
//实例工厂方法
private Map<String,Dog> dogs = null;
public InstanceDogFactory(){
dogs = new HashMap<>();
dogs.put("Test1",new Dog("Test1",3,"公"));
dogs.put("Test2",new Dog("Test2",3,"公"));
dogs.put("Test3",new Dog("Test3",3,"公"));
dogs.put("Test4",new Dog("Test4",3,"公"));
}
public Dog getDog(String name){
return dogs.get(name);
}
}
<!--配置实例工厂方法来配置Bean-->
<bean id="dogFactory" class="Spring.InstanceDogFactory"/>
<bean id="dog2" factory-bean="dogFactory" factory-method="getDog">
<constructor-arg value="Test2"/>
</bean>
3.通过FactoryBean
public class DogFactoryBean implements FactoryBean<Dog> {
private String name;
public void setName(String name) {
this.name = name;
}
@Override
public Dog getObject() throws Exception {
return new Dog(name,3,"公");
}
/**
* 返回对象的类型
*/
@Override
public Class<?> getObjectType() {
return Dog.class;
}
/**
* 是否为单实例
*/
@Override
public boolean isSingleton() {
return true;
}
}
<bean id="dog" class="Spring.DogFactoryBean"><!--class 指向FactoryBean全类名-->
<property name="name" value="Test"/>
</bean>
4.通过注解的方式配置Bean(目前使用较为广泛)
当在组件类上使用了特定的注解之后,还需要在Spring的配置文件中声明<context:component-scan>:
-
base-package属性指定一个需要扫描的基类包,Spring容器将会扫描这个基类包里及其子包中的所有类
-
当需要扫描多个包时,可以用逗号隔开
-
如果仅需要扫描特定的类而非基包下的所有类,可使用resource-pattern属性过滤特定的类
-
<context:include-filter> 子节点表示要包含的目标类
-
<context:exclude-filter> 子节点表示要排除的目标类
-
<context:component-scan>下可以包含多个<context:include-filter>和<context:exclude-filter>
特定组件包括:
-
@Component:基本注解,标识了一个受Spring管理的组件
-
@Respository:标识持久层组件
-
@Service:标识服务层(业务层)组件
-
@Controller:标识表现层组件
<context:include-filter>和<context:exclude-filter>tepy类型:
-
annotation通过注解判断是否包含(即不包含那种类型的注解)
-
assignable指定不包含具体类
<!--指定spring IOC容器扫描的包-->
<context:component-scan base-package="Spring.annotation"<!--指定扫描的包-->
resource-pattern="repository/*.class"><!--resource-pattern指定了只扫描repository包下的类-->
<context:exclude-filter type="assignable" expression="Spring.annotation.repository.UserRepository"/><!--指定不包含具体类-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/><!--指定不包含Repository注解的类-->
</context:component-scan>
组件配置
<context:component-scan>元素还会自动注册AutoWiredAnnotationBeanPostProcessor(Bean后置处理器)实例,该实例可以自动装配具有@Autowired和@Resource、@Inject注解的属性。@Resource和 @Inject与@Autowired作用类似,所以建议使用@Autowired。
@Autowired:注解自动装配具有兼容类型的单个Bean属性构造器,普通字段,一切具有参数的方法都可以应用@Autowired注解(@Autowired注解的对象必须是一个允许自动装配的类,或者设置@Autowired的required值为false,允许装配不上,如果被装配的类有多个实现,就会根据其名字去查询装配对象,如果未指定名字则会报错,还可以通过在@Autowired注解下加@Qualifier("BeanClassName")来指定装配的Bean的类型)。
@Controller
public class UserController {
//@Autowired(required = false)
//@Qualifier("UserRepositoryIpml")
private UserService userService;
@Autowired(required = false) //即使装配不上也不报错,设置为null
@Qualifier("UserRepositoryIpml")
public void setUserService(UserService userService) {
this.userService = userService;
}
// public void setUserService(@Qualifier("UserRepositoryIpml") UserService userService) {
// this.userService = userService;
// }
public void execute(){
System.out.println("UserController execute...");
userService.add();
}
}
@Repository("userRepository")//为Bean指定id
public class UserRepositoryIpml implements UserRepository {
@Override
public void save() {
System.out.println("UserRepositoryIpml save...");
}
}
十五、Spring AOP面向切面编程
AOP:是一种新的方法论,是对传统OOP(面向对象编程)的补充,在使用AOP编程时,仍然需要定义公共功能,并且不必修改影响的类。这样一来横切关注点就被模块化到特殊的对象(切面)里了。好处:1、每个事物逻辑位于一个位置,代码不分散,便于维护和升级。2、业务模块更简洁,只包含核心业务代码,实现了解耦合。
示例需要用到的类
public interface ArithmeticCalculator {
int add(int i,int j);
int sub(int i,int j);
int mul(int i,int j);
int div(int i,int j);
}
@Component
public class ArithmeticCalculatorIpml implements ArithmeticCalculator {
@Override
public int add(int i,int j) {
int result = i+j;
return result;
}
@Override
public int sub(int i,int j) {
int result = i-j;
return result;
}
@Override
public int mul(int i,int j) {
int result = i*j;
return result;
}
@Override
public int div(int i,int j) {
int result = i/j;
return result;
}
}
1.动态代理方式实现日志(开发时不常使用)
/**
* 动态代理
*/
public class ArithmeticCalculatorLoggingProxy {
//要代理的对象
private ArithmeticCalculator target;
public ArithmeticCalculatorLoggingProxy(ArithmeticCalculator target){
this.target = target;
}
public ArithmeticCalculator getLoggingProxy(){
ArithmeticCalculator proxy = null;
//类加载器 用于加载被代理对象
ClassLoader loader = target.getClass().getClassLoader();
//被代理对象实现了哪些接口 具有哪些方法
Class [] interfaces = new Class[]{ArithmeticCalculator.class};
//当调用代理对象其中的方法时,该执行的具体方法
InvocationHandler handler = new InvocationHandler() {
/**
* @param proxy 正在返回的那个代理对象,一般情况下,在invoke中都不实用该对象
* @param method 正在被调用的方法
* @param args 调用方法时传入的参数
* @return
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String methodName = method.getName();
//日志
System.out.println("The method:"+methodName+" begins with "+ Arrays.asList(args));
//执行方法
Object result = method.invoke(target,args);
//日志
System.out.println("The method:"+methodName+" ends with "+ result);
return result;
}
};
proxy = (ArithmeticCalculator) Proxy.newProxyInstance(loader,interfaces,handler);
return proxy;
}
}
//使用动态代理
ArithmeticCalculator target = new ArithmeticCalculatorIpml();
ArithmeticCalculator proxy = new ArithmeticCalculatorLoggingProxy(target).getLoggingProxy();
System.out.println(proxy.add(1,2));
2.Spring AOP通知
AspectJ:java社区里最完整最流行的AOP框架,在Spring2.0以后可以使用。IDEA在使用AspectJ要先通过jar包的方式导入。
AspectJ支持5中类型的通知注解:
-
@Before 前置通知,在方法执行前调用
-
@After 后置通知,在方法区执行后调用
-
@AfterRunning 返回通知,在方法返回结果之后执行
-
@AfterThrowing 异常通知,在方法抛出异常后执行
-
@Around 环绕通知,围绕着方法执行
1.使用AspectJ创建切面
创建切面类
/**
* 日志切面
*/
@Aspect //声明其为一个切面
@Component
public class LoggingAspect {
//申明该方法为一个前置通知
@Before(value = "execution(public int Spring.aop.ArithmeticCalculatorIpml.*(..))")//匹配所有参数类型的方法
//@Before(value = "execution(public int Spring.aop.ArithmeticCalculatorIpml.*(..))")//匹配指定参数类型的方法
//@Before(value = "execution(public int Spring.aop.*.*(..))")//匹配包下的所有类的所有方法
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method "+methodName+" begins "+args);
}
}
在xml中启动Aspect
<!--Aspect自动为匹配的类生产代理对象-->
<aop:aspectj-autoproxy/>
2.切面优先级
通过@Order注解给切面添加优先级,值越小优先级越高
@Order(1)
@Aspect //声明其为一个切面
@Component
public class LoggingAspect {
//申明该方法为一个前置通知
@Before(value = "execution(public int Spring.aop.ArithmeticCalculatorIpml.*(..))")//匹配所有参数类型的方法
//@Before(value = "execution(public int Spring.aop.ArithmeticCalculatorIpml.*(..))")//匹配指定参数类型的方法
//@Before(value = "execution(public int Spring.aop.*.*(..))")//匹配包下的所有类的所有方法
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method "+methodName+" begins "+args);
}
}
3.重用切点表达式
@Aspect //声明其为一个切面
@Component
public class LoggingAspect {
@Pointcut("execution(public int Spring.aop.ArithmeticCalculatorIpml.*(..))")
public void AspectPoint(){}
//申明该方法为一个前置通知
@Before(value = "AspectPoint()")//匹配所有参数类型的方法
//@Before(value = "execution(public int Spring.aop.ArithmeticCalculatorIpml.*(..))")//匹配指定参数类型的方法
//@Before(value = "execution(public int Spring.aop.*.*(..))")//匹配包下的所有类的所有方法
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method "+methodName+" begins "+args);
}
@After(value = "AspectPoint()")//匹配所有参数类型的方法
public void afterMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method "+methodName+" ends");
}
}
十六、Spring 支持JDBC操作数据库
1.连接数据库
c3p0连接池 jar包c3p0下载地址
注意:c3p0下的mchange-commons-java.jar,c3p0-oracle-thin-extras.jar,c3p0.jar这三个jar包都需要导入
junit 单元测试 jar包junit下载地址
mysql-connector-java下载地址
创建外部文件存储连接数据库的相关数据
jdbc.user=你的用户名
jdbc.password=你的密码
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://127.0.0.1:3306/spring4?serverTimezone=UTC //后面serverTimezone=UTC这个表示设置时区 spring4是我的数据库名称
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
配置Bean文件
<!--导入资源文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--配置C3P0数据源-->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"/>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
</bean>
创建单元测试测试是否连通
public class JdbcTest {
private ApplicationContext mContext = null;
{
mContext = new ClassPathXmlApplicationContext("beans-jdbc.xml");
}
@Test
public void Test(){
DataSource dataSource = mContext.getBean(DataSource.class);
try {
System.out.println(dataSource.getConnection());
} catch (SQLException e) {
e.printStackTrace();
}
}
}
当控制面板输出com.mchange.v2.c3p0.impl.NewProxyConnection@48524010 [wrapping: com.mysql.cj.jdbc.ConnectionImpl@4b168fa9]类似这种信息时表示连接数据库成功。
2.使用jdbcTemplate操作数据库
会使用到的类
public class User {
private String username;
private int id;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
配置jdbcTemplate的Bean
<!--配置Spring的jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
使用jdbcTemplate更新数据库
public void testUpdate(){
jdbcTemplate = (JdbcTemplate) mContext.getBean("jdbcTemplate");
String sql = "UPDATE user SET username = ? WHERE id = ?";//问号表示需要传入的参数
jdbcTemplate.update(sql,"name2",1);
}
使用jdbcTemplate插入数据
public void testInsert(){
jdbcTemplate = (JdbcTemplate) mContext.getBean("jdbcTemplate");
String sql = "INSERT INTO user(username,id) VALUES (?,?)";
List<Object[]> batchArgs = new ArrayList<>();
batchArgs.add(new Object[]{"name3",2});
batchArgs.add(new Object[]{"name4",3});
batchArgs.add(new Object[]{"name5",4});
jdbcTemplate.batchUpdate(sql,batchArgs);
}
使用jdbcTemplate查询指定数据
public void testQueryForObject(){
jdbcTemplate = (JdbcTemplate) mContext.getBean("jdbcTemplate");
String sql = "SELECT * FROM user WHERE id = ?";
RowMapper<User> rowMapper = new BeanPropertyRowMapper<>(User.class);
User user = jdbcTemplate.queryForObject(sql,rowMapper,1);
System.out.println("username:"+user.getUsername());
}
使用jdbcTemplate查询所有表内所有信息
public void testQueryForAll(){
jdbcTemplate = (JdbcTemplate) mContext.getBean("jdbcTemplate");
String sql = "SELECT * FROM user";
RowMapper<User> rowMapper = new BeanPropertyRowMapper<>(User.class);
List<User> users = jdbcTemplate.query(sql,rowMapper);
System.out.println("username:"+users.toString());
}
十七、Spring事务管理
事务就是一系列的动作,它们都被当作一个单独的工作单元,这些动作要么全部完成,要么全部不起作用,当某一任务出错后我们需要回滚数据。
基于注解方式声明事务
1.配置事务管理器
<!--配置事物-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--启用事物注解-->
<tx:annotation-driven transaction-manager="transactionManager"/>
2.通过注解声明事务
通过@Transactional为方法添加上事物注解
3.事务的传播行为(即一个事物被另一个事物引用时的行为)
事务的传播行为
-
REQUIRED_NEW:表示当前方法必须在其自己的事务中运行,一个新的事务将会被启动。如果存在当前事务,在该方法执行期间,当前事务会被挂起。
-
REQUIRED:表示当前方法必须运行在事务中,如果当前事务存在,方法将会在该事物中运行。否则,会启动一个新的事务。
-
SUPPORTS:表示当前方法不需要事务上下文,但是如果存在当前事务的话,那么该方法会在这个事务中运行。
-
MANDATORY:表示该方法必须在事务中运行,但如果当前事务不存在,则会抛出一个异常。
-
NOT_SUPPORTED:表示该方法不应该运行在它自己的事务中。如果存在当前事务,在该方法运行期间,当前事务将被挂起。
-
NEVER:表示当前方法不应该运行在事务上下文中,如果当前正有一个事务在运行,则抛出异常。
-
NESTED:表示如果当前已经存在一个事务,那么该方法将会嵌套在事务中运行,嵌套事务可以独立于当前事务进行单独的提交或回滚,如果当前事务不存在,那么其行为与REQUIRED一样。
4.通过propagation指定事务的传播行为,其默认值为REQUIRED
@Transactional(propagation = Propagation.REQUIRED)
5.使用isolation指定事务的隔离级别,最常用的取值为READ_COMMITTED
默认情况下Spring的声明事务将对所有的运行时异常进行回滚,可以通过对应属性进行设置