`
1140566087
  • 浏览: 548301 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
博客专栏
2c4ae07c-10c2-3bb0-a106-d91fe0a10f37
c/c++ 入门笔记
浏览量:18103
3161ba8d-c410-3ef9-871c-3e48524c5263
Android 学习笔记
浏览量:309740
Group-logo
J2ME 基础学习课程集
浏览量:18041
A98a97d4-eb03-3faf-af96-c7c28f709feb
Spring 学习过程记录...
浏览量:17212
社区版块
存档分类
最新评论

Spring Bean工厂和上下文

阅读更多
package com.svse.entity;

import java.io.Serializable;
import java.util.List;

public class AdminEntity implements Serializable {
	
	private String college;
	private String tel;
	private List ar;
	public String getCollege() {
		return college;
	}
	public void setCollege(String college) {
		this.college = college;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	public List getAr() {
		return ar;
	}
	public void setAr(List ar) {
		this.ar = ar;
	}
}


package com.svse.entity;

import java.io.Serializable;

public class UserEntity implements Serializable {
	private int id;
	private String name;
	private AdminEntity admin;
	
	public UserEntity(){}
	public UserEntity(int id ,String name,AdminEntity admin){
		this.id = id;
		this.name = name;
		this.admin = admin;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public AdminEntity getAdmin() {
		return admin;
	}

	public void setAdmin(AdminEntity admin) {
		this.admin = admin;
	}

}


<?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-3.0.xsd">

	<!-- 1、解释:AdminEntity adminEntity = new AdminEntity(); -->
	<bean id="adminEntity" class="com.svse.entity.AdminEntity">

		<!-- AdminEntity 类中有个属性为:college 的,赋值为:value="" -->
		<property name="college" value="湖北国土资源职业学院"></property>
		<property name="tel" value="1533*******"></property>
		<!-- 为集合赋值List -->
		<property name="ar">
			<list>
				<value>上网</value>
				<value>吃饭</value>
				<value>睡觉</value>
			</list>
		</property>
	</bean>

	<!-- 解释:==UserEntity userEntity = new UserEntity(); -->
	<bean id="userEntity" class="com.svse.entity.UserEntity">
		<property name="id" value="1"></property>
		<property name="name">
			<value>小王八蛋</value>
		</property>
		<!-- 属性名:admin,为其依赖注入 adminEntity== admin = new AdminEntity(); 并进行赋值 -->
		<property name="admin" ref="adminEntity"></property>
	</bean>

	<!--
		2、bean 中的属性介绍: abstract="true" 设置该bean对应的类为抽象类,不能直接进行使用
		parent="id标识,继承的父类"
	-->

	<!--
		===========================定义UserEntity 为抽象类==========================
	-->
	<bean id="userEntity1" class="com.svse.entity.UserEntity"
		abstract="true">
		<property name="id" value="123"></property>
		<property name="name" value="谢逊"></property>
	</bean>
	<!--
		通过parent这个属性集成:抽象类(根据ID标识找到需要继承的类),继承了对应类中的属性,也可以随时的根据自己的需要更改相关的属性
		不需要更改的可以直接的进行引用;
	-->
	<bean id="xx" parent="userEntity1">
		<property name="id" value="456"></property>
	</bean>
	<!--============================================================== -->

	<!-- 3、bean中属性:autowire的用法 ,以及null值的设置-->
	<!--
		+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	-->
	<bean id="admin" class="com.svse.entity.AdminEntity">

		<!-- 将college属性值设为:"" -->
		<property name="college">
			<value></value>
		</property>
		<!-- 将tel的属性值设为:null -->
		<property name="tel">
			<null />
		</property>
		<property name="ar">
			<list>
				<value>八嘎</value>
				<value>九噶</value>
			</list>
		</property>
	</bean>
	<bean id="userEntity3" class="com.svse.entity.UserEntity"
		autowire="byName">
		<property name="id" value="3"></property>
		<property name="name">
			<value>小红</value>
		</property>
		<!--
			该实体下包含一个对象名称为:admin的对象,设置了autowire 然后自动的寻找到id名为admin的bean,实现自动匹配
		-->
	</bean>
	<!--
		+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	-->

	<!-- 4、bean的name属性、depends-on="标识ID名" -->
	<!--
		+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	-->
	<!--
		bean中的ID 是唯一标识,而name 能多名称标识,相当于给同一个bean起多个名称,可以根据任意的一个别名获取到当前
		的bean,命名方式如:name="/user,yy,xx,aa" , id="xx"
		代码中可以根据任意你喜欢的名称(user,yy,xx,aa)获取,而 id只能根据xx获取; depends-on: 通过ID
		指定依赖的对象,被依赖者执行之后才会执行本身;例如:处理数据之前必须先链接数据库;
	-->
	<!--
		+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	-->

	<!-- 5、通过xml文件给实体中构造方法赋值 传递参数值,以及传递实体对象 -->
	<!--
		=============================================================================================
	-->
	<bean id="adminEntity1" class="com.svse.entity.AdminEntity">
		<property name="college" value="工业大学"></property>
		<property name="tel">
			<value></value>
		</property>
		<property name="ar">
			<null />
		</property>
	</bean>
	<bean id="userEntity4" class="com.svse.entity.UserEntity">
		<constructor-arg index="0" value="1"></constructor-arg>
		<constructor-arg index="1" value="大奖人"></constructor-arg>
		<constructor-arg index="2" ref="adminEntity1"></constructor-arg>
	</bean>
	<!--
		=============================================================================================
	-->

	<!-- 6、BeanFactory 的作用域 ,scope属性-->
	<!--
		scope="singleton" : 单例模式 ,查询一次后数据存入内存,然后常驻内存,再次查询的时候仍然是内存中的数据,
		数据没有和数据库同步; scope="prototype" :代理模式 , 只负责封装数据,能保证页面数据和数据库的同步
	-->
	<bean id="adminEntity6" class="com.svse.entity.AdminEntity"></bean>
	<bean id="userEntity6" class="com.svse.entity.UserEntity" scope="singleton"></bean>
	<!-- 單例模式下:
		UserEntity user1 = app.getBean("userEntity6",UserEntity.class);
		UserEntity user2= app.getBean("userEntity6",UserEntity.class);
		此時:user1==user2   : true
	 -->
	<bean id="userEntity7" class="com.svse.entity.UserEntity" scope="prototype"></bean>
	<!-- 代理模式下:
		UserEntity user1 = app.getBean("userEntity7",UserEntity.class);
		UserEntity user2= app.getBean("userEntity7",UserEntity.class);
		此時:user1!=user2   : false
	 -->
	 <!-- ==================================================================================== -->




</beans>
0
4
分享到:
评论

相关推荐

    通过Spring上下文获取bean的实例对象

    通过Spring上下文获取bean的实例对象

    Spring 应用上下文获取 Bean 的常用姿势实例总结

    主要介绍了Spring 应用上下文获取 Bean,结合实例形式总结分析了Spring 应用上下文获取 Bean的实现方法与操作注意事项,需要的朋友可以参考下

    springIOC核心组件分析.vsdx

    spring-context:上下文,即IOC容器 spring-context-support:对IOC的扩展,以及IOC子容器 spring-context-indexer:类管理组件和Classpath扫描 spring-expression:表达式语句 切面编程: spring-aop:面向切面编程,...

    spring的bean作用域

    讲解了spring的6种作用域:singleton(单例)、non-singleton(也称 prototype),Spring2.0以后,增加了session、request、global session三种专用于Web应用程序上下文的Bean

    springboot 使用上下文获取bean

    主要介绍了springboot 使用上下文获取bean,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    让spring解决控制springboot中bean的加载顺序的问题.docx

    只需要把需要注册进容器的bean声明为@Component即可,spring会自动扫描到这个Bean完成初始化并加载到spring上下文容器。 而当你在项目启动时需要提前做一个业务的初始化工作时,或者你正在开发某个中间件需要完成...

    java面试Spring.pdf

    1.3 详细讲解一下核心容器(spring context应用上下文) 模块 2. Spring俩大核心概念 IOC,Inversion of Control,控制反转 AOP(Aspect-OrientedProgramming),面向切面编程 Spring AOP里面常用名词的概念: Spring...

    spring-spock-integration-testing:如何将Spock模拟注入到Spring集成测试中

    有时,您想使用整个Spring上下文进行更完整的集成测试。 这通常会导致您宁愿嘲笑的少数问题点。 在Spring Boot 1.4中,他们引入了很多很酷的测试玩具,包括@MockBean 。 遗憾的是,仅在使用JUnit进行测试时才支持。...

    Spring面试题含答案.pdf

    4. 核心容器(应用上下文) 模块 5. BeanFactory – BeanFactory 实现举例 6. XMLBeanFactory 7. 解释 AOP 模块 8. 解释 JDBC 抽象和 DAO 模块 9. 解释对象/关系映射集成模块 10. 解释 WEB 模块 12. Spring 配置文件 ...

    Spring Security、Spring Social 、Spring Security OAuth

    它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制...

    SpringSecurity.zip

    它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制...

    Spring启动流程.java

    AbstractApplicationContext.refresh(){ 1.为刷新准备此上下文 prepareRefresh(){ ...配置工厂的标准上下文特征,例如上下文的类加载器和后置处理器。 prepareBeanFactory(beanFactory){ . . .

    Spring in Action(第2版)中文版

    2.1.2使用应用上下文 2.1.3bean的生命 2.2创建bean 2.2.1声明一个简单的bean 2.2.2通过构造函数注入 2.3注入bean属性 2.3.1注入简单的数值 2.3.2使用其他的bean 2.3.3装配集合 2.3.4装配空值 2.4自动装配 ...

    基于java的企业级应用开发:Spring AOP简介.ppt

    support模块和SpEL(Spring Expression Language,Spring表达式语言)模块组成,具体介绍如下: Beans模块:提供了BeanFactory,是工厂模式的经典实现,Spring将管理对象称为Bean。 Core核心模块:提供了Spring框架...

    spring源代码解析

    /** 这是和web部署相关的位置信息,用来作为默认的根上下文bean定义信息的存放位置*/ public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml"; public static final String ...

    Spring in Action(第二版 中文高清版).part2

    2.1.2 使用应用上下文 2.1.3 Bean的生命 2.2 创建Bean 2.2.1 声明一个简单的Bean 2.2.2 通过构造函数注入 2.3 注入Bean属性 2.3.1 注入简单的数值 2.3.2 使用其他的Bean 2.3.3 装配集合 2.3.4 装配空值 ...

    Spring in Action(第二版 中文高清版).part1

    2.1.2 使用应用上下文 2.1.3 Bean的生命 2.2 创建Bean 2.2.1 声明一个简单的Bean 2.2.2 通过构造函数注入 2.3 注入Bean属性 2.3.1 注入简单的数值 2.3.2 使用其他的Bean 2.3.3 装配集合 2.3.4 装配空值 ...

    Spring Boot 集成 WebSocket(原生注解与Spring封装方式)

    在通过两种方式集成的过程中会摘取整体框架中的核心逻辑,简化代码实现过程,保留核心功能,例如:IOC、AOP、Bean生命周期、上下文、作用域、资源处理等内容实现。 适合人群:具备一定编程基础,工作1-3年的研发人员...

    spring -core

    common-logging 日志 spring-beans bean节点 spring-context spring 上下文 spring-core 核心jar 包 spring-expression spring 表达式相关表

    使用Java配置进行Springbean管理

    的配置,通过利用应用程序上下文XML文件来管理bean依赖性。此文件处于应用程序的外部,包含bean 及其与该应用程序的依赖项的定义。尽管使用XML配置较为简单和便捷,但仍有另外一种方法可定义bean

Global site tag (gtag.js) - Google Analytics