Spring 对象的声明与注入

1、怎么把一个对象该过过交给Spring管理?

  1)通过xml中配置<bean>节点来声明

  2)通过Spring注解来声明,如:@Service、@Repository、@Component

2、怎么把Spring容器中的对象注入到某个类的某个属性中?

  1)通过xml中配置<property>节点来注入

  2)通过Spring注解来注入,如@Autowired、@Resource

3、几个注解的却别(@Service、@Repository、@Component、@Autowired、@Resource)

    见我的另一篇文章《Spring常用注解

4、ApplicationContext是什么?怎么获取ApplicationContext对象?

   Application Context 是 spring 中较高级的容器。和 BeanFactory 类似,它可以加载配置文件中定义的 bean,将所有的 bean 集中在一起,当有请求的时候分配 bean。 另外,它增加了企业所需要的功能,比如,从属性文件从解析文本信息和将事件传递给所指定的监听器。

  web中,可以通过ContextLoader.getCurrentWebApplicationContext();来获取;

  main方法中,可以通过new ClassPathXmlApplicationContext或AnnotationConfigApplicationContext来获取

5、怎么看到Spring容器的所有对象?

  获取ApplicationContext对象 --> beanFactory --> beanDefinationMap

原文地址:https://www.cnblogs.com/aligege/p/8243363.html