文章目录
前言
- 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