怎么通过注解配置Bean

1.什么是通过注解配置bean

通过注解配置bean是一种使用注解来替代传统的XML配置方式,在Spring框架中定义和配置应用程序中的对象(即bean)。通过使用特定的注解,我们可以将Java类或方法标记为bean,并告诉Spring如何创建和管理它们。

使用注解配置bean的好处包括:

简化配置:相比传统的基于XML的配置方式,注解配置更简洁直观,可以更快速地定义和配置bean。
提高可读性:注解直接嵌入在Java类或方法中,可以更清晰地表达bean的属性和关系,提高了代码可读性。
减少依赖:不再依赖外部的XML配置文件,避免了配置文件的管理和部署问题。
提供更多功能:注解可以携带更多的元数据信息,比如事务配置、请求映射等,使得开发更加灵活和便捷。
通过使用各种注解,我们可以在类、字段、方法等级别上定义和配置bean,包括声明bean的类型、作用域、依赖关系等。这样,Spring容器就可以根据这些注解来自动实例化、装配和管理bean,简化了开发过程。

2.常见的组件注解的形式

@Component 表示当前注解标识的是一个组件
@Controller 表示当前注解标识的是一个控制器,常用于Servlet类
@Service 表示当前注解标识的是一个处理业务逻辑的类,通常用于Service类
@Repository 表示当前注解标识的是一个持久化层的类,通常用于Dao类

3.快速入门案例

在使用注解配置bean时,首先我们需要引入 spring-aop-5.3.8.jar 包
此包在spring的libs目录下
接下来我们创建几个类,并且给它们添加相应的注解

@Component
public class UserComponent {    
}
@Repository
public class UserDao {
}
@Service
public class UserService {
}
@Controller
public class UserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doPost(request,response);
    }
}

创建好类之后,我们开始写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: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 https://www.springframework.org/schema/context/spring-context.xsd">
    <!--    component-scan:要对指定包下的类进行扫描,并创建对象到容器-->
    <!--    base-package:指定要扫描的包-->
    <context:component-scan base-package="com.spring.component"/>
</beans>

这样,我们就完成了通过注解配置bean,并且对注解进行了扫描
接下来看一下底层
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述可以看到,ioc容器中,已经有了这些类,默认的id是类名首字母小写
让我们写一个测试类测试一下吧!

@Test
    public void beansByAnnotation(){
        //创建容器
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beansByAnnotation.xml");
        //得到对象
        UserServlet userServlet = ioc.getBean("userServlet", UserServlet.class);
        UserComponent userComponent = ioc.getBean("userComponent", UserComponent.class);
        UserDao userDao = ioc.getBean("userDao", UserDao.class);
        UserService userService = ioc.getBean("userService", UserService.class);
        System.out.println(userComponent);
        System.out.println(userServlet);
        System.out.println(userDao);
        System.out.println(userService);
    }

输出以下结果,说明我们的配置成功了
在这里插入图片描述

4.注意事项

Spring的ioc容器不能检查一个使用了@Controller注解的类到底是不是一个真正的控制器,注解的名称是用于程序员自己识别当前标识的是什么组件。也就是说spring的ioc容器只要检查到注解就会生成对象,但是这个注解的含义spring不会识别,注解是给程序员方便看的。
如果我们希望排除某个包/子包下的某种类型的注解,可以通过exclude-filter来指定
看一个例子

<?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 https://www.springframework.org/schema/context/spring-context.xsd">
   <!--    component-scan:要对指定包下的类进行扫描,并创建对象到容器-->
   <!--    base-package:指定要扫描的包-->
   <!--    exclude-filter 指定要排除哪些类-->
   <!--    type指定排除方式 annotation是按照注解排除-->
   <!--    expression指定要排除的注解的全路径-->
   
   <context:component-scan base-package="com.spring.component">
   <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
   </context:component-scan>
</beans>

这样配置过后,ioc容器就不会加载Service注解配置的类了
同理如果我们希望指定某个包/子包下的某种类型的注解,可以通过include-filter来指定。用这个属性有一个前提,需要把默认的use-default-filters设置为false
代码如下:

<context:component-scan base-package="com.spring.component" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>

在配置注解时,可以通过value属性,指定id

@Repository(value = "userDao")
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值