Spring学习之实验十五:通过注解分别把Dao、Service、Controller层注册到容器中。十六,十七

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:p="http://www.springframework.or.g/schema/p"
	xmlns:util="http://www.springframework.org/schema/util"
	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.0.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
		<bean class="Test.Book" id="book">
			<property name="bookName" value="西游记"></property>
		</bean>
	 	<!-- Spring学习之实验十五:通过注解分别把Dao、Service、Controller层注册到容器中 -->
	 	<!-- Controller:控制器,控制网站跳转逻辑Servlet -->
	 	<!--通过给bean添加某些注解,可以快速的将bean添加到ioc容器中
	 	某些类上添加以下四个注解的任何一个就可以把这个组件快速的注册到ioc容器中;
	 	Spring有四个注解:
	 	@Controller:控制器,我们推荐给控制器层(servlet包下的组件)添加的注解
	 	@Service:业务逻辑层注解:推荐给业务逻辑层的组件添加的注解:Service层
	 	@Repository:给数据库层(持久化层,Dao层)的组件添加的注解
	 	@Component:给不属于以上三种的组件添加的注解
	 	 
	 	###注解可以随便加,Spring底层不会去验证该组件是否加的正确
	 	各自推荐加的注解实际上是给程序员看的
	 	
	 	使用注解注册的步骤:
	 	1.给要添加注解的组件添加注解
	 	2.告诉Spring,自动扫描加了注解的组件(依赖于context名称空间)
	 		context:component-scan :自动进行组件扫描
	 		base-package :指定扫描的基础包,会自动扫描基础包和基础包下面所有包所有加了注解的类
	 	
	 	
	 	
	 	 -->
	 	 <context:component-scan base-package="Test"></context:component-scan>
</beans>

Test

package Test;

import static org.junit.Assert.*;

import java.applet.AppletContext;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.junit.Test;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class IOCTest2 {
	private ApplicationContext ioc=new  ClassPathXmlApplicationContext("IOCTest.xml");
	@Test
	
	
	/*1.通过注解注册的组件id默认是类名首字母小写
	 * 		修改默认id:@Component("car01")
	 * 2.注解注册的和配置注册的组件默认是一样的,作用域默认是单例的
	 * 		修改成多实例:@Scope(value="prototype")
	 * 3.必须导入aop包,否则会报错
	 *java.lang.ClassNotFoundException: 
	 *org.springframework.aop.TargetSource
	 **/
	public void test() {
		Object bean = ioc.getBean("car");
		System.out.println(bean);
	}
	
}

<context:component-scan base-package="Test" use-default-filters="false">
	 	 	<!--扫描的时候排除一些组件
	 	 		常用::type="annotation"指定排除规则,按照注解进行排除。标注了指定注解的组件不要
	 	 		expression="" 注解的全类名
	 	 		常用::type="assignable" 指定排除某个具体的类
	 	 		
	 	 		以下三种用不到
	 	 		type="aspectj"后来要写的aspectj表达式
	 	 		type="custom":自定义一个TypeFilter;自己定义排除哪一个
	 	 		type="regex" 还可以写正则表达式
	 	 	  -->
	 	 	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Component"/>
	 	 	<!-- 扫描的时候包含哪些组件 -->
	 	 	<!-- 指定只扫描哪些组件 
	 	 	一定要禁用默认的扫描规则
	 	 	可以多写几个
	 	 	其他规则同上
	 	 	use-default-filters="true" 设置为false,意思是不使用默认的扫描规则 
	 	 	-->
	 	 	<context:include-filter type="annotation"  expression=""/>
	 	 </context:component-scan>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值