Spring

本文深入讲解Spring框架的IOC/DI、AOP等核心概念,包括控制反转、依赖注入的实现方式,以及面向切面编程的注解配置和使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Spring IOC/DI (控制反转/依赖注入)

1、创建pojo模型,提供set/get方法

public class Category {
    private int id;
    private String name;
}

 

2、src目录下创建applicationContext.xml是Spring的核心配置文件

该对象获取的时候,即被注入了字符串"category 1“到name属性中

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  
    <bean name="c" class="com.how2java.pojo.Category">
        <property name="name" value="category 1" />
    </bean>
  
</beans>

 

3、编写测试代码

public class TestSpring {
    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
        Category category = (Category) context.getBean("category");
        System.out.println(category.getName());
    }
}

 

4、原理图

以获取对象的方式来进行比较

 

传统的方式:

通过new 关键字主动创建一个对象

IOC方式

对象的生命周期由Spring来管理,直接从Spring那里去获取一个对象。 IOC是反转控制 (Inversion Of Control)的缩写,就像控制权从本来在自己手里,交给了Spring。

 

打个比喻:

传统方式:相当于你自己去菜市场new 了一只鸡,不过是生鸡,要自己拔毛,去内脏,再上花椒,酱油,烤制,经过各种工序之后,才可以食用。

用 IOC:相当于去馆子(Spring)点了一只鸡,交到你手上的时候,已经五味俱全,你就只管吃就行了。

 

二、注入对象

1、Product中创建属性Category

2、applicationContext.xml中添加配置

<bean name="c" class="com.cex.pojo.Category">
        <property name="name" value="category 1" />
    </bean>
    <bean name="p" class="com.cex.pojo.Product">
        <property name="name" value="product1" />
        <property name="category" ref="c" />
    </bean><bean name="c" class="com.cex.pojo.Category">
        <property name="name" value="category 1" />
    </bean>
    <bean name="p" class="com.cex.pojo.Product">
        <property name="name" value="product1" />
        <property name="category" ref="c" />
    </bean>

 

3、创建测试类

public class TestSpring {
    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
        Product product  = (Product) context.getBean("product");
        System.out.println(product.getCategory().getName());
    }
}

 

三、注解方式IOC/DI

1、applicationContext.xml中添加配置,表示告诉Spring要用注解的方式

<context:annotation-config/>

 

2、Product的Category属性上添加注解

@Autowired by type

@Resource by name

@Autowired
private Category category;

 

3、对Bean的注解

applicationContext.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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:annotation-config/>
    <context:component-scan base-package="com.cex.pojo"/>
</beans>

 

4、模型上增加注解

@Component("c")
public class Category

 

四、AOP面向切面编程

1、创建一个业务类

public class ProductService {
    public void doSomeService(){
        System.out.println("doSomeService");
    }
}

 

2、准备日志切面 LoggerAspect

public class LoggerAspect {
    public Object log(ProceedingJoinPoint joinPoint){
        System.out.println("start log "+joinPoint.getSignature().getName() );
        Object object = joinPoint.proceed();
        System.out.println("start log "+joinPoint.getSignature().getName() );
        return object;
    }
}

 

3、配置applicationContext.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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com.cex.pojo"/>
   <!-- <bean name="category" class="com.cex.pojo.Category">
        <property name="name" value="category 1" />
    </bean>

    <bean name="product" class="com.cex.pojo.Product">
        <property name="name" value="product 1"/>
        <property name="category" ref="category" />
    </bean>-->

    <bean name="service" class="com.cex.service.ProductService"/>
    <bean id="loggerAspect" class="com.cex.aspect.LoggerAspect"/>

    <aop:config>
        <aop:pointcut id="loggerCutPoint" expression="execution(* com.cex.service.ProductService.*(..))"/>
        <!--第一个*表示返回任意类型,第二个类型表示任意方法-->
        <aop:aspect id="logAspect" ref="loggerAspect">
            <aop:around method="log" pointcut-ref="loggerCutPoint"/>
        </aop:aspect>
    </aop:config>

</beans>

4、测试类

public class TestSpring {
    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
        Product product  = (Product) context.getBean("p");
        System.out.println(product.getCategory().getName());

        ProductService service = (ProductService)context.getBean("service");
        service.doSomeService();
    }
}

 

五、注解方式AOP

1、注解配置业务类

package com.cex.service;

import org.springframework.stereotype.Component;

@Component("s")
public class ProductService {
    public void doSomeService(){
        System.out.println("doSomeService");
    }
}

 

2、注解配置切面

@Aspect
@Component
public class LoggerAspect {
    @Around(value = "execution(* com.cex.service.ProductService.*(..))")
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("start log "+joinPoint.getSignature().getName() );
        Object object = joinPoint.proceed();
        System.out.println("start log "+joinPoint.getSignature().getName() );
        return object;
    }
}

 

3、applicationContext.xml中增加配置

<context:annotation-config/>
<context:component-scan base-package="com.cex.pojo"/>
<context:component-scan base-package="com.cex.aspect"/>
<context:component-scan base-package="com.cex.service"/>

 

六、注解方式测试

测试类中进行如下配置:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestSpring {
    @Autowired
    Category category;

    @Test
    public void test(){
        System.out.println(category.getName());
    }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值