spring学习-1

spring框架的组件结构图


IOC(Inversion of Control):反转控制,思想是反转资源获取方向,传统的资源查找方向是组件向容器发起请求资源查找,作为回应,容器适时的返回资源,IOC则相反,容器主动将资源推送给它所管理的组件,组件所要做的就是选择一种合适的方式接受资源。
DI(Dependency Injection):依赖注入,(IOC另一种表述)组件以一些预先定义的方式来自容器的资源注入。

spring用到的jar包

1、创建Bean

public class HelloSpring {
	private String name;

	public void setName(String name) {
		System.out.println("setname= " + name);
		this.name = name;
	}   
     
	public void Hello() {
		System.out.println("hello" + name);
	}

	public HelloSpring() {
		// TODO Auto-generated constructor stub
		System.out.println("constructor start。。。。 ");
	}

2、在根目录下创建New Spring Bean Definition file xml文件,通过属性方法注入

   <!-- 配置beans class :Bean的全类名,通过反射的方式在IOC容器中创建Bean,所以Bean中必须要有无参数的构造器 id:标识容器中的Bean,id唯一 -->
   <bean id="helloSpring" class="com.test.HelloSpring">
   	<!-- property 的name属性和beans的set方法相对应 -->
   	<property name="name" value="spring"></property>
   </bean>

3、使用构造方法注入,在constructor-arg元素里面声明属性值

3.1 新建User Bean

public class User {
	private String name;
	private int age;
	private double money;
	private String no;

	public User(String name, double money, String no) {
		super();
		this.name = name;
		this.money = money;
		this.no = no;
	}

	public User(String name, int age, String no) {
		super();
		this.name = name;
		this.age = age;
		this.no = no;
	}

	@Override
	public String toString() {
		return "User [name=" + name + ", age=" + age + ", money=" + money
				+ ", no=" + no + "]";
	}
}

3.2 通过构造方法注入

//使用构造器注入属性值,可以指定参数的位置和参数的类型,以区分重载的构造器 index:位置 type:类型 
<bean id="user1" class="com.test.User">
		<constructor-arg value="aa" index="0"></constructor-arg>
		<constructor-arg value="10" index="1" type="double"></constructor-arg>
		<constructor-arg value="123456" index="2"></constructor-arg>
	</bean>

	<bean id="user2" class="com.test.User">
		<constructor-arg value="cc"></constructor-arg>
		<constructor-arg value="10" type="int"></constructor-arg>
		<constructor-arg value="78901" ></constructor-arg>
	</bean>

4、创建spring IOC容器对象,并从IOC容器获取bean实例,然后调用Bean中的方法

  //ApplicationContext IOC容器
  //ClassPathXmlApplicationContext:是ApplicationContext接口实现类,该实现从类路径下来加载配置文件
     ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
 
  //利用id定位到IOC容器的bean
     HelloSpring hello = (HelloSpring) ctx.getBean("helloSpring");

  //利用类型获取,不推荐使用
   //HelloSpring hello = ctx.getBean(HelloSpring.class);

   // 调用类的方法
      hello.Hello();

     User use = (User) ctx.getBean("user1");
     System.out.println(use);

     use = (User) ctx.getBean("user2");
     System.out.println(use);

5、运行效果

一月 17, 2017 10:38:52 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]

constructor start。。。。 
setname= spring
hellospring
User [name=aa, age=0, money=10.0, no=123456]
User [name=cc, age=10, money=0.0, no=78901]

此系列学习源码:传送门

作者:starryfeiii
出处:http://www.cnblogs.com/feiii/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,原文链接否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/feiii/p/6294988.html