spring核心

aop的概念:

面向切面编程:Aspect Oriented Programming


annotation:

想用annotation,在spring.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.xsd">
<context:annotation-config/>
</beans>
xmlns:context的意思是xml中,ns(namespace),以context开头的名字是在"<span style="font-family: Arial, Helvetica, sans-serif;">http://www.springframework.org/schema/context</span>"的<span style="font-family: Arial, Helvetica, sans-serif;">schemaLocation</span><span style="font-family: Arial, Helvetica, sans-serif;">去找,</span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html"><span style="font-family: Arial, Helvetica, sans-serif;">"</span><span style="font-family: Arial, Helvetica, sans-serif;">http://www.springframework.org/schema/context</span><span style="font-family: Arial, Helvetica, sans-serif;">"定义的</span><span style="font-family: Arial, Helvetica, sans-serif;">schemaLocation是对应的"</span><span style="font-family: Arial, Helvetica, sans-serif;">http://www.springframework.org/schema/context/spring-context.xsd</span><span style="font-family: Arial, Helvetica, sans-serif;">",</span>
<span style="font-family: Arial, Helvetica, sans-serif;">xsi提供了文件的名字和内容</span><span style="font-family: Arial, Helvetica, sans-serif;">然后在xml catalog里面配置</span><span style="font-family: Arial, Helvetica, sans-serif;">schemaLocation</span>

 
xml语法旧的是dtd文件,新的是xsd文件
然后写了<context:annotation-config />就可以用了
在需要注入的set方法上面写@Autowired或者在属性上面写,例如在service中getDao的地方用到即可,就不用在xml的service里面配置dao了
spring调用4个processor处理器去处理:
(The implicitly registered post-processors include AutowiredAnnotationBeanPostProcessor,
CommonAnnotationBeanPostProcessor, PersistenceAnnotationBeanPostProcessor, as
well as the aforementioned RequiredAnnotationBeanPostProcessor.)
处理器是按照type去处理,如果有两个一样的bean,就会报错,不知道匹配哪个类
可以在setBean(BeanClass class)的地方写个<span style="font-family: Arial, Helvetica, sans-serif;">@Qualifier("name")就可以按照名字注入了   </span><span style="font-family: Arial, Helvetica, sans-serif;">setBean(</span><span style="font-family: Arial, Helvetica, sans-serif;">@Qualifier("name")</span><span style="font-family: Arial, Helvetica, sans-serif;">BeanClass class)</span>
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>

可以用java自带的@Resource代替@AutoWire,效果是一样的

当没有源码的时候,就没办法用annotation了,需要在配置文件里面配置

另一种方法:

<context:component-scan base-package="org.example"/>

不用去配置,直接去包里面找组件,需要在类的头部加@Component,容器初始化会查找组件,然后放到容器里面。

如:userDao类的头部加上@Component,userService加@Component,然后再setDao上面加@Recourse,就可以用了

@Component(value="")最好这样写,指定名字,否则就将类名的第一个字母小写作为名字

文档中说默认的注解有:

By default, classes annotated with @Component, @Repository, @Service, @Controller, or
a custom annotation that itself is annotated with @Component are the only detected candidate
components.


@Resource默认是按名字查找,如果名字找不到就按照类型查找

给bean设置scope的注解写法:@Scope("prototype")


@PostConstruct and @PreDestroy 相当于init 和destroy


以上是IOC常用注解





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值