xmlns:p=“http://www.springframework.org/schema/p”
xmlns:c=“http://www.springframework.org/schema/c”
<context:annotation-config/>
<aop:annotation-config/>
@Autowired
先匹配类型,通过byType的方式实现 后匹配id 而且要求这个对象必须存在
(required = false)时 指定可以为空值
可以手动为@Autowired匹配value
通过java方法实现自动装配 先匹配name 后匹配value
注解放在类上,说明这个类被Spring管理了,相当于添加了bean
1.确保aop的包的导入
使用注解需要导入context约束,增加注解的支持
@Component有几个衍生注解,我们在Web开发中,会按照MVC三层架构分层!
####dao层 @Repository
####service层 @Service
####controller层 @controller
这四个注解功能都是一样的,都是将他们某个类注册到Spring容器中,装配Bean
-@Autowired:通过类型自动装配,后匹配名字
如果Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value=“xxx”)
-@Nullable :字段标记了此注解,表示这个字段可以为null;
-@Resource:通过名字自动装配,后匹配类型
、xml用来管理Bean
、注解只负责完成属性的导入
、在使用过程中必须开启注解支持,让注解生效
##作用域
@Scope
singleton
prototype
@Configuration会让类被容器托管,注册到容器中,因为它本来就是一个@Component
@Configuration代表这是一个配置类,相当于beans.xml中的配置
import org.springframework.context.annotation.Bean; @Configuration @ComponentScan(com.wang.pojo) public class MyConfig { // @Bean 注册一个Bean,相当于之前写的Bean标签 // 它的方法的名字相当于Bean标签中的id属性 // 它的返回类型相当于Bean标签中的class属性 @Bean publi User getUser(){ return new User(); } }