Spring配置文件、配置方法

Spring配置文件、配置方法

标签(空格分隔): Spring


bean属性的取值

  1. scope
    1.1 singleton

    • Bean实例化个数:1个
    • Bean的实例化时机:当Spring核心文件被加载时,实例化配置的Bean实例
    • Bean的生命周期:
      • 对象创建:当应用加载,创建容器时,对象被创建
      • 对象运行:只要容器存在,对象一直活着
      • 对象销毁:当应用卸载,销毁容器时,对象被销毁

    1.2 prototype

    • Bean的实例化个数:多个
    • Bean的实例化时机:当调用getBean()方法时实例化Bean
      • 对象创建:当使用对象时,创建新对象实例
      • 对象运行:只要对象在使用,就一直活着
      • 对象销毁:当对象长时间不用时,被Java的垃圾回收器回收了

Bean生命周期配置

1. init-method: 指定类中的初始化方法
2. destroy-method:指定勒种的销毁方法名称

Bean实例化的三种方式

  1. 无参构造方法实例化
  2. 工厂静态方法实例化
  3. 工厂实例方法实例化

Bean的依赖注入

例:将UserDao注入到UserService内部 ```
1.通过构造方法

    构造注入:
    
<bean id=”xxx” class=”xxx”>

    <!--当中间为空的时候,默认无参构造
    ref为bean的id名,name构造内部的参数名-->
    <constructor-arg name=”userDao” ref=”userDao”></constructor-arg>
</bean>
2.set方法
<bean id="userService" class="com.allwinter.service.impl.UserServiceImpl">
    <constructor-arg name="userDao" ref="userDao" ></constructor-arg>
</bean>
constructor-arg中的:

name="xxx",xxx为userService中被注入对象set方法后的名称(首字母小写)

ref="xxx",注入目标(就是上面bean标签中已经存在的被注入对象的id名)
还可以通过命名空间来进行依赖注入:
<bean id=”userService” class=”com.allwinter.service.impl.UserServiceImpl” p:userDao-ref=”userDao”/>

其中p就是命名空间定义的:
<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
将普通数据类型依赖注入
<bean id=”xxx” class=”xxx”>
<!--当注入类型为普通数据类型时,使用value来进行注入-->
<constructor-arg name=”username” value=”allwinter””></constructor-arg>
<constructor-arg name=”age” value=”18””></constructor-arg>

<!--集合类型的注入 -->
<property name=”strlist”>
<list>
<value>allwinter</value>
<value>LOVE</value>
<value>you</value>
</list>
</property>
<property name=”usermap”>
<map>
<!-- value-ref是其他bean的id -->
<entry key=”user1” value-ref=”user1”></entry>
</map>
</property>
<property name=”properties”>
<props>
<prop key=”p1”>pp1</prop>
<prop key=”p2”>pp2</prop>
</props>
</property>

</bean>

Spring的重点API

ApplicationContext app = new ClasspathXmlApplication("xml文件");
//第一种
app.getBean("id");
//第二种
app.getBean(Class);
--ClassPathXmlApplicationContext:它是从类的根目录下加载配置文件(推荐使用)
Application app = new ClassPathXmlApplicationContext(“application.xml”);

--FileSystemXmlApplicationContext:它是从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置
ApplicationContext app = new FileSystemApplicationContext(“D:\\allwinter\\xxx”);

--AnnotationConfigApplicationContext:当使用注解配置容器对象时,需要使用此类来创建spring容器,它用来读取注解
getBean()方法
public Object getBean(String name) throws BeansException

允许在容器中出现多个向同类型的Bean
-->因为该方法是使用bean中的id进行查找并返回对象,所以可以出现相同类型的bean(因为相同类但不同id算是不同的对象)
例:UserService userservice = (UserService)app.getBean(“userservice1”);

public <T> T getBean(Class<T> requiredType) throws BeansException
只能出现一个类型的Bean
--->因为该方法是使用类进行查找,只能返回一个该类的对象
例;UserService userservice = app.getbean(UsreService.class);

×当xml中,该类的bean只存在一个时,可以使用泛型方法getbean(Class)
若该类的bean存在多个,则只能使用id查找的getbean(“id”)方法

Spring配置数据源

数据源(连接池)的作用:

  • 数据源是提高程序性能出现的
  • 事先实例化数据源,初始化部分连接资源
  • 使用连接资源时从数据源中获取
    -使用完毕后将连接资源归还给数据源

初代版本:

<bean id”dataSource” class=”com.mchange.v2.c3p0.ComboPoolrdDataSource”>
<property name=”driverClass” value=”com.ntsql.cj.jdbc.Driver”></property>
<property name=”jdbcUrl” value=”jdbc:mysql://locahost:3306/test”></property>
<property name=”user” value=”root”></pproperty>
<property name=”password” value=”Yxs-2020”></property>
</bean>

二代版本:通过配置文件解耦!!!

相关信息:
注:其中name属性的值是set方法后的名字,不同的连接池各有不同,需要修改,
如: c3p0中的Driver为driverClass
druid中的Driver为driverClassName

(
Java获取properties文件:ResourceBundle rb = new ResourceBundle.getBundle(“jdbc”);
其中,getBundle方法的参数是:resources文件夹下的properties文件的文件名
)

---->通过spring直接获取已经配置好的DataSource对象
Application app = new ClassPathXmlApplicationContext(“applicationContext.xml”);
DataSource dataSource = app.getBean(DataSource.class);
Connection connection = dataSource.getConnection();

Spring容器加载properties配置文件(例如解耦上面的jdbc)
首先修改xml上的命名空间:
<bean xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/beans/spring-context.xsd"/>

<context:properties-placeholder location=”xx.properties”/>
<bean>
<property name=”” value=”${key}”/>
<bean>

例:
<bean id”dataSource” class=”com.mchange.v2.c3p0.ComboPoolrdDataSource”>
<property name=”driverClass” value=”${jdbc.driver}”></property>
<property name=”jdbcUrl” value=”${jdbc.url}”></property>
<property name=”user” value=”${jdbc.username}”></pproperty>
<property name=”password” value=”${jdbc.password}”></property>
</bean>
AOP的动态代理技术

常用的动态代理技术:

- JDK代理:基于接口的动态代理技术
- cglib代理:基于父类的动态代理技术

此处输入图片的描述

AOP相关概念

Spring的AOP实现底层就是对上面的动态代理的代码进行了封装,封装后我们只需要对需要挂住的部分进行代码编写,并通过配置的方式完成指定目标的方法增强

  • Target(目标对象);代理的目标对象

  • Proxy(代理):一个类被AOP织入增强后,就产生一个结果代理类

  • Joinpoint(连接点):所谓连接点是指那些被拦截到的点。在Spring中,这些点指的是方法,因为Spring只支持方法型连接点(就是可以被增强的方法)

  • Pointcut(切入点):所谓切入点是指我们要对那些Joinpoint进行拦截的定义(就是实际上要被增强的方法)

  • Advice(通知/增强):所谓通知是指拦截到Joinpoint之后所要做的事情就是通知

  • Aspect(切面):是切入点和通知(引介)的结合(就是实际上已被增强的方法)

  • Weaving(织入):是指把增强应用到目标对象来创建新的代理对象的过程。spring采用动态代理织入,而Aspect采用编译期织入和类装载期织入。

XML配置AOP详解

首先配置命名空间

    <bean xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd />

切点表达式的写法

表达式语法:
excution[修饰符] 返回值类型.包名.类名.方法名(参数)

  • 访问修饰符可以省略
  • 返回值类型、包名、类名、方法名可以使用星号(*)代表任意
  • 包名与类名之间一个点,代表当前包下的类,两个点,表示当前类及其子包下的类
  • 参数列表可以使用两个点,表示任意个数,任意类型的参数列表
 <!--配置织入,告诉spring框架,告诉那些方法(切点)需要进行那些增强(前置、后置...)-->
    <aop:config>
<!--        声明切面-->
        <aop:aspect ref="myAspect">
<!--            切面:切点+通知-->

<!--            <aop:before pointcut="execution(public  void com.allwinter.aop.Target.save())" method="before"/>   &lt;!&ndash;前置增强设置&ndash;&gt;-->
            <aop:before pointcut="execution(* com.allwinter.aop.*.*(..))" method="before"/>   <!--前置增强-->
            <aop:after method="after" pointcut="execution(* com.allwinter.aop.*.*(..))"/>
        </aop:aspect>
    </aop:config>

此处输入图片的描述

此处输入图片的描述

各种增强:

 <aop:aspect ref="myAspect">
<!--            切面:切点+通知-->

<!--            <aop:before pointcut="execution(public  void com.allwinter.aop.Target.save())" method="before"/>   &lt;!&ndash;前置增强设置&ndash;&gt;-->
            <aop:before method="before" pointcut="execution(* com.allwinter.aop.*.*(..))"/>   <!--前置增强-->
            <aop:before method="before2" pointcut="execution(* com.allwinter.aop.*.*(..))"/>	
            <aop:after method="after" pointcut="execution(* com.allwinter.aop.*.*(..))"/> <!--最终增强-->
            <aop:around method="around" pointcut="execution(* com.allwinter.aop.*.*(..))"/> <!--环绕增强-->
            <aop:after-throwing method="afterThrowing" pointcut="execution(* com.allwinter.aop.*.*(..))"/> <!--异常抛出增强-->
        </aop:aspect>

其中:环绕增强需要传入当前切点为参数

//ProceedingJoinPoint : 正在执行的连接点 == 切点
public Object around(ProceedingJoinPoint pjp) throws Throwable {
    System.out.println("环绕前增强...");
    Object proceed = pjp.proceed();
    System.out.println("环绕后增强...");
  return proceed;
}

当切点表达式有较多重复时,可以通过aop:pointcut进行抽取

<aop:pointcut id="myPointcut" expression="execution(* com.allwinter.aop.*.*(..))"/>
<aop:before method="before" pointcut-ref="myPointcut"/>
使用注解进行aop的配置

此处输入图片的描述

此处输入图片的描述

//定义切点表达式
@Pointcut("execution(* com.allwinter.anno.*.*(..))")
public void pointcut(){}

//使用
@Around("pointcut()")

需要修改命名空间,对bean进行组件扫描,对aop进行组件扫描

<?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
">
    
    <context:component-scan base-package="com.allwinter.anno"/>
    <aop:aspectj-autoproxy/>
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值