spring IOC注解:
@Autowired:表示自动注入,要让 @Autowired 起作用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。
<!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
或者:
使用隐式注册(隐式注册 post-processors 包括了 AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="doc.guandang.net;
xmlns:xsi="doc.guandang.net;
xmlns:context="doc.guandang.net;
xsi:schemaLocation="doc.guandang.net
doc.guandang.net
doc.guandang.net
doc.guandang.net;
<context:annotation-config/>
</beans>
@Autowired默认按照类型匹配的方式进行注入
@Autowired注解可以用于成员变量、setter方法、构造器函数等
使用@Autowired注解须有且仅有一个与之匹配的Bean,当有多个匹配的bean或者没有找到匹配的bean,spring会抛异常
@Resource:
@Resource 的作用相当于 @Autowired,只不过 @Autowired 默认按byType 自动注入,而@Resource 默认按 byName自动注入罢了。
@Resource 有两个属性是比较重要的:name和type,指定了name,则按byName自动注入,指定了type,则按byType自动注入,两者都不指定,则按默认的byName自动注入
@PostConstruct 和 @PreDestroy:
@PostConstruct:
被@PostConstruct标注的方法会在该类实例化后被自动调用,
@PreDestroy:
被@PreDestroy标注的方法会在该类销毁后被自动调用
@Component:
被@Component标注的bean表示需要spring依赖注入,若xml里也定义了,xml中的定义会覆盖类中注解的Bean定义。
@Component 有一个可选的入参,用于指定 Bean 的名称。
示例:
@Component
public class ActionMovieCatalog implements MovieCatalog {
// ...
}
@Component 定义的 Bean默认都是singleton单实例的,@Scope注解可以指定是单例还是非单例
示例:
@Scope("prototype")
@Repository
public class MovieFinderImpl implements MovieFinder {
// ...
}
Spring 2.5引入了更多典型化注解(stereotype annotations):
@Component、@Repository、@Service和 @Controller。
@Component是所有受Spring管理组件的通用形式;
而@Repository、@Service和 @Controller则是@Component的细化
@Repository:用于Dao层
@Service:用于service层bean的定义
@Controller:用于控制层bean的定义
<context:component-scan/>:定义过
Spring 学习笔记 Spring 控制反转器控制反转(Inversion of Contorl,IoC) 接口的选择 在构建 JavaEE 应用时,通常使用 ApplicationContext,因为它不仅提 供了 BeanFactory...
Spring学习笔记(马士兵spring视频笔记)_IT/计算机_专业资料。此文档是我自己在原有的马士兵老师的笔记上整理的spring学习笔记 文档中添加许多我个人的说明希望能给...
spring学习笔记_IT/计算机_专业资料。本人学习,写下的东西,还没有完成,慢慢完善!控制反转控制反转分为依赖注入与依赖查找。 依赖查找:组件必须持有对象的一个引用...
Spring 学习笔记 IOC 控制反转控制反转:将对象实例化的工作交给 Spring 做。 Spring 实例化对象的方式以及在配置文件中的体现: 1、 调用默认构造方法实例化对象。 ...
Spring个人学习笔记 23页 1下载券 关于Spring的学习笔记 3页 免费 spring学习笔记2 8页 2下载券 Spring学习笔记,精华摘要... 10页 免费 Spring学习笔记总结 ...
Spring学习笔记,精华摘要_IT/计算机_专业资料。Spring学习笔记,精华摘要Spring中大量使用两种设计模式:工厂模式、单态模式 1.一个简单的单态模式 public class ...
spring学习笔记spring学习笔记隐藏>> spring主要功能:控制反转,依赖注入,面向切面,及提供工具类 一,配置环境:导入包:dist/spring.jar lib/jakarta-commons/commons-...
Spring学习笔记Spring学习笔记隐藏>> 第一章 Spring概述 1、轻量级框架 1)框架:Hibernate、Struts、Spring 1. Struts:web层的框架 2. Hibernate:持久层的框架 3...
Spring学习笔记_工学_高等教育_教育专区。在基于 Spring 的应用中,应用组件在 Spring 范围内是激活的。容器可以创建组件,装备和配置组件,以及管理它们 的整个生命...
热门文档