引入包:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<!-- ServletContextResource -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
src/main/resources下的beans.xml
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="car1" class="com.t1.Car"
p:brand="红旗CA72"
p:color="黑色"
p:maxSpeed="200"/>
</beans>
Car.java
package com.t1;
public class Car {
private String brand;
private String color;
private String maxSpeed;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(String maxSpeed) {
this.maxSpeed = maxSpeed;
}
public void introduce(){
System.out.println("brand:"+brand+",color:"+color+",maxSpeed:"+maxSpeed);
}
}
Beans.java
package com.t1;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Beans {
@Bean(name="car")
public Car buildTcar(){
Car tcar = new Car();
tcar.setBrand("红旗CA72");
tcar.setColor("黑色");
tcar.setMaxSpeed("200");
return tcar;
}
}
测试类:
package com.t1;
import java.io.IOException;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
public class T2 {
public static void main(String[] args) throws IOException {
m3();
}
public static void m1() throws IOException{
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource resource = resolver.getResource("classpath:beans.xml");
System.out.print(resource.getURL());
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(resource);
System.out.println("init BeanFactory");
Car car = factory.getBean("car1",Car.class);
car.introduce();
}
public static void m2() throws IOException{
ApplicationContext application = new ClassPathXmlApplicationContext("classpath:beans.xml");
Car car = application.getBean("car1", Car.class);
car.introduce();
((ClassPathXmlApplicationContext)application).close();
}
public static void m3() throws IOException{
ApplicationContext context = new AnnotationConfigApplicationContext(Beans.class);
Car car = context.getBean("car",Car.class);
car.introduce();
((AnnotationConfigApplicationContext)context).close();
}
}