Spring学习之路(3)----代码练习1

前言

  • spring是容器框架
  • 可以配置各个组件(bean)
  • 维护bean与bean之间的关系

程序

多个bean

package com.service;

public class UserService {
	private String name;

	public String getName() {
		return name;
	}

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

package com.service;

public class ByeService {
	
	private String name;
	private int id;
	public ByeService() {
	// TODO Auto-generated constructor stub
		System.out.println("byeid:"+id);
	}
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
public void saybye() {
	System.out.println("bye:"+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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx 
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context      
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 在容器文件中配置bean -->
<bean id="userService" class="com.service.UserService">
	<property name="name">
		<value>gyy</value>
	</property>
</bean>

<bean id="byeService" class="com.service.ByeService">
	<property name="name" value="ygg"></property>
	<property name="id">
		<value>123</value>
	</property>
</bean>

</beans>
package com.test;

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

import com.service.ByeService;
import com.service.UserService;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		UserService us=new UserService();
//		us.setName("gyy");
//		us.sayHello();
		//自动调用构造器
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		//us就是userService
		UserService us=(UserService)ac.getBean("userService");
		us.sayHello();
		
		ByeService bs=(ByeService)ac.getBean("byeService");
		bs.saybye();

	}

}

结果:

byeid:0
hello:gyy
bye:ygg


bean调用bean

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx 
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context      
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 在容器文件中配置bean -->
<bean id="userService" class="com.service.UserService">
	<property name="name">
		<value>gyy</value>
	</property>
	<property name="byeService" ref="byeService"/>
</bean>

<bean id="byeService" class="com.service.ByeService">
	<property name="name" value="ygg"></property>
	<property name="id">
		<value>123</value>
	</property>
</bean>

</beans>
package com.service;

public class UserService {
	private String name;
	private ByeService byeService;

	public ByeService getByeService() {
		return byeService;
	}

	public void setByeService(ByeService byeService) {
		this.byeService = byeService;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	public void sayHello() {
		System.out.println("hello:"+name);
		byeService.saybye();
	}
}

其他不变

结果:

byeid:0
hello:gyy
bye:ygg
bye:ygg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值