<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="model.annotation"></context:component-scan>
</beans>
package model.annotation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component("demo1Bean") //受Spring IoC控管的任何Bean元件
//也可以寫成@Component(value="demo1Bean"),同<bean id="demoBean0" class="model.DemoBean">
@Scope("prototype")
public class Demo1Bean {
private String name="這是一個annotation";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package org.iiiedu.samuel.ProjSpringDemo;
import model.annotation.Demo1Bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAnnotation {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beansTest2.xml");
Demo1Bean demo1Bean =(Demo1Bean)context.getBean("demo1Bean");
System.out.println(demo1Bean.getName());
}
}
六月 07, 2016 8:33:30 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
資訊: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@117ae12: startup date [Tue Jun 07 20:33:30 CST 2016]; root of context hierarchy
六月 07, 2016 8:33:30 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
資訊: Loading XML bean definitions from class path resource [beansTest2.xml]
這是一個annotation