Spring
常用依赖
注解
1.@Resource(常用)自动装配,通过类型,名字可通过加name属性精细 寻找
2.@Autowired自动装配,通过名字,类型,如果不能自动装配则需要@Qualifier(value=" ")
3.@component: 组件,放在类上,说明这个类被Spring管理了,就是bean。
等价于xml中的bean,创建一个域
4.@Value 注解给值,相当于property中给对象赋值
5.@Repository dao层 @Service service层 @Controller controller层 注入Spring 类似bean
6.@Scope 作用域 单例模式("singleton") 原型模式("prototype")
实现注解需要导入约束
xmlns:context="http://www.springframework.org/schema/context"
context:annotation-config/
--
实现aop需要导入约束
xmlns:aop="http://www.springframework.org/schema/aop"
--
beans.xml头部
context:annotation-config/
context:annotation-config/
beans.xml注入问题
1.下标赋值、参数类型赋值、直接参数名赋值 三者不能混合使用(参数类型赋值类型不可重复)
2.起别名:
3.作用域:Scope 作用域 单例模式("singleton") 原型模式("prototype")
4.import:将多个beans.xml文件导入同一文件内。
可通过
无参构造
有参构造
依赖注入:
构造器注入(如上)
set注入:直接参数名赋值 数组array list list map map Set set
拓展注入:
两者需要导入xml约束
c命名:与p命名类似
p命名注入:在bean中直接赋值 p:username="王士贤"
autowire:通过type name 自动装配其他bean
指定要扫描的包,包下的注解生效
<context:component-scan base-package=""/>等价于@component
@component: 组件,放在类上,说明这个类被Spring管理了,就是bean。
等价于xml中的bean,创建一个域
@Value 注解给值,相当于property中给对象赋值
@Repository dao层 @Service service层 @Controller controller层 注入Spring 类似bean
context:annotation-config</context:annotation-config>
使用javaconfig配置 用new AnnotationConfigApplicationContext()链接配置类
注解Aop
Aop包导入
<dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.4</version>
</dependency>
--