spring之IoC注解(二)

本文详细介绍了如何在Spring中使用注解进行Bean的管理和选择性实例化,包括加入AOP依赖、配置扫描包、常见注解的使用,并提供了解决不同场景下Bean控制的两种策略。

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


前言

1、Spring注解的使用
2、Bean的选择性实例化


一、Spring注解的使用

1、加入aop的依赖

当加入spring-context依赖后,会关联加入aop的依赖

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>

2、在配置文件中添加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"
       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>

3、在配置文件中指定扫描的包

给spring框架指明要扫描哪些包中的类

<context:component-scan base-package="com.powernode.bean"></context:component-scan>

4、在Bean类上使用注解

Vip类:

@Controller("vipBean")
public class Vip {
}

以上的这个注解就相当于以下的配置信息:

<bean id="vipBean" class="com.powernode.bean.Vip">

User类:

//@Component(value = "UserBean")
//如果属性名是value ,value可以省略
@Component("userBean")
public class User {
}

以上的这个注解就相当于以下的配置信息:

<bean id="userBean" class="com.powernode.bean.User">

不指定value值就是默认类名首字母小写
Order类:

@Service
public class Order {
}

以上的这个注解就相当于以下的配置信息:

<bean id="order" class="com.powernode.bean.Order">

Student类:

@Repository
public class Student {
}

以上的这个注解就相当于以下的配置信息:

<bean id="student" class="com.powernode.bean.Student">

测试类:

    @Test
    public void testBeanComponent(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        User userbean = applicationContext.getBean("userBean",User.class);
        Vip vipBean = applicationContext.getBean("vipBean",Vip.class);
        Student studentBean = applicationContext.getBean("student", Student.class);
        Order orderBean = applicationContext.getBean("order", Order.class);
        System.out.println(userbean);
        System.out.println(vipBean);
        System.out.println(studentBean);
        System.out.println(orderBean);
    }

运行结果
在这里插入图片描述
在这里插入图片描述

如果有多个包怎么办?有两种解决方案
1、在配置文件中指定多个包,用逗号隔开
在这里插入图片描述
2、指定多个包的共同父包(但是这肯定牺牲一部分效率)
在这里插入图片描述

二、Bean的选择性实例化

1、需求

假设在某个包下有很多Bean,有的Bean上标注了Component,有的标注了Controller,有的标注了Service,有的标注了Repository,现在由于某种特殊业务的需求,只允许其中所有的Controller参与Bean管理,其他的都不实例化。这应该怎么办?

@Component
public class A {
    public A(){
        System.out.println("A的无参数构造方法执行");
    }
}
@Controller
class B {
    public B() {
        System.out.println("B的无参数构造方法执行");
    }
}
@Service
class C {
    public C() {
        System.out.println("C的无参数构造方法执行");
    }
}
@Repository
class D {
    public D() {
        System.out.println("D的无参数构造方法执行");
    }
}

@Controller
class E {
    public E() {
        System.out.println("E的无参数构造方法执行");
    }
}

2、解决方案

解决方案一:

use-default-filters=“false”
这个属性是false,表示这个包下所有的带有声明Bean的注解全部失效
在这里插入图片描述
<context:include-filter type=“annotation” expression=“org.springframework.stereotype.Controller”/>
设置只有Controller生效
在这里插入图片描述
运行结果:
在这里插入图片描述

解决方案二:

use-default-filters=“true”
这个属性是true,表示这个包下所有的带有声明Bean的注解全部生效
在这里插入图片描述
<context:exclude-filter type=“annotation” expression=“org.springframework.stereotype.Repository”/>
设置只让Component注解生效,Repository、Service、Controller注解失效
在这里插入图片描述
运行结果
在这里插入图片描述

use-default-filters=“true” 默认值是true

完整代码:

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

<!--    解决方案一:-->
<!--    <context:component-scan base-package="com.powernode.bean2" use-default-filters="false">-->
<!--        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>-->
<!--    </context:component-scan>-->


    <!--    解决方案二:-->
    <context:component-scan base-package="com.powernode.bean2" use-default-filters="true">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
</beans>


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值