67,512
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<description>Spring</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
package com.javarticles.spring.annotations;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@Configuration
@ComponentScan(basePackages = {
"com.javarticles.spring.annotations.packageA",
"com.javarticles.spring.annotations.packageB" })
public class SpringComponentScanAnnotationViaXMLExample {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
try {
System.out.println("BeanA: "
+ ctx.getBean("beanA"));
System.out.println("BeanB: "
+ ctx.getBean("beanB"));
System.out.println("Contains BeanC1?: "
+ ctx.containsBean("beanC31"));
System.out.println("Contains BeanD?: "
+ ctx.containsBean("beanD"));
System.out.println("Contains BeanE1?: "
+ ctx.containsBean("beanE1"));
} finally {
ctx.close();
}
}
}