Spring容器类扫描注解

1、component

package Class_Scan_Annotation;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Created by Administrator on 2019/6/1.
 */

@Component("person")
public class Person {
    @Resource(name = "student")
//    @Autowired 按照类型匹配
//    @Qualifier("student") 按照name匹配
    private Student student;

    public Student getStudent() {
        return student;
    }
}

2、component

package Class_Scan_Annotation;

import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2019/5/29.
 */
@Component("student")
public class Student {
    public void say() {
        System.out.println("你好学生!");
    }
}

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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <!--
        component:把一个类放入到Spring容器中,该类就是一个component
        在base-package指定的包及子包下 扫描 所有的类
    -->
    <context:component-scan base-package="Class_Scan_Annotation"></context:component-scan>
</beans>

4、单元测试

package Class_Scan_Annotation;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AnnotationTest {
    @Test
    public void testAnnotation() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Class_Scan_Annotation/DI_Annotation.xml");
        Person person = (Person) context.getBean("person");
        person.getStudent().say();
    }
}

5、输出
学生你好
6、原理

* 原理
 * 1、启动spring容器,spring容器解析配置文件
 * 2、当解析到 <context:component-scan base-package="Class_Scan_Annotation"></context:component-scan>
 *      就会在上面指定的包及子包中扫描所有的类,看那些类上面有@component注解
 * 3、如果有component注解,则由如下的规则
 *          @component
 *          public class PersionDaoImpl{
 *
 *          }
 *          ==
 *          <bean id="persionDaoImpl" class ="XXXX"></bean>
 *          id的值,把类的第一个字母变成小写,其他的字母不变
 *           @component(name="persionDao")
 *          public class PersionDaoImpl{
 *
 *          }
 *          ==
 *          <bean id="persionDao" class ="XXXX"></bean>
确保 Spring 容器正确扫描组件可以从以下几个方面入手: ### 使用注解指定扫描范围 使用 `@ComponentScan` 注解可以指定 Spring扫描的基础包。例如: ```java import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan(basePackages = "com.example.demo") public class AppConfig { // 配置类的其他内容 } ``` 这里 `basePackages` 属性指定了要扫描的基础包,Spring扫描该包及其子包下带有 `@Component`、`@Service`、`@Repository`、`@Controller` 等注解的类,并将它们注册为 Spring Bean。还可以使用 `basePackageClasses` 属性,它告诉 Spring扫描哪些包以及其子包。例如: ```java import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import com.example.demo.service.ExampleService; @Configuration @ComponentScan(basePackageClasses = ExampleService.class) public class AppConfig { // 配置类的其他内容 } ``` 现在,如果在 `ExampleService` 类所在的包或者其任意子包下创建一个新的类(例如 `TestService`),那么这个组件类也会被自动注册为一个 bean [^4]。 ### 配置 XML 文件 在 XML 配置文件中,可以使用 `<context:component-scan>` 标签来指定要扫描的包。例如: ```xml <context:component-scan base-package="com.example.demo"/> ``` 这样 Spring扫描 `com.example.demo` 包及其子包下的组件类。同时,还可以使用过滤控制来精确管理扫描内容,例如排除特定组件: ```xml <context:component-scan base-package="com.hspedu.spring.component"> <!-- 排除@Service注解的类 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan> ``` 这将排除带有 `@Service` 注解的类被扫描 [^2]。 ### 确保注解正确使用 确保组件类上使用了正确的注解,如 `@Component`、`@Service`、`@Repository`、`@Controller` 等,同时确保配置类上使用了 `@Configuration` 注解,并且在配置类或 XML 中正确配置了组件扫描。 ### 避免不必要的配置冲突 避免在不同的配置文件或配置类中对同一组件进行重复配置,或者对扫描范围进行冲突的配置。同时,要注意一些特殊情况,例如某些开发人员创建新的 `@Configuration` 类时可能忘记将其包含在过滤器中,此时可以考虑指定将被 Spring 的组件扫描排除的特殊软件包 [^3]。 ### 检查 Spring 容器启动日志 在 Spring 容器启动时,查看日志信息,确认组件扫描是否按预期进行。如果发现有组件未被扫描到,可以根据日志中的提示信息进行排查。 ### 确保类路径正确 确保项目的类路径中包含了扫描的组件类,避免因为类路径问题导致组件无法被扫描到。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值