Spring框架之容器(applicationContex.xml)

 

简单介绍

  1. Spring有两项技术:容器和AOP
  2. Spring现在已经从2.5到3.0,现在已经到了4.0版本,但是最好还是从2.5开始学起。
  3. 在Spring配置文件applicationContext.xml(当然名字是可以随意取的,但是还是要尽量遵守规范)
  4. 通过xml造出BeanFactory拿出bean
  5. 更多请见代码中有详细注解

 

简单初识Spring的容器技术

canSpring25Demo

 

Person.java

package cn.hncu.spring25;

public class Person {
	private String name;
	private Integer age;
	private Cat cat;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Cat getCat() {
		return cat;
	}
	public void setCat(Cat cat) {
		this.cat = cat;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", cat=" + cat + "]";
	}
	
}


 

Cat.java

package cn.hncu.spring25;

public class Cat {
	private String name;

	public String getName() {
		return name;
	}

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

	@Override
	public String toString() {
		return "Cat [name=" + name + "]";
	}
	
}

 

applicationContext.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:context="http://www.springframework.org/schema/context"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<!-- 默认是单例 -->
	<bean id="p1" class="cn.hncu.spring25.Person"></bean>
	<!-- 配置了scope="prototype"每次都是新new 一个对象,默认是 singleton-->
	<bean id="pp" class="cn.hncu.spring25.Person" scope="prototype"></bean>
	
	<bean id="p2" class="cn.hncu.spring25.Person">
		<property name="name" value="Jack"></property>
		<property name="age">
			<value>33</value>
		</property>
		<property name="cat" ref="cat"></property>
	</bean>
	<bean id="cat" class="cn.hncu.spring25.Cat" >
		<property name="name" value="Tom"></property>
	</bean>

</beans>


 

Demo.java

package cn.hncu.spring25;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Demo {
	public static void main(String[] args) {
		Resource rs=new ClassPathResource("applicationContext.xml");
		BeanFactory factory=new XmlBeanFactory(rs);
		Person p1=(Person) factory.getBean("p1");//参数为xml配置bean的id
		Person p2=(Person) factory.getBean("p1");
		System.out.println(p1);
		System.out.println(p2);
		System.out.println(p1==p2);
		
		System.out.println("-------------------------------");
		
		Person p3=(Person) factory.getBean("pp");//参数为xml配置bean的id
		Person p4=(Person) factory.getBean("pp");
		System.out.println(p3);
		System.out.println(p4);
		System.out.println(p3==p4);
	}

}


结果图:

 

canSpring30Demo


Person.java

