美文网首页
4.3 基于 注解 方式管理 Bean

4.3 基于 注解 方式管理 Bean

作者: 大也 | 来源:发表于2023-11-30 10:48 被阅读0次

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

<!--
    1)普通配置包扫描 直接在类名前添加 可以通过 value 设置<bean id
        @Component(value = "tianDog") 3层架构外的使用
        @Controller("ianDog")     Controller
        @Repository      Repository 数据库 dao层
        @Service        Service
    2)package 指定ioc容器去哪些包下查找注解类 -> ioc容器
        一个包或者多个包 com.atguigu.xxx 包1,包2
        指定包 就相当于指定包下所有包
    3)直接使用 ApplicationContext实例化就好  添加ioc注解 默认组件名为 类的首字母小写
        Object xxxService = classPathXmlApplicationContext.getBean("xxxService");
        System.out.println("xxxService" + xxxService);
    4) 指定排除规则
        4.1)use-default-filters属性:取值false表示关闭默认扫描规则
        4.2)context:exclude-filter标签:指定排除规则
        4.3)context:include-filter标签:指定在原有扫描规则的基础上追加的规则
        4.4)需要打开 />  改为 >
        4.5)expression 类地址 可以点 @Repository 下去查看类在哪个包下再给地址
        4.6)报错org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'atguigu.XxxDao' available
    5)周期方法
        5.1)@PostConstruct  初始化方法
        5.2)@PreDestroy     销毁方法
        5.3)周期方法命名格式 public  void  XXX() 没有返回值
        5.4)作用域  @Scope
            @Scope(scopeName = ConfigurableBeanFactory.SCOPE_SINGLETON)
            @Scope(scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
            5.4.1) 多例模式下 close() 也不管理 销毁方法
            5.4.2) ConfigurableBeanFactory 从@Scope 进去后的绿色注释上看
    6) di 依赖注入
        6.1).原本形式
            6.1.1) xml 配置
                <bean id="userService" class="com.atguigu.ioc_03.UserServiceImpl"/>
                <bean id="UserController" class="com.atguigu.ioc_03.UserController">
                    <property name="userService" ref="userService"/>
                </bean>
            6.1.2) 类 配置
                controll 1.定义方法show() 2.设置接口为属性 3.设置属性set方法
                svrvice接口类 1.定义方法
                svrvice接口类 实现类 1.定义方法 写业务逻辑
       6.2).现在
            6.2.1) xml 配置
                <context:component-scan base-package="com.atguigu"/>
            6.2.2) 类 配置
                controll  1.定义方法show() 2.设置接口为属性 3.@Autowired
                svrvice接口类 1.定义方法 2.@Service
                svrvice接口类 实现类 1.定义方法 2.写业务逻辑 3.@Service
       6.3).
            6.3.1).@Autowired[基本加到成员变量上]常用
                1.在ioc容器中查找符合类型的组件对象2.设置当前属性(di)
                2.修饰后至少有一个bean 否则报错 特殊3.
                3.@Autowired(required = false) 调用方法时报错 不推荐
                4.多个组件解决 1.属性名设置为实现类类名 2.@Qualifier(value = "userServiceImpl")
            6.3.2).@Qualifier (value = "beanid")
                1.不能单独使用 @Autowired 配合使用
                2.(value = "userServiceImpl")
            6.3.3).@Resource = @Autowired + @Qualifier
                1.JSR-250注解 需要加入依赖
                2.  <dependency>
                        <groupId>jakarta.annotation</groupId>
                        <artifactId>jakarta.annotation-api</artifactId>
                        <version>2.1.1</version>
                    </dependency>
    7)注解赋值 常用与外部变量赋值
        7.1)xml配置
            <context:property-placeholder location="classpath:jdbc.properties"/>
        7.2)1.:xxxx 默认值 2.只能放字符串 3.格式@Value() 4.外部数据导入格式 "${}"
            @Value("${atguigu.password:xxxx}")
    8)第三方 <bean 还是原来的配置方式
    
    9)注意点
        1)同名文件
                test:
                com.atguigu.ioc_04.JavaBean bean = classPathXmlApplicationContext.getBean(com.atguigu.ioc_04.JavaBean.class);
                xml:
                <context:component-scan base-package="com.atguigu.ioc_04"/>
-->

<!--    <context:component-scan base-package="atguigu,atguigu"/>-->

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




<context:property-placeholder location="classpath:jdbc.properties"/>








</beans>

相关文章

网友评论

      本文标题:4.3 基于 注解 方式管理 Bean

      本文链接:https://www.haomeiwen.com/subject/flrowdtx.html