Spring创建Bean的几种方式

引入包:

<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();
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值