初学Spring-XML文件配置Bean的一些知识点
来源:互联网 发布:黑道圣徒捏脸数据 编辑:程序博客网 时间:2024/05/16 04:46
<?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.xsd"> <bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --> </bean> <bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --> </bean> <!-- more bean definitions go here --></beans>
以上代码摘抄自官方文档。
1.构造注入,setter注入
(1)setter注入,个人代码示例
<bean id="card1" class="beans.Card"><property name="id" value="1"></property><property name="number"><null/></property></bean>
(2)构造注入,个人代码示例
<!-- 依赖构造注入可以指定index 和type类型,让属性自动匹配,他们可以混合使用 --><bean id="animal" class="beans.Animal"><constructor-arg value="TOM" index="0"></constructor-arg><constructor-arg value="1000" type="double"></constructor-arg><constructor-arg value="cat" index="2"></constructor-arg></bean>
(3)内部bean
<bean id="worker" class="beans.Worker"><property name="name" value="sam"></property><property name="card" ><!-- 内部bean 只能被自己引用 --> <bean id="card1" class="beans.Card"><property name="id" value="1"></property><property name="number"><null/></property></bean></property></bean>
(4)集合属性配置
<bean id="classroom" class="beansUtil.ClassRoom"><property name="id" value="1"></property><property name="key" value="hello java"></property><property name="students"><list><ref bean="student1"/><ref bean="student2"/></list></property></bean><bean id ="teacher" class="beansUtil.Teacher"><property name="name" value="sam"></property><property name="map"><map><entry key="1" value-ref="student1"></entry></map></property></bean><bean id="pro1" class="beansUtil.pro"><property name="properties"><props><prop key="username">root</prop><prop key="password">123</prop></props></property></bean>
(5)初识scope属性
<bean id="test1" class="beans.autowire.testScope" scope="prototype"><property name="id" value="1"></property></bean>
常用有:prototype singleton(单例,缺省值)
可以使用一个demo来认识scope属性
package beans.autowire;public class testScope {private Integer id;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}@Overridepublic String toString() {return "testScope [id=" + id + "]";}public testScope(){System.out.println("測試scope");}}
package beans.autowire;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class util {public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext3.xml");System.out.println(applicationContext);Student student = (Student)applicationContext.getBean("student");System.out.println(student);ClassRoom classRoom = (ClassRoom)applicationContext.getBean("classroom");System.out.println(classRoom);/* * 如果scope设置为singleton的话是单例模式,那么当applicationContext对象被初始化时,则会自动初始化bean * 如果选择prototype则是需要就new */testScope scope1 = (testScope) applicationContext.getBean("test1");testScope scope2= (testScope) applicationContext.getBean("test1");}}
- 初学Spring-XML文件配置Bean的一些知识点
- spring中bean的一些知识点
- spring中bean的一些知识点
- Spring bean.xml的配置
- Spring Bean的XML配置
- Spring的xml配置bean文件原理-[Java反射机制]
- Spring 3.0 中bean.xml文件的配置问题
- 简化Spring的xml文件配置-自动装配Bean属性
- 简化Spring的xml文件配置-自动检测bean
- spring配置bean.xml头文件模板
- Spring在xml文件中配置bean
- Spring在xml文件中配置bean
- 初学Spring遇到的第一个问题:bean.xml
- 初学Spring遇到的第一个问题:bean.xml
- Spring中bean的基本xml配置
- Spring中bean的基本xml配置
- Spring中bean的基本xml配置
- Spring中bean的基本xml配置
- Android的消息机制
- poi 内容乱码解决方法
- 大牛们是怎么阅读 Android 系统源码的?
- 花好月圆,Angular2正式发布!
- IOS10 导航栏异常问题
- 初学Spring-XML文件配置Bean的一些知识点
- Django1.8学习笔记总结(基础+settings)
- Android动画深入分析
- 如何在百宝云应用中心发布自己的应用
- mybatis学习(2)---建立Demo
- Jni中C++和Java的参数传递
- ButterKnife & Zelezny插件基本使用
- Hdu5893树链剖分(2016沈阳网络赛B)
- 腾讯2017暑期实习生编程题之我见