package cn.hncu.domain;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Person {
	private String name;
	private Integer age;
	private Cat cat;
	private List<String> list;
	private Map<String, Object> map;
	private Set<String> set;
	
	private Object[] obj;
	
	public Object[] getObj() {
		return obj;
	}
	public void setObj(Object[] obj) {
		this.obj = obj;
	}
	public Set<String> getSet() {
		return set;
	}
	public void setSet(Set<String> set) {
		this.set = set;
	}
	public Map<String, Object> getMap() {
		return map;
	}
	public void setMap(Map<String, Object> map) {
		this.map = map;
	}
	public List<String> getList() {
		return list;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public Cat getCat() {
		return cat;
	}
	public void setCat(Cat cat) {
		this.cat = cat;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", cat=" + cat
				+ ", list=" + list + ", map=" + map + ", set=" + set + ", obj="
				+ Arrays.toString(obj) + "]";
	}
	
}


Cat.java

package cn.hncu.domain;

public class Cat {
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	public void eatFish(){
		System.out.println("I like fish, it is delicious");
	}
	@Override
	public String toString() {
		return "Cat [name=" + name + "]";
	}
	
}

applicationContext.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:context="http://www.springframework.org/schema/context"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
	<bean  class="cn.hncu.domain.Person">
		<property name="name" value="Space"></property>
		<property name="age" value="20"></property>
		<property name="cat" ref="tigerCat"></property> 
		
		<property name="list">
			<list>
				<value>XXX</value>
				<value>YYY</value>
				<value>ZZZ</value>
			</list>
		</property>
		<property name="map">
			<map>
				<entry key="name" value="Hi"></entry>
				<entry key="age" value="15"></entry>
			</map>
		</property>
		<property name="set">
			<set>
				<value>111</value>
				<value>222</value>
				<value>333</value>
			</set>
		</property>
		<property name="obj">
			<array>
				<value>一个字符串</value>
				<ref bean="tigerCat"/>
				<bean class="cn.hncu.domain.Cat">
					<property name="name" value="Tom"></property>
				</bean>
				<list>
					<value>aaa</value>
					<value>111</value>
				</list>
			</array>
		</property>
	</bean>

	<!-- 外部导入xml中的bean -->
	<import resource="2.xml"/>
</beans>

 

2.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:context="http://www.springframework.org/schema/context"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
	<bean id="tigerCat" class="cn.hncu.domain.Cat">
		<property name="name" value="Tiger"></property>
	</bean>

</beans>


Demo.java

package cn.hncu.demo;

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

import cn.hncu.domain.Person;

public class Demo {
	public static void main(String[] args) {
		//换种方式加载配置文件
		ApplicationContext ctx=new ClassPathXmlApplicationContext("cn/hncu/demo/applicationContext.xml");
		Person p=ctx.getBean(Person.class);
		p.getCat().eatFish();
		System.out.println(p);
	}
}


 

简单的学生登录:

 

StudDemo.java

package cn.hncu.stud;

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

import cn.hncu.stud.action.LoginAction;

public class StudDemo {
	public static void main(String[] args) {
		ApplicationContext ctx=new ClassPathXmlApplicationContext("cn/hncu/stud/stud.xml");
		LoginAction action=ctx.getBean(LoginAction.class);
		action.login();
	}
}


 

Stud.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:context="http://www.springframework.org/schema/context"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
	<bean  class="cn.hncu.stud.action.LoginAction">
		<property name="service" ref="service"></property>
	</bean>
	
	<bean id="service" class="cn.hncu.stud.service.IStudServiceImpl">
		<property name="orclDao" ref="orcl"></property>
	</bean>
	<!-- 如果想把改用jdbc方式访问,那么把该段的注释去掉 且 把上一段有关orcl的注入注释上
		<bean id="service" class="cn.hncu.stud.service.IStudServiceImpl">
			<property name="jdbcDao" ref="orcl"></property>
		</bean>
	 -->
	<bean id="jdbcDao" class="cn.hncu.stud.dao.IStudJdbcDao"></bean>
	<bean id="orclDao" class="cn.hncu.stud.dao.IStudOrclDao"></bean>
</beans>


LoginAction.java

package cn.hncu.stud.action;

import cn.hncu.stud.service.IStudService;

public class LoginAction {
	//依赖Spring注入service接口,可以不用再new 实现类,但是set和get方法必须要写
	private IStudService service=null;
	public void login(){
		service.login();
	}
	public IStudService getService() {
		return service;
	}
	public void setService(IStudService service) {
		this.service = service;
	}
	
}


IStudService.java

package cn.hncu.stud.service;

public interface IStudService {
	public void login();
}

 

IStudServiceImpl.java

package cn.hncu.stud.service;

import cn.hncu.stud.dao.IStudDAO;

public class IStudServiceImpl implements IStudService{
	//依赖Spring注入service接口,可以不用再new 实现类,但是set和get方法必须要写
	private IStudDAO dao=null;
	@Override
	public void login() {
		dao.login();
	}
	public IStudDAO getDao() {
		return dao;
	}
	public void setDao(IStudDAO dao) {
		this.dao = dao;
	}
	
}


 

IStudDAO.java

package cn.hncu.stud.dao;

public interface IStudDAO {
	public void login();
}


 

IStudJdbcDAO.java

package cn.hncu.stud.dao;

public class IStudJdbcDao implements IStudDAO{
	@Override
	public void login() {
		System.out.println("student  is   login  ----jdbc");
	}
}


 

IStudOrclDao.java

package cn.hncu.stud.dao;

public class IStudOrclDao implements IStudDAO{
	@Override
	public void login() {
		System.out.println("student  is   login  ----Orcl");
	}
}


 

有了Spring技术以后,实现service和dao的注入,就不再需要依赖实现类了(但要写接口的set和get方法),而且通过xml就可以配置,代码不需要改变,符合开闭原则。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值