Spring配置文件以及基本常识

package com.ant.spring;

public class HellWorld {
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

       //构造器
	public void hell() {
		System.out.println("hell:"+this.name);
	}
	
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


 <!--  注入方式:
        1.getter setters(常用)
        2.构造器注入(死的应用)
        3.接口注入(不常用,不推荐)
       通过getters setters注入
	配置spring 
	class:bean的全类名(包名+实体类类名),通过反射的方式在IOC容器中创建bean,并要求bean中必须有无参的构造器
	id:标识容器中的bean.id 唯一
	-->
	
	<bean id
package com.ant.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
	public static void main(String[] args) {
		
		//1.创建spring的IOC容器对象,并根据spring.xml文件实例化对象,调用property配置进行赋值
		//ApplicationContext代表IOC容器
		//ClassPathXmlApplicationContext:是ApplicationContext的实现类
		ApplicationContext con=new ClassPathXmlApplicationContext("spring.xml");
		
		//2.从IOC容器中获取bean并实例,利用id定位到IOC
		HellWorld hellWorld=(HellWorld) con.getBean("hellworld");
		hellWorld.hell();
		
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值