Bean配置

简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean

什么是Spring中的Bean?

Spring可以被看作是一个大型工厂,这个工厂的作用就是生产和管理Spring容器中的Bean。如果想要在项目中使用这个工厂,就需要开发者对Spring的配置文件进行配置,配置的时候要在配置文件中告诉它需要哪些Bean,以及需要使用何种方式将这些Bean装配到一起。

提示: Bean的本质就是Java中的类,而Spring中的Bean其实就是对实体类的引用,来生产Java类对象,从而实现生产和管理Bean 。

Spring容器支持XML和Properties两种格式的配置文件,在实际开发中,最常使用的就是XML格式的配置方式。这种配置方式通过XML文件来注册并管理Bean之间的依赖关系。

使用XML文件的形式对Bean的属性和定义进行详细的讲解。

在Spring中,XML配置文件的根元素是, 中包含了多个子元素,每一个子元素定义了一个Bean,并描述了该Bean如何被装配到Spring容器中。元素中同样包含了多个属性以及子元素,其常用属性及子元素如表所示。

表元素的常用属性及其子元素



表中只介绍了元素的一些常用属性和子元素,实际上元素还有很多属性和子元素,我们可以到网上查阅相关资料进行获取。

在Spring的配置文件中,通常一个普通的Bean只需要定义id(或name)和class两个属性即可,定义Bean的方式如下所示。

<?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">
<!--将指定类配置给Spring,让Spring创建其对象的实例 -->
<!-- 使用id 属性定义bean1,其对应的实现类为com.awen.instance.constructor.Bean1-->
<bean id="bean1" class="com.awen.instance.constructor.Bean1"/>
<!-- 使用name 属性定义bean1,其对应的实现类为com.awen.instance.static_factory.Bean2-->
<bean name="bean2" class="com.awen.instance.static_factory.Bean2"
</beans>

在上述代码中,分别使用id属性和name属性定义了两个Bean,并使用class元素指定其对应的实现类

注意:如果在Bean中未指定id和name,则Spring会将class值当作id使用。

  • 一般情况下,装配一个Bean时,通过指定一个id属性作为Bean的名称
  • id属性在IOC容器中必须是唯一的
  • 如果Bean的名称中含有特殊字符,就需要使用name属性

实例

1、复杂类型

public class Address {
private String address; public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
}
}

2、真实测试对象

public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
}

Address的对象address在Student类作为属性

3、beans.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.xsd"> <bean id="address" class="com.kuang.pojo.Address"/> <bean id="student" class="com.kuang.pojo.Student">
<!--第一种,普通值注入,value-->
<property name="name" value="憨批"/>
<!--第二种,Bean注入,ref-->
<property name="address" ref="address"/>
<!--数组注入-->
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
<!--List注入-->
<property name="hobbies">
<list>
<value>听歌</value>
<value>敲代码</value>
<value>看电影</value>
</list>
</property>
<!--Map-->
<property name="card">
<map>
<entry key="身份证" value="1555555555"/>
<entry key="银行卡" value="5555555555"/>
</map>
</property>
<!--Set-->
<property name="games">
<set>
<value>lol</value>
<value>wow</value>
</set>
</property>
<!--null-->
<property name="wife">
<null/>
</property>
<!--Properties-->
<property name="info">
<props>
<prop key="driver">com.mysql.jdbc.Driver</prop>
<prop key="url">jdbc:mysql://localhost:3306/news</prop>
<prop key="root">root</prop>
<prop key="password">123456</prop>
</props>
</property> </bean> </beans>

4、测试类

public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.getName());
}
}

