spring基础学习二(配置文件)
2011-12-04 18:50
477 查看
目录:
1、bean的注入方式
2、bean的范围(singleton/scope)
3、生命集合的
4、自动装配(byName,byType)
5、bean的生命周期
内容:
1、bean的注入方式
当在一个类中要使用其他类的对象时,如何引用到这个自己的,类中呢?
(1)setter方法
<bean id="aDAO" class="com.dao.impl.ADAO"/>
<bean id="aService" class="com.service.impl.AService">
<property ref="aDAO">
</bean>
(2)带参构造函数constructor
<bean id="aDAO" class="com.dao.impl.ADAO"/>
<bean id="aService" class="com.service.impl.AService">
<constructor-arg>
<ref="aDAO"/>
</constructor-arg>
</bean>
在AService中:
class AService{
private ADAO aDAO;
AService(ADAO aDAO){
this.aDAO = aDAO;
}
}
2、bean的范围(singleton/prototype)
<bean id="aService" class="com.service.impl.AService" scope="prototype">//默认方式singleton
3、生命集合的
(1)list的注入
<bean id="list" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>value1</value>
<value>value2</value>
</list>
</constructor-arg>
</bean>
(2) map 的注入
<bean id="map" class="java.util.HashMap">
<constructor-arg>
<map>
<entry key="key1" value="value1"/>
<entry key="key2" value="value2"/>
</map>
</constructor-arg>
</bean>
(3) set的注入
<bean id="set" class="java.util.HashSet">
<constructor-arg>
<set>
<value>value1</value>
<value>value2</value>
</set>
</constructor-arg>
</bean>
4、自动装配(byName,byType)
<bean id="aService" class="com.service.impl.AService" autowire="byName"/>
<bean id="aService" class="com.service.impl.AService" autowire="byType"/>
使用事例:
bean.xml中:
其中的get、set方法是必须的。
5、bean的生命周期
<bean id="aService" class="com.service.impl.AService" init-method="init" destroy-method="destroy"/>
含义就是在初始化aService这个对象时,先执行AService类中的init()方法,在销毁这个对象时,执行AService类中的destroy()这个方法。
1、bean的注入方式
2、bean的范围(singleton/scope)
3、生命集合的
4、自动装配(byName,byType)
5、bean的生命周期
内容:
1、bean的注入方式
当在一个类中要使用其他类的对象时,如何引用到这个自己的,类中呢?
(1)setter方法
<bean id="aDAO" class="com.dao.impl.ADAO"/>
<bean id="aService" class="com.service.impl.AService">
<property ref="aDAO">
</bean>
(2)带参构造函数constructor
<bean id="aDAO" class="com.dao.impl.ADAO"/>
<bean id="aService" class="com.service.impl.AService">
<constructor-arg>
<ref="aDAO"/>
</constructor-arg>
</bean>
在AService中:
class AService{
private ADAO aDAO;
AService(ADAO aDAO){
this.aDAO = aDAO;
}
}
2、bean的范围(singleton/prototype)
<bean id="aService" class="com.service.impl.AService" scope="prototype">//默认方式singleton
3、生命集合的
(1)list的注入
<bean id="list" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>value1</value>
<value>value2</value>
</list>
</constructor-arg>
</bean>
(2) map 的注入
<bean id="map" class="java.util.HashMap">
<constructor-arg>
<map>
<entry key="key1" value="value1"/>
<entry key="key2" value="value2"/>
</map>
</constructor-arg>
</bean>
(3) set的注入
<bean id="set" class="java.util.HashSet">
<constructor-arg>
<set>
<value>value1</value>
<value>value2</value>
</set>
</constructor-arg>
</bean>
4、自动装配(byName,byType)
<bean id="aService" class="com.service.impl.AService" autowire="byName"/>
<bean id="aService" class="com.service.impl.AService" autowire="byType"/>
使用事例:
bean.xml中:
<beans> <bean id="category" class="java.util.TreeMap"> <constructor-arg> <map> <entry key="1001" value="餐饮美食"></entry> <entry key="1002" value="休闲娱乐"/> </map> </constructor-arg> </bean> <!--上面这个就是使用了带参数的构造函数,初始化category这个对象的。 <bean id="testapp" class="com.nulltest.Test"> </bean> <bean id="spring1" class="com.spring.app.Spring1" autowire="byName"> <!-- <property name="category" ref="category"></property> --> <!-- <property name="testapp" ref="testapp"/> --> </bean> </beans>java代码中:
public class Spring1 { private com.nulltest.Test testapp; private TreeMap<String , String> category; public com.nulltest.Test getTestapp() { return testapp; } public void setTestapp(com.nulltest.Test testapp) { this.testapp = testapp; } public TreeMap<String, String> getCategory() { return category; } public void setCategory(TreeMap<String, String> category) { this.category = category; } @SuppressWarnings("unchecked") public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); TreeMap<String, String> cate = (TreeMap<String, String>) context.getBean("category"); Test testa = (Test) context.getBean("testapp"); System.out.println(cate.get("1001")); Test.test(new SnowRobot("snow..")); testa.displaySnow(); System.out.println("******************************************"); Spring1 spring1 = (Spring1) context.getBean("spring1"); System.out.println(spring1.getCategory().get("1002")); spring1.getTestapp().displaySnow(); Spring1 spring2 = (Spring1) context.getBean("spring1"); System.out.println(spring1.hashCode()); System.out.println(spring2.hashCode()); } public void init(){ System.out.println("init..."); } public void destroy(){ System.out.println("destroy..."); }
}
其中的get、set方法是必须的。
5、bean的生命周期
<bean id="aService" class="com.service.impl.AService" init-method="init" destroy-method="destroy"/>
含义就是在初始化aService这个对象时,先执行AService类中的init()方法,在销毁这个对象时,执行AService类中的destroy()这个方法。
相关文章推荐
- Spring基础学习教程(Spring配置文件详解)-03
- Spring基础学习教程(applicationContext.xml 配置文件 详解)-07
- SpringBoot 基础知识学习(二)——配置文件多环境配置
- JavaEE进阶知识学习-----SpringBoot基础知识-2-配置文件知识
- Spring Boot 基础知识学习(二)——配置文件多环境配置
- Spring4学习笔记-声明式事务(基于配置文件的方式)
- Spring学习----------AOP以及Spring配置文件详解
- Spring 学习(三) 配置文件详解
- Spring学习笔记(18)----使用Spring配置文件实现事务管理
- spring学习-引入配置文件
- (spring-第5回【IoC基础篇】)spring容器从加载配置文件到实例化bean的内部工作机制
- Spring学习基础---一个简单的配置案例
- Spring源码学习--Spring配置文件解析BeanDefinitionReader(二)
- Spring学习总结(3)——Spring配置文件详解
- Spring AOP基础 配置文件方式
- spring源码学习之:xml配置文件标签自定义
- spring-security学习笔记--配置文件
- 程序开发基础学习五(json配置、解析文件,c++篇)
- Spring学习1--配置文件的命名空间
- SpringBoot学习<二>——SpringBoot的默认配置文件application和多环境配置