Spring cheat sheet

依赖

Spring

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.10.RELEASE</version>
</dependency>

控制反转(IoC)

配置文件

<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>

使用xml创建对象

数值和字符串

<bean id="hello" class="com.kuang.pojo.Hello">
    <property name="name" value="Spring"/>
</bean>

对象

<bean id="world" class="com.*">
<bean id="hello" class="com.kuang.pojo.Hello">
    <property name="name" ref="world"/>
</bean>

数组

<bean id="hello" class="com.kuang.pojo.Hello">
    <property name="books">
        <array>
            <value>西游记</value>
            <value>红楼梦</value>
            <value>水浒传</value>
        </array>
    </property>
</bean>

List

<bean id="hello" class="com.kuang.pojo.Hello">
    <property name="hobbys">
        <list>
            <value>听歌</value>
            <value>看电影</value>
            <value>爬山</value>
        </list>
    </property>
</bean>

Map

<bean id="hello" class="com.kuang.pojo.Hello">
    <property name="card">
        <map>
            <entry key="中国邮政" value="456456456465456"/>
            <entry key="建设" value="1456682255511"/>
        </map>
    </property>
</bean>

Set

<bean id="hello" class="com.kuang.pojo.Hello">
    <property name="card">
        <map>
            <entry key="中国邮政" value="456456456465456"/>
            <entry key="建设" value="1456682255511"/>
        </map>
    </property>
</bean>

有参构造

<bean id="hello" class="com.kuang.pojo.Hello">
    <constructor-arg name="name" value="kuangshen2"/>
</bean>

使用自动装配

<beans xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd" >
<context:annotation-config/>

@Autowired
按类型自动匹配,可以使用@Qualifier(value="name")指定装配对象。
@Resource

使用注解开发

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
</beans>

增加扫描包注解

<context:component-scan base-package="*"/>

@Component(“name”)
使用此命令创建名为name的bean。
还有三个注解等价于此注解。

  • @Controller
  • @Service
  • @Repository

以上用于springmvc搭建过程。

@Value(“value”)
使用此命令对变量进行注入。

获取Bean

ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
JobMapper jobMapper = context.getBean("JobDao", JobMapper.class);

面向切面编程(AOP)

面向切面编程使用代理模式实现。

导入依赖

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.4</version>
</dependency>

通过Spring API实现

实现以下接口

  • MethodBeforeAdvice
  • AfterReturningAdvice

xml注册接口

<bean id="log" class="*"/>
<bean id="afterLog" class="*"/>
<aop:config>
    <!--切入点  expression:表达式匹配要执行的方法-->
    <aop:pointcut id="pointcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
    <!--执行环绕; advice-ref执行方法 . pointcut-ref切入点-->
    <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
    <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>

通过自定义类实现AOP

<bean id="diy" class="com.kuang.config.DiyPointcut"/>

<!--aop的配置-->
<aop:config>
    <!--第二种方式:使用AOP的标签实现-->
    <aop:aspect ref="diy">
        <aop:pointcut id="diyPonitcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
        <aop:before pointcut-ref="diyPonitcut" method="before"/>
        <aop:after pointcut-ref="diyPonitcut" method="after"/>
    </aop:aspect>
</aop:config>

通过注解实现

@Aspect
通过此注解将类注册为切面。

@Before(“execution(* com.kuang.service.UserServiceImpl.*(…))”)
设置前置方法。

@After(“execution(* com.kuang.service.UserServiceImpl.*(…))”)
设置后置方法。

@Around(“execution(* com.kuang.service.UserServiceImpl.*(…))”)
设置环绕方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值