Spring编程<二>
用纯JAVA方式演示—-容器与配置文件
导入需要用到的包:
写一个配置文件:
//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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 默认是单例-->
<bean id="p1" class="cn.hncu.domain.Person" >
<property name="name" value="Tom"></property>
<property name="age" value="22"></property>
</bean>
<!-- prototype原型,每次拿的都是新的 -->
<bean id="p2" class="cn.hncu.domain.Person" scope="prototype">
<property name="name" value="Jack"></property>
<property name="age" value="20"></property>
</bean>
</beans>
演示代码1:
@Test
public void demo1() {
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
Person p1 = context.getBean("p1", Person.class);
System.out.println(p1);
Person p2 = context.getBean("p1", Person.class);//参数1为XML的Bean中的ID属性
System.out.println(p2);
System.out.println(p1==p2);// Spring默认是单例,所以是true
}
@Test
public void demo2() {
ApplicationContext ctx =
new ClassPathXmlApplicationContext("applicationContext.xml");
Person p1 = ctx.getBean("p2", Person.class);
Person p2 = ctx.getBean("p2", Person.class);
System.out.println(p1);
System.out.println(p1==p2);
}
演示结果1:
demo1:
demo2:
演示如何在配置文件中导入其他配置文件
<!--demo2.xml -->
<!-- 属性变量是一个Bean -->
<!-- ref 依赖注入 -->
<property name="cat" ref="cat2"></property>
<!-- 导入其他配置文件 -->
<import resource="cat.xml" />
必须写SET,GET函数,空参构造
//cat.java
private String name;
private String hobby;
public Cat(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "Cat [name=" + name + ", hobby=" + hobby + "]";
}
<!-- cat.xml -->
<bean id="cat1" class="cn.hncu.demo2.Cat">
<property name="name" value="Tom"></property>
<property name="hobby" value="上天"></property>
</bean>
<bean id="cat2" class="cn.hncu.demo2.Cat">
<property name="name" value="R"></property>
<property name="hobby" value="唱歌"></property>
</bean>
Demo2:
不需要在里面New对象Cat
@Test
public void t1(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/demo2/demo2.xml");
User u = ctx.getBean("user", User.class);
System.out.println(u);
}
演示结果:
配置文件中复杂属性变量:List等。
<!-- 属性变量是一个list,map,set -->
<property name="addrs">
<list>
<value>益阳</value>
<value>长沙</value>
<value>湘潭</value>
</list>
</property>
<property name="map">
<map>
<entry key="name" value="pwd" />
<entry key="age" value="20" />
</map>
</property>
<property name="set">
<set>
<value>111</value>
<value>222</value>
<value>333</value>
</set>
</property>
<!-- 属性值是一个Object[]的数组,集合嵌套 -->
<property name="objs">
<array>
<value>一个字符串</value>
<ref bean="cat1" />
<!-- 内部bean (匿名) -->
<bean class="cn.hncu.demo2.Cat">
<property name="name" value="恶汉猫"></property>
<property name="hobby" value="PK"></property>
</bean>
</array>
</property>
模拟MVC框架DAO与Service注入:
以前注入都是New一个,现在全部写NULL,并写好SET,GET函数
配置文件:
<!-- 全部用关系ref,连起来 -->
<bean id="mysqlDao" class="cn.hncu.demo3.dao.StudDaoJdbc" >
</bean>
<bean id="oracleDao" class="cn.hncu.demo3.dao.StudDaoOracle" >
</bean>
<!-- 依赖注入 -->
<bean id="service" class="cn.hncu.demo3.service.StudServiceImpl">
<property name="dao" ref="mysqlDao"></property>
</bean>
<bean id="action" class="cn.hncu.demo3.action.StudAction">
<property name="service" ref="service"></property>
</bean>
Spring下载地址:
http://repo.springsource.org/libs-release-local/org/springframework/spring/