Spring getBean method: get the JavaBean in the container

Grammar 1

getBean(String name)

Example

public static void main(String[] args){
  ApplicationContext context = new ClassPathXmlApplicationContext(
    "appContext.xml");  //初始化容器对象
  Object bean=context.getBean("dao");  //从容器中获取dao对象
}

Grammar 2

getBean(String name,Class requiredType)

Example

public static void main(String[] args){
  ApplicationContext context = new ClassPathXmlApplicationContext(
    "appContext.xml");  //加载appContext.xml
  Object bean = context.getBean("time",Date.class);  //获取指定JavaBean对象
  System.out.println("bean="+bean);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>