spring IOC笔记,@Autowired 与@Resource

spring IOC笔记,@Autowired 与@Resource

控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。其中最常见的方式叫做依赖注入(Dependency Injection,简称DI),还有一种方式叫“依赖查找”(Dependency Lookup)

spring编程的风格
schemal-based-------xml
annotation-based-----annotation
java-based----java Configuration

@Autowired 与@Resource的区别@Autowired 默认按类型byType装配,如果对应的类型没找到或者有多个,按名称byType装配。
例如:
@Autowired
pubilc Student student;
如果按Student没有找到唯一的记录,就按student去找(Student类);@Resource 可以指定是按类型,还是按名称。
例如:
@Resource(name=“xxx”)
pubilc Student student;

@Resource(type=“xxx”)
pubilc Student student;
两种方式都不用写set方法。
按byName获取,也可以实现Spring提供的类,自定义规则。 或者@Service(“自定义名称”)

spring加载的bean,默认是single?

单例类中引用了原形类,原型就失去了意义。
单例类实现Spring提供的ApplicationContextAware,可以修改。这样和Spring耦合度太高了。
可以改用Lookup . 这样的话侵入性低一点。

上一篇:(六)Bean的自动装配


下一篇:spirngboot整合webservice入门(@Autowired 无法注入问题)