Spring需要的包:Spring-context
<?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.xsd">
</beans>
bean标签
让Spring创建核心容器时,创建配置的类
属性
id:唯一标志,在在spring核心容器中创建对象时将使用ID值
class:需要配置的类的全限定类名
scope:指定对象的生命周期
值:
singleton :默认值,单例的.
prototype :多例的.
request :WEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 request 域中.
session :WEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 session 域中.
global session
:WEB 项目中,应用在 Portlet 环境.如果没有 Portlet 环境那么
globalSession 相当于 session.
init-method:指定类中的初始化方法名称。
destroy-method:指定类中销毁方法名称。
factory-method 属性:指定生产对象的静态方法
factory-bean 属性:用于指定实例工厂 bean 的 id。
factory-method 属性:用于指定实例工厂中创建对象的方法。
Spring实例化对象的三种方式:
使用默认无参构造函数
spring 管理静态工厂使用静态工厂的方法创建对象
spring 管理实例工厂使用实例工厂的方法创建对象
<bean id="instancFactory" class="com.itheima.factory.InstanceFactory">
<bean id="accountService"
factory-bean="instancFactory"
factory-method="createAccountService">
配置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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置spring创建容器要扫描的包-->
<context:component-scan base-package="gx"></context:component-scan>
<!-- 配置spring开启注解AOP的支持-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
@Component:让spring在创建容器时,创建对象,属性:value:指定id
@Controller @Service @Repository:与@Component相同,语义化
改变作用范围:
@Scope :指定 bean 的作用范围;value:指定范围的值。
? 取值:singleton prototype request session globalsession
生命周期:
@PostConstruct:初始化
@PreDestroy :销毁
@Configuration :指定当前类是一个配置类;value:用于指定配置类的字节码
@ComponentScan:指定初始化容器时要扫描的包;basePackages:用于指定要扫描的包
@Bean:该注解只能写在方法上,表明使用此方法创建一个对象,并且放入 spring 容器;name:bean的id
@PropertySource:用于加载.properties 文件中的配置。value[]:用于指定 properties 文件位置,如果是在类路径下,需要写上 classpath:
使用:
@Value("${jdbc.driver}")
private String driver;
@Import :用于导入其他配置类,在引入其他配置类时,可以不用再写@Configuration 注解;value[]:用于指定其他配置类的字节码
注入数据适用于:数据不会频繁改变的值
要求:
? 类中需要提供一个对应参数列表的构造函数。
constructor-arg
属性:
指定参数
index:指定参数在构造函数参数列表的索引位置
type:指定参数在构造函数中的数据类型
name:指定参数在构造函数中的名称
赋值
value:它能赋的值是基本数据类型和 String 类型
ref:它能赋的值是其他 bean 类型,也就是说,必须得是在配置文件中配置过的 bean
要求:有set方法
property
属性:
name:找的是类中 set 方法后面的部分
ref:给属性赋值是其他 bean 类型的
value:给属性赋值是基本数据类型和 string 类型的
配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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="accountService"
class="com.itheima.service.impl.AccountServiceImpl4"
p:name="test" p:age="21" p:birthday-ref="now"/>
</beans>
List 结构的:
array,list,set
Map 结构的
map,entry,props,prop
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<!-- 在注入集合数据时,只要结构相同,标签可以互换 -->
<!-- 给数组注入数据 -->
<property name="myStrs">
<set>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</set>
</property>
<!-- 注入 list 集合数据 -->
<property name="myList">
<array>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</array>
</property>
<!-- 注入 set 集合数据 -->
<property name="mySet">
<list>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</list>
</property>
<!-- 注入 Map 数据 -->
<property name="myMap">
<props>
<prop key="testA">aaa</prop>
<prop key="testB">bbb</prop>
</props>
</property>
<!-- 注入 properties 数据 -->
<property name="myProps">
<map><entry key="testA" value="aaa"></entry>
<entry key="testB">
<value>bbb</value>
</entry>
</map>
</property>
</bean>
@Autowired :自动按照类型注入。当使用注解注入属性时,set 方法可以省略。它只能注入其他 bean 类型
@Qualifier :在自动按照类型注入的基础之上,再按照 Bean 的 id 注入。它在给字段注入时不能独立使用
@Resource:直接按照 Bean 的 id 注入。它也只能注入其他 bean 类型;name:指定 bean 的 id
@Value:注入基本数据类型和 String 类型数据的
获取Spring核心容器:
new ClassPathXmlApplicationContext("bean.xml");
BeanFactory 和 ApplicationContext 的区别
BeanFactory 才是 Spring 容器中的顶层接口。
ApplicationContext 是它的子接口。
BeanFactory 和 ApplicationContext 的区别:
创建对象的时间点不一样。
? ApplicationContext:只要一读取配置文件,默认情况下就会创建对象。
? BeanFactory:什么使用什么时候创建对象
ApplicationContext 接口的实现类
ClassPathXmlApplicationContext**:**
它是从类的根路径下加载配置文件 ,推荐使用这种
FileSystemXmlApplicationContext**:**
它是从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置。
AnnotationConfigApplicationContext:
当我们使用注解配置容器对象时,需要使用此类来创建 spring 容器。它用来读取注解。
导入:spring-test,junit4.12
使用**@RunWith**注解替换原有运行器
使用**@ContextConfiguration** 指定 spring 配置文件的位置
@ContextConfiguration 注解:
locations **属性:**用于指定配置文件的位置。如果是类路径下,需要用 **classpath:**表明
classes **属性:**用于指定注解的类。当不使用 xml 配置时,需要用此属性指定注解类的位置。
使用**@Autowired** 给测试类中的变量注入数据
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:bean.xml"})
public class AccountServiceTest {
@Autowired
private IAccountService as ; }
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
<bean id="logger" class="gx.utils.Logger"></bean>
配置标签,用于声明开始aop的配置
<!--
aop:config:
作用:用于声明开始 aop 的配置
-->
<aop:config>
<!--配置的代码都写着这里-->
</aop:config>
<!--
aop:aspect:
作用:
用于配置切面。
属性:
id:给切面提供一个唯一标识。
ref:引用配置好的通知类 bean 的 id。
-->
<aop:aspect id="txAdvice" ref="txManager">
<!--配置通知的类型要写在此处-->
</aop:aspect>
切入点表达式的写法:
<!--
aop:pointcut:
作用:
用于配置切入点表达式。就是指定对哪些类的哪些方法进行增强。
属性:
expression:用于定义切入点表达式。
id: 用于给切入点表达式提供一个唯一标识
-->
<aop:pointcut expression="execution(
public void com.itheima.service.impl.AccountServiceImpl.transfer(
java.lang.String, java.lang.String, java.lang.Float) )" id="pt1"/>
前置通知:
<!--
aop:before
作用:
用于配置前置通知。指定增强的方法在切入点方法之前执行
属性:
method:用于指定通知类中的增强方法名称
ponitcut-ref:用于指定切入点的表达式的引用
poinitcut:用于指定切入点表达式
执行时间点:
切入点方法执行之前执行
-->
<aop:before method="beginTransaction" pointcut-ref="pt1"/>
后置通知:
<!--
aop:after-returning
作用:
用于配置后置通知
属性:
method:指定通知中方法的名称。
pointct:定义切入点表达式
pointcut-ref:指定切入点表达式的引用
执行时间点:
切入点方法正常执行之后。它和异常通知只能有一个执行
-->
<aop:after-returning method="commit" pointcut-ref="p
异常通知:
<!--
aop:after-throwing
作用:
用于配置异常通知
属性:
method:指定通知中方法的名称。
pointct:定义切入点表达式
pointcut-ref:指定切入点表达式的引用
执行时间点:
切入点方法执行产生异常后执行。它和后置通知只能执行一个
-->
<aop:after-throwing method="rollback" pointcut-ref="pt1"/>
最终通知:
<!--
aop:after
作用:
用于配置最终通知
属性:
method:指定通知中方法的名称。
pointct:定义切入点表达式
pointcut-ref:指定切入点表达式的引用
执行时间点:
无论切入点方法执行时是否有异常,它都会在其后面执行。
-->
<aop:after method="release" pointcut-ref="pt1"/>
环绕通知:
<!--配置方式: -->
<aop:config>
<aop:pointcut expression="execution(* gx.service.impl.*.*(..))" id="pt1"/>
<aop:aspect id="txAdvice" ref="txManager">
<!-- 配置环绕通知 -->
<aop:around method="transactionAround" pointcut-ref="pt1"/>
</aop:aspect>
</aop:config>
<!--
aop:around:
作用:
用于配置环绕通知
属性:
method:指定通知中方法的名称。
pointct:定义切入点表达式
pointcut-ref:指定切入点表达式的引用
说明:
它是 spring 框架为我们提供的一种可以在代码中手动控制增强代码什么时候执行的方式。
注意:
通常情况下,环绕通知都是独立使用的
-->
/**
* 环绕通知
* @param pjp
* spring 框架为我们提供了一个接口:ProceedingJoinPoint,它可以作为环绕通知的方法参数。
* 在环绕通知执行时,spring 框架会为我们提供该接口的实现类对象,我们直接使用就行。
* @return
*/
public Object transactionAround(ProceedingJoinPoint pjp) {
//定义返回值
Object rtValue = null;
try {
//获取方法执行所需的参数
Object[] args = pjp.getArgs();
//前置通知:开启事务
beginTransaction();
//执行方法
rtValue = pjp.proceed(args);
//后置通知:提交事务
commit();
}catch(Throwable e) {
//异常通知:回滚事务
rollback();
e.printStackTrace();
}finally {
//最终通知:释放资源
release();
}
return rtValue;
}
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<!--切入点表达式用到的[aspectj:语言的软件联盟;解析execution表达式] -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
第二部:在配置文件中导入context命名空间
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
把资源使用注解配置
在spring中配置要扫描的包,这也是所有Spring基于开发需要做的;
在通知类上用@Aspect
注解声明这是一个通知类
在增强方法上使用注解配置通知
Spring注解的通知调用顺序有问题
@Before
作用:
把当前方法看成是前置通知。
属性:
value:用于指定切入点表达式,还可以指定切入点表达式的引用。
@AfterReturning
作用:
把当前方法看成是后置通知。
属性:
value:用于指定切入点表达式,还可以指定切入点表达式的引用
@AfterThrowing
作用:
把当前方法看成是异常通知。
属性:
value:用于指定切入点表达式,还可以指定切入点表达式的引用
@After
作用:
把当前方法看成是最终通知。
属性:
value:用于指定切入点表达式,还可以指定切入点表达式的引用
@Around
作用:
把当前方法看成是环绕通知。
属性:
value:用于指定切入点表达式,还可以指定切入点表达式的引用。
@Pointcut
作用:
指定切入点表达式
属性:
value:指定表达式的内容
@Pointcut("execution(* com.itheima.service.impl.*.*(..))")
private void pt1() {}
不适用XMl的配置方式
在类上加入
@EnableAspectJAutoProxy
原文:https://www.cnblogs.com/platinumcat/p/12558187.html