spring 注解
时间:2020-10-08 19:26:45
收藏:0
阅读:23
在使用注解注入时,需要在配置文件中导入context命名空间和注解扫描路径,多个路径之间用逗号隔开
@Autowired
- 标注在属性上表示此属性需要被注入
- 默认是按类型注入,如果想修改成按名称注入在@Autowired注解的下方添加注解@Qualifier(),并传入相应的名称
@Resource
- 这个注解 是java中的注解 - 默认是按照byName注入,如果没有找到,则按照byType注入
- 所以把对象名称和spring容器中对象名要命名相同,可以节省资源
注意:
@Resource 与@Autowired作用相同,两者二选一就行,标注的属性都不需要写getter/setter方法,@Autowired不同于@Resouce的作用是对JDK进行了解耦合
@Scope(“prototype”)
- 在类上标注此注解表示,以原型模式来创建实例
- 在类上标注此注解表示,以原型模式来创建实例
Repository("")
- 用于标注DAO类
- 用于标注DAO类
@Value
- 获取properties文件中的内容
Service("")
- 用于标注业务类
@Controller
- 用于标注控制器类
The use of <context:component-scan> implicitly enables the functionality of <context:annotation-config> . There is usually no need to include the <context:annotation-config> element when using <context:component-scan> . |
@Bean
- @Bean用于表示方法实例化,在spring Ioc管理容器中配置和初始化一个新的对象,相当于xml配置文件中的
,经常用于@Configruration标注的bean,例如:
@Configuration public class AppConfig { @Bean public MyService myService() { return new MyServiceImpl(); } }
相当于XML中配置了一个
<beans> <bean id="myService" class="com.acme.services.MyServiceImpl"/> </beans>
- @Bean用于表示方法实例化,在spring Ioc管理容器中配置和初始化一个新的对象,相当于xml配置文件中的
Aop注解
<!--在使用aop注解时需要在xml文件添加下注解-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
@Aspect
- 在类上添加此注解表示,这个类是一个增强类
- 在类上添加此注解表示,这个类是一个增强类
- @Before() 前置增强
- @After 最终增强
- @AfterReturning 后置增强
- @AfterThrowing 异常增强
- @Around 环绕增强
参考:
https://docs.spring.io/spring-framework/docs/5.1.3.RELEASE/spring-framework-reference/core.html#beans-annotation-config
https://www.cnblogs.com/caoyc/p/5626365.html
评论(0)