---
title: "Java获取spring注解bean"
author: "youchengLin"
date: "2015-06-12T17:13:25Z"
tags: []
categories: ["企业架构"]
slug: "java-spring-bean"
draft: false
lastmod: "2015-06-12T17:14:25Z"
id: 2219262
url: "https://youchenglin.iteye.com/blog/2219262"
---

**获取注释方式(Annotaction)的bean**

    
```java
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());   
    TestService testService= (TestService) wac.getBean("TestService");  
```


**获取ApplicationContext.xml中的bean**

    
```java
BeanFactory factory = new ClassPathXmlApplicationContext("classpath:/applicationContext*.xml");  
              
    ITestService testService = (ITestService)factory.getBean("test-service"); 
```