context:component-scan使用

context:component-scan使用

  1.使用自定义注解

   我们知道,context:componet-scan只会扫描类上的@Configuration @Component @Controller @Service @Repository

    并且自动开启<context:annotation-config />标签,扫描类中的注解,那让它怎么扫描我们自定义的注解了

        并且我们可以使@Component失效

   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 http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <context:component-scan base-package="liusheng" >
        <!-- 这样便可以扫描我们的自定义注解了 -->
        <context:include-filter type="annotation" expression="liusheng.dao.MyAnnotation"/>
        <!-- 我们这样可以使 @Component标签失效-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Component"/>
    </context:component-scan>
</beans>

    实体类:

  UserDao

package liusheng.dao;

@MyAnnotation
public class UserDao {
    /**
     * 
     * 模仿插入方法
     */
        public void add(){
            System.out.println("UserDao.add()");
        }
}

  StudentDao:

  

package liusheng.dao;

import org.springframework.stereotype.Component;

@Component//失效
public class StudentDao {
        public void add(){
            System.out.println("StudentDao.add()");
        }
}

  测试类:

  

package liusheng.dao;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/java/applicationContext.xml")
public class SanTest {
    @Autowired
    UserDao dao;
          //  为了能正常输出,我把属性设为false,默认为true
    @Autowired(required=false)
    StudentDao studentDao;
    @Test
    public void test(){
        System.out.println(dao);
        System.out.println(studentDao);
    }

}
    

  结果:

    

  2.使用regex的模式匹配类

    我们可以把所有类上的注解去掉,使用regex模式匹配完整类名

    配置文件:

    

<?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-4.1.xsd">

    <context:component-scan base-package="liusheng" >
    <!--里面的点不用打双斜杠-->
<context:include-filter type="regex" expression="liusheng.dao.*Dao"/> </context:component-scan> </beans>

  测试类还是上述的测试类

  结果:

  

  我发现跟AspectJ的表达的匹配默认是一样的,所以Spring很多地方都是相似的,降低了我们的学习成本,这也是Spring的一个优点

    

  

 

     

转载于:https://www.cnblogs.com/SpringStudy/p/8598460.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: component-scan报错是指在Spring配置文件中使用component-scan标签扫描组件时出现了错误。可能的原因包括: 1. 扫描路径配置错误:component-scan标签中的base-package属性配置错误,导致无法扫描到需要的组件。 2. 组件类定义错误:扫描到的组件类定义不符合Spring的要求,例如缺少必要的注解或配置。 3. 依赖注入错误:扫描到的组件类中存在依赖注入错误,例如注入的bean不存在或注入方式不正确。 解决方法包括: 1. 检查component-scan标签中的base-package属性是否正确配置。 2. 检查组件类定义是否符合Spring的要求,例如是否添加了必要的注解或配置。 3. 检查依赖注入是否正确,例如注入的bean是否存在或注入方式是否正确。 4. 查看错误信息,根据错误信息进行排查和解决。 ### 回答2: component-scan是Spring框架中用来自动扫描并注册Bean的组件之一。它通常会自动扫描指定路径下的所有类,并将标记有特定注解的类注册为Spring Bean。 如果在配置文件中配置了component-scan,但是在启动时出现了报错,可能有以下几种原因: 1. 配置文件中component-scan的路径错误 如果component-scan扫描的路径错误,Spring框架就会找不到需要注册的Bean,从而报错。需要检查路径是否正确,并且确保扫描路径下确实有需要注册为Bean的类存在。 2. 需要注册的Bean类缺少必要的注解 component-scan通常会查找标记有特定注解的类,并将其注册为Spring Bean。如果需要注册的Bean类缺少必要的注解,那么在扫描并注册Bean的时候就会报错。需要检查需要注册的Bean类是否标记了正确的注解,比如@Service、@Component、@Controller、@Repository等。 3. 版本不兼容 有时候,新版本的Spring框架与旧版本的Spring配置文件不兼容,也会导致component-scan报错。需要检查Spring框架和配置文件的版本是否匹配。如果不匹配,需要更新版本或者修改配置文件。 4. xml命名空间冲突 如果配置文件中引入了多个命名空间,有时候会出现命名空间冲突的情况,导致component-scan无法正常工作。需要检查配置文件中是否存在命名空间冲突的问题,并及时进行修改。 总之,在出现component-scan报错时,需要认真检查配置文件和代码,找出错误的原因,并进行修正。只有这样才能使我们的Spring应用程序正常工作,达到我们预期的效果。 ### 回答3: component-scan 报错是 Spring 框架中常见的问题,通常是因为 Spring 无法找到需要扫描的包或组件的原因。下面我将讨论一些可能导致 component-scan 报错的常见原因,以及如何解决这些问题。 1. 扫描的包路径错误 在配置 component-scan 的时候,指定的扫描包路径可能不正确。解决方法是检查 applicationContext.xml 配置文件中 component-scan 标签内指定的包路径是否正确,是否存在这个路径下。正确的格式为: < context:component-scan base-package="com.example"/> 如果你的包路径不在这个路径下,需要对标签中的包路径进行更改。 2. 在路径中排除了类 有时候在扫描的包路径中排除了类,比如排除了 controller 包,但是在 controller 包中定义了一些组件。解决方法是检查在 component-scan 标签上下文中的过滤器配置是否正确,确保所有需要的类都被扫描到了。 < context:component-scan base-package="com.example" exclude-filter="org.springframework.stereotype.Controller"/> 3. 缺少 Bean 的配置信息 有时候会出现 component-scan 不起作用的问题,原因是缺少 bean 的配置信息。解决方法是在 applicationContext.xml 文件中手动添加 bean 的配置信息,确保所有需要的 bean 被正确配置。 < bean id="exampleBean" class="com.example.ExampleBean"/> 4. 所需的 jar 包未加载 在使用 Spring 框架时,通常需要加载相关的 jar 包,否则 component-scan 报错时应检查相关 jar 包是否被正确加载,并且与版本兼容。 上述是在我遇到 component-scan 报错时常见的原因和解决方法,希望对你有所帮助。如果以上解决方法均不能解决问题,则需要检查应用程序的其它方面是否存在问题,如是否正确配置了数据库连接,或是否存在其它错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值