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
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值