dao入门案例,alians(别名标签),工厂模式,单例和多例,懒加载,bean中配置初始化和销毁的方法,autowire(自动装配)
本文大多通过代码演示,讲解较为抽象,且创建bean容器的方式过于原始
对于初学者而言可能不易理解
jar包作用
spring-jdbc-3.2.17.RELEASE.jar编译好的.class文件,需要在编程时引入
spring-jdbc-3.2.17.RELEASE-sources.jar关于jdbc相关操作的源代码
dao入门案例(最常用的bean)
com.wiscom.domain.Person
public class Person { public Person(String name){ } public void eat(){ System.out.println("去北京吃烤鸭"); } public void drink(){ System.out.println("去青岛喝啤酒"); } }
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" xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <!-- 配置spring需要管理的类 --> <bean id="person" class="com.wiscom.domain.Person"></bean> </beans>
com.wiscom.test.TestDemo
public class TestDemo { @Test public void test01(){ Calendar c = Calendar.getInstance(); // 向spring要Person对应的对象 // 1.加载spring对应的xml文件(spring容器) ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // 2.通过id从spring容器中获取对象 Person p = (Person) context.getBean("person");// map.get(id); Person p1 = (Person) context.getBean("person"); System.out.println(p); System.out.println(p1); // 3.调用方法 p.eat(); p.drink(); } }
别名标签(alias)
<bean id="person" class="com.wiscom.beans.Person"></bean>
<alias name="person" alias="personx"/>
静态工厂模式
com.wiscom.domain.Person
public class Person { public Person(String name){ } public void eat(){ System.out.println("去北京吃烤鸭"); } public void drink(){ System.out.println("去青岛喝啤酒"); } }
com.wiscom.factory.PersonStaticFactory
package com.wiscom.factory; import com.wiscom.domain.Person; // 静态工厂 -->生成Person对象 public class PersonStaticFactory { public static Person getPerson(){ return new Person("df"); } }
applicationContext.xml
<!-- 配置spring需要管理的类 --> <!-- <bean id="person" class="com.wiscom.domain.Person"></bean> --> <!-- 配置静态工厂对应的类 --> <!-- <bean id="calendar" class="com.wiscom.factory.CalendarStaticFactory" factory-method="getInstance"></bean> --> <!-- 配置静态工厂对应的Person类 --> <bean id="person" class="com.wiscom.factory.PersonStaticFactory" factory-method="getPerson"></bean>
com.wiscom.test.TestDemo
public class TestDemo { @Test public void test02(){ // 加载spring容器 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // 获取对象 Person p = (Person) context.getBean("person"); p.eat(); } }
实例工厂创建对象
<bean id="calendarFactory" class="com.wiscom.factory.CalendarFactory"></bean>
<bean id="calendar" factory-bean="calendarFactory" factory-method="getCalendar"/>
Spring工厂创建对象(不常用)
实现
implements FactoryBean<T>接口
package com.wiscom.factory
public class CalendarSpringFactory implements FactoryBean<Calendar>{ /** * Spring工厂生产对象的方法 */ @Override public Calendar getObject() throws Exception { return Calendar.getInstance(); } /** * 获取当前工厂生产的对象的类型的方法 */ @Override public Class<?> getObjectType() { return Calendar.class; } /** * Spring工厂生产对象时是否采用单例模式 * 如果返回true,则在spring中该对象只创建一次 后续 重复使用 * 如果返回false,则每次获取该bean 都重新 创建对象 */ @Override public boolean isSingleton() { return true; } }
applicationContext.xml
<bean id="calendar" class="com.wiscom.factory.CalendarSpringFactory"></bean>
Test
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Calendar calendar = (Calendar) context.getBean("calendar"); System.out.println(calendar);
单例和多例
单例(singleton)
<bean id="person" class="com.wiscom.domain.Person" scope="singleton"></bean>
Spring默认的模式是单例模式
一个bean只能创建一个对象,创建多个对象时,得到的是同一对象
单例生命周期
解析xml文件时,单例的对象被创建()
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");容器退出时xml文件被销毁
多例(prototype)
<bean id="person" class="com.wiscom.domain.Person" scope="prototype"></bean>
一个bean可以创建多个对象
多例生命周期
Bean被调用时对象创建
销毁由用户自己决定什么时候销毁
懒加载机制(lazy-init)
<bean id="cart" class="com.wiscom.beans.Cart" lazy-init="true"></bean>指定bean配置
default-lazy-init="true"全局配置
懒加载机制针对的是单例模式
一旦bean非常多时,spring需要在启动的过程中花费大量的时间来创建bean
懒加载机制就是可以规定指定的bean不在启动时立即创建,而是在后续第一次用到时才创建
bean中配置初始化和销毁时的方法
<bean id="prodDao" class="com.wiscom.dao.ProdDao" init-method="initFunc" destroy-method="destroyFunc"></bean>
com.wiscom.dao.ProdDao
package com.wiscom.dao; public class ProdDao { public ProdDao(){ System.out.println("开始创建对象"); } public void initFunc(){ System.out.println("数据库开始连接..."); } public void destroyFunc(){ System.out.println("数据库关闭连接..."); } }
Test
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); ProdDao dao = (ProdDao) context.getBean("prodDao"); System.out.println(dao); // 关闭上下文 context.close();
IOC(DI):控制反转(依赖注入)
创建对象的过程中Spring可以依据配置对对象的属性进行设置,这个过称之为依赖注入,也即DI。
package com.wiscom.domain; public class Cat { private int age; private String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Cat(int age, String name) { this.age = age; this.name = name; } public Cat() { } @Override public String toString() { return "Cat [age=" + age + ", name=" + name + "]"; } }
com.wiscom.domain.Dog省略
com.wiscom.domain.Hero
package com.wiscom.domain; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class Hero { private int age; private String name; private List<String> jobs; private Set<String> skills; private Map<String,String> map; private Properties porp; private Dog dog; private Cat cat; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<String> getJobs() { return jobs; } public void setJobs(List<String> jobs) { this.jobs = jobs; } public Set<String> getSkills() { return skills; } public void setSkills(Set<String> skills) { this.skills = skills; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public Properties getPorp() { return porp; } public void setPorp(Properties porp) { this.porp = porp; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } public Cat getCat() { return cat; } public void setCat(Cat cat) { this.cat = cat; } public Hero(int age, String name, List<String> jobs, Set<String> skills, Map<String, String> map, Properties porp, Dog dog, Cat cat) { super(); this.age = age; this.name = name; this.jobs = jobs; this.skills = skills; this.map = map; this.porp = porp; this.dog = dog; this.cat = cat; } public Hero() { } @Override public String toString() { return "Hero [age=" + age + ", name=" + name + ", jobs=" + jobs + ", skills=" + skills + ", map=" + map + ", porp=" + porp + ", dog=" + dog + ", cat=" + cat + "]"; } }
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" xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <!-- 使用DI --> <bean id="hero" class="com.wiscom.domain.Hero"> <property name="age" value="19"></property> <property name="name" value="虚竹"></property> <property name="jobs"> <list> <value>和尚</value> <value>驸马</value> <value>公主</value> </list> </property> <property name="skills"> <set> <value>天山折梅手</value> <value>生死符</value> <value>降龙十八掌</value> </set> </property> <property name="map"> <map> <entry key="于谦" value="郭德纲"></entry> <entry key="赵本山" value="宋丹丹"></entry> <entry key="周杰伦" value="方文山"></entry> </map> </property> <property name="porp"> <props> <prop key="高富帅">20</prop> <prop key="曹阳">19</prop> <prop key="李帅">18</prop> </props> </property> <property name="dog" ref="dog"></property> <property name="cat" ref="cat"></property> </bean> <!-- 将狗和猫交由spring进行创建对象 --> <bean id="dog" class="com.wiscom.domain.Dog"> <property name="age" value="8"></property> <property name="name" value="旺财"></property> </bean> <bean id="cat" class="com.wiscom.domain.Cat"> <property name="age" value="3"></property> <property name="name" value="进宝"></property> </bean> </beans>
Test
package com.wiscom.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wiscom.domain.Hero; public class TestDemo { @Test public void test01(){ // 加载Spring容器 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // 获取英雄 Hero hero = (Hero) context.getBean("hero"); System.out.println(hero); } }
自动装配(autowire)
bean中自动装配
autowire="byName"建议使用,根据id自动装配
autowire="byType"不建议使用,根据类名自动装配
全局自动装配
default-autowire="byName"
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd" default-autowire="byName" > <!-- 使用DI dog cat byType:根据对应属性的类型去spring容器中找寻对应的类型,如果找到,那么自动装配 byName:根据对应属性的名称去spring容器中找寻对应的id,如果找到,那么自动装配 推荐使用byName --> <bean id="hero" class="com.wiscom.domain.Hero" autowire="byName"> </bean> <!-- 将狗和猫交由spring进行创建对象 --> <bean id="dog" class="com.wiscom.domain.Dog"> <property name="age" value="8"></property> <property name="name" value="旺财"></property> </bean> <bean id="cat" class="com.wiscom.domain.Cat"> <property name="age" value="3"></property> <property name="name" value="进宝"></property> </bean> </beans>

浙公网安备 33010602011771号