【Spring】Spring中的Bean - 1、Baen配置的更多相关文章

  1. 7 -- Spring的基本用法 -- 5... Spring容器中的Bean;容器中Bean的作用域;配置依赖;

    7.5 Spring容器中的Bean 7.5.1 Bean的基本定义和Bean别名 <beans.../>元素是Spring配置文件的根元素,该元素可以指定如下属性: default-la ...

  2. 从Spring容器中获取Bean。ApplicationContextAware

    引言:我们从几个方面有逻辑的讲述如何从Spring容器中获取Bean.(新手勿喷) 1.我们的目的是什么? 2.方法是什么(可变的细节)? 3.方法的原理是什么(不变的本质)? 1.我们的目的是什么? ...

  3. Spring xml中进行面向切面的配置

    Spring xml中进行面向切面的配置 XML: <?xml version="1.0" encoding="UTF-8"?> <beans ...

  4. 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean

    7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...

  5. SpringBoot 之 普通类获取Spring容器中的bean

    [十]SpringBoot 之 普通类获取Spring容器中的bean   我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器 ...

  6. 获取Spring容器中的Bean协助调试

    在使用Spring进行开发时,有时调bug真的是很伤脑筋的一件事,我们可以通过自定义一个监听器来获取Spring容器中的Bean实例来协助我们调试. 第一步:编写自定义监听器 /** * 监听serv ...

  7. Spring Boot中普通类获取Spring容器中的Bean

    我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,自己动手n ...

  8. Spring Boot中只能有一个WebMvcConfigurationSupport配置类

    首先将结论写文章的最前面,一个项目中只能有一个继承WebMvcConfigurationSupport的@Configuration类(使用@EnableMvc效果相同),如果存在多个这样的类,只有一 ...

  9. 【String注解驱动开发】如何按照条件向Spring容器中注册bean?这次我懂了!!

    写在前面 当bean是单实例,并且没有设置懒加载时,Spring容器启动时,就会实例化bean,并将bean注册到IOC容器中,以后每次从IOC容器中获取bean时,直接返回IOC容器中的bean,不 ...

  10. 【String注解驱动开发】面试官让我说说:如何使用FactoryBean向Spring容器中注册bean?

    写在前面 在前面的文章中,我们知道可以通过多种方式向Spring容器中注册bean.可以使用@Configuration结合@Bean向Spring容器中注册bean:可以按照条件向Spring容器中 ...

随机推荐

  1. Vue-组件化,父组件传子组件常见传值方式

    前言 我们都知道vue核心之一:组件化,vue中万物皆组件,组件化我认为应该来至于模块化的设计思想,比如在模块化开发中,一个模块就是一个实现特定功能的独立的文件,有了模块我们就更方便去阅读代码,更方便 ...

  2. 参数文件恢复:RMAN-0617

    RMAN> restore spfile from autobackup; Starting restore at 03-APR-19using channel ORA_DISK_1using ...

  3. INS-06004 file operation on user's .ssh directory is not permitted

    在搭建rac的时候不能先ssh到另一台服务器,否则安装程序无法自动创建/home/oracle/.ssh这个目录,然后就会报INS-06004这个错误了. 原因:在/home/oracle/这个路径下 ...

  4. IOS中使用.xib文件封装一个自定义View

    1.新建一个继承UIView的自定义view,假设类名叫做 MyAppVew #import <UIKit/UIKit.h> @class MyApp; @interface MyAppV ...

  5. Linux下修改禅道端自定义端口号

    第一种方式 一.        首先,如果我们的服务器的80端口没有开放的话,那么我们就是只能修改Apache应用服务的端口了,其实非常简单,安装完成禅道后,在任意目录下输入命令: /opt/zbox ...

  6. Hbase各组件职责

    Hbase各组件职责 Client职责 1.HBase有两张特殊表: .META.:记录了用户所有表拆分出来的的Region映射信息,.META.可以有多个Regoin -ROOT-:记录了.META ...

  7. Django项目连接多个数据库配置

    1.设置数据库连接 pip install PyMySQL 2.在项目同名目录myproject/myproject下的__init__.py添加以下代码 import pymysql pymysql ...

  8. EF Core 三 、 骚操作 (导航属性,内存查询...)

    EF Core 高阶操作 本文之前,大家已经阅读了前面的系列文档,对其有了大概的了解 我们来看下EF Core中的一些常见高阶操作,来丰富我们业务实现,从而拥有更多的实现选择 1.EF 内存查找 wh ...

  9. 1.Redis基础命令

    重要概念 redis是单线程模型,所有命令都会进入一个队列,然后依次被执行. 全局命令 >>>select dbindex #切换数据库,默认有16个库,库标识符为0-15 > ...

  10. 服务器挂掉导致需要对k8s新增节点

    [导读]今日是周六,本想午休起来之后写篇有关kubernetes service的文章,没想到两台虚拟机接连挂掉,所以又重新创建了两台虚拟机.新建时又一直黑屏,所以只能克隆. 由于虚拟机是新建的,所以 ...