spring自动添加注解
来源:互联网 发布:匡恩网络是个奇葩公司 编辑:程序博客网 时间:2024/05/01 18:34
pring Filter Components In Auto Scanning —— 在自动扫描中过滤组件
Filter Component——include
下例演示了用“filter”自动扫描注册组件,这些组件只要匹配定义的“regex”的命名规则,Clasee前就不需要用@Component进行注释。
DAO层,CustomerDAO.java如下:
package com.lei.customer.dao;public class CustomerDAO { @Override public String toString() { return "Hello , This is CustomerDAO"; } }
Service层,CustomerService.java如下:
package com.lei.customer.services;import org.springframework.beans.factory.annotation.Autowired;import com.lei.customer.dao.CustomerDAO;public class CustomerService { @Autowired CustomerDAO customerDAO; @Override public String toString() { return "CustomerService [customerDAO=" + customerDAO + "]"; }}
Spring Filtering,xml配置如下:
<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"> <context:component-scan base-package="com.lei" > <context:include-filter type="regex" expression="com.lei.customer.dao.*DAO.*" /> <context:include-filter type="regex" expression="com.lei.customer.services.*Service.*" /> </context:component-scan></beans>
以上xml文件中,所有文件名字,只要包含DAO和Service(DAO.,Service.)关键字的,都将被检查注册到Spring容
运行以下代码:
package com.lei.common;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.lei.customer.services.CustomerService;public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-AutoScan.xml"}); CustomerService cust = (CustomerService)context.getBean("customerService"); System.out.println(cust); }}
运行结果:CustomerService [customerDAO=Hello , This is CustomerDAO]
Filter Component——exclude
你也可以用exclude,制定组件避免被Spring发现并被注册到容器中。
以下配置排除用@Service注释过的组件
<context:component-scan base-package="com.lei.customer" > <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan>
<context:component-scan base-package="com.lei" > <context:exclude-filter type="regex" expression="com.lei.customer.dao.*DAO.*" /> </context:component-scan>
0 0
- spring自动添加注解
- spring添加注解事务
- spring 注解 自动装配
- Spring注解自动注入Bean
- spring注解自动装配Bean
- spring注解自动注入bean
- Spring注解自动注入Bean
- Spring 使用注解自动装配
- Spring自动装配(注解)
- spring 4 注解方式添加数据源
- 让Spring自动加载Hibernate 注解类
- Spring@Autowired注解与自动装配
- Spring自动装配的注解用法
- Spring@Autowired注解与自动装配
- Spring@Autowired注解与自动装配
- Spring@Autowired注解与自动装配
- Spring常用注解,自动扫描装配Bean
- spring的自动注解@Autowired配置
- POJ - 1251----Jungle Roads(prim)
- beactiful_code
- 免费java架构师大数据redis 视频
- hibernate序列化问题
- nexus-3.2.0-01.zip安装及使用说明
- spring自动添加注解
- USB host control
- 程序员的自主学习
- linux,go!
- Mysql的索引和慢查询优化
- Android各个版本名称与版本号对照表
- Android酷炫实用的开源框架(UI框架)
- JDBC 连接步骤
- Linux链接库三(C跟C++之间动态库的相互调用)