10.开发基于注解的spring

开发基于注解的Spring

Spring IoC容器指的是什么?

  • 有2种形式
  1. xml配置文件(applicationContext.xml)形式时:

存bean:
取bean: ApplicationContext applicationContext= newClassPathXmlApplicationContext("applicationContext.xml"); context.getBean();

  1. 注解形式(带有@Configuration注解的类[主配置类])

注意:两种形式获取的Ioc容器是 独立的

存bean : XXX
取bean(或者用@Autowired自动装配):
ApplicationContext ac = new AnnotationConfigApplicationContext(MyConfig.class) ;

  • XXX:注解形式 给IoC容器中存放Bean:
  1. 必须有@Configuration注解(配置类)
  2. 形式:
    • 要把三层组件加入IOC容器: 给个各类加注解 、 扫描器识别注解所在包
    • 给三层组件分别加注解(@Controller、@Service、@Repository -> @Component泛指)
    • 将注解所在包纳入ioc扫描器(ComponentScan)
    • 纳入ioc扫描器:xml配置文件 :
      <context:component-scan base-package="com.yanqun.controller" ></context:component-scan>
  3. 逻辑:
    • 在三层类上加注解,让ioc识别扫描器:注解扫描器 component-scan:只对三层组件负责

给扫描器指定规则,来达到一些扫一些不扫描:

//excludeFilters-------------FilterType.ANNOTATION:
//@ComponentScan(value="com.yanqun",excludeFilters={@ComponentScan.Filter(type= FilterType.ANNOTATION,classes={Controller.class})})

//excludeFilters--------------FilterType.ASSIGNABLE_TYPE:
//@ComponentScan(value="com.yanqun",excludeFilters={@ComponentScan.Filter(type= FilterType.ASSIGNABLE_TYPE,classes={StudentDao.class})})

// includeFilters-------------FilterType.ANNOTATION: 
//@ComponentScan(value="com.yanqun",includeFilters={@ComponentScan.Filter(type= FilterType.ANNOTATION,classes={Controller.class})},useDefaultFilters=false)

// includeFilters-------------FilterType.CUSTOM
//@ComponentScan(value="com.yanqun",includeFilters={@ComponentScan.Filter(type= FilterType.CUSTOM,classes ={MyFilter.class})},useDefaultFilters=false)

过滤类型:FilterType(ANNOTATION,ASSIGNABLE_TYPE,CUSTOM)

  • 其中:ANNOTATION:三层注解类型@Controller、@Service、@Repository -> @Component

excludeFilters属性:排除
includeFilters属性:有默认行为,可以通过useDefaultFilters = false来禁止

  • ASSIGNABLE_TYPE:具体的类(StudentService.class)
    二者区分:
    ANNOTATION:Controller.class指的是所有标有@Controller的类
    ASSIGNABLE_TYPE:值得是具体的一个类 StudentController.class

CUSTOM自定义:自己定义包含规则
@ComponentScan.Filter(type= FilterType.CUSTOM ,value={MyFilter.class}

MyFilter implements TypeFilter,重写其中的match,如果return true则加入IoC容器

  • 非三层组给ioc加入bean(Student.class,IntToStringConver.class)
    • @Bean+方法的返回值 ,id默認就是方法名(可以通过@Bean(“stu”) 修改id值)
    • import 、FactoryBean

配置bean: 类
1. 非三层组件在主配置类中:@Bean,id默认为方法名(也可以指定)
2. 三层组件:Controller、Service、Dao:

@Component(@Controller、@Service、@Repository)

三层组件
1. 加入@Component等注解
2. 配置,让容器识别注解

XML形式:通过扫描器将@Component等注解所在包扫描
<context:component-scan base-package="com.yanqun" ></context:component-scan>

注解(主配置类)

@Configuration//表示该类为spring主配置类
@ComponentScan(value="com.yanqun")
public class MyConfig {...}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值