初学Spring-XML文件配置Bean的一些知识点
2016-09-19 17:53
441 查看
<?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;
}
@Override
public 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 Cloud介绍 Part2: Spring Cloud与微服务
- spring aop 无法代理父类方法 的解决方法
- Spring MVC过滤器-超类 GenericFilterBean
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring AOP动态代理-切面
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- java 中Spring task定时任务的深入理解
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- 监听器获取Spring配置文件的方法
- Spring Boot 定制URL匹配规则的方法
- Java利用Sping框架编写RPC远程过程调用服务的教程