spring bean配置记录
scope: singleton, prototype, request, session, globle sesssion
默认为singleton
id和name的区别:
id是唯一标识bean.不能用特殊字符:×#@ ,不能用数字开头。在bean引用的时候只能有id指向你需要的bean
name 可以用特殊字符,并且一个bean可以用多个名称:name=“bean1,bean2,bean3” ,用逗号隔开。如果没有id,则name的第一个名称默认是id
通过id和name都可以取出该Bean.
<bean id=“thisbean” name=“bean1,bean2,bean3” class=“org.example.Dao" />
BeanFactory factory=new XmlBeanFactory(new ClassPathResource("config.xml"));
bean definition inheritance(bean 定义继承)
1 配置文件继承
<bean id="baseDaoImpl" abstract="true"> <property name="name" value="baseName"></property> <property name="age" value="11"></property> </bean> <bean id="testDaoImpl" class="main.daoImpl.TestDaoImpl" parent="baseDaoImpl"> <property name="age" value="1"></property> </bean>
如上所示:testDaoImpl的bean配置了baseDaoImpl的配置,bean没有指定具体class,如果子配置没有配置具体的属性,则从父配置中继承;如果配置了则覆盖父配置中的值
2 类继承配置
java类:
public class BaseDaoImpl extends HibernateDaoSupport { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } public class TestDaoImpl extends BaseDaoImpl { }
对应spring配置:
<bean id="baseDaoImpl" class="main.daoImpl.BaseDaoImpl" abstract="true"> <property name="name" value="baseName"></property> <property name="age" value="11"></property> <property name="sessionFactory" ref="mySessionFactory"></property> </bean> <bean id="testDaoImpl" class="main.daoImpl.TestDaoImpl" parent="baseDaoImpl"> <property name="age" value="1"></property> </bean> <bean id="testService" class="main.service.TestService"> <constructor-arg index="0" ref="testDaoImpl"></constructor-arg> </bean>
测试代码:
@Test public void testA() throws IOException { try { TestService testService = (TestService)context.getBean("testService"); TestDaoImpl testDaoImpl = testService.getTestDaoImpl(); System.out.println("name: " + testDaoImpl.getName() + " age:" + testDaoImpl.getAge() + " sessionFactory:" + testDaoImpl.getSessionFactory()); } catch (Exception e) { e.printStackTrace(); } finally { } }
测试结果:name: baseName age:1 sessionFactory:org.hibernate.impl.SessionFactoryImpl@b02928
baeDaoImpl类在配置文件中配置成了abstract,不能再进行实例化,如果尝试去实例化会报错!
相关推荐
创建SpringBean配置工具类(安全)如: <bean id=... scope="prototype"></bean>
spring bean配置 运行环境:eclipse 构建工具:maven 不提供maven构建,maven用来解决jar包的依赖
Spring的Bean配置Spring的Bean配置说明说明
Spring bean 一般通过配置文件和注解进行加载,如果要实现jar或class文件,动态实现spring bean 的动态加载,并通过UrlClassLoader完成jar和class文件的加载。可以实现jar的热替换。spring的bean动态加载则需要对...
Spring IOC和DI概述,Bean的配置形式,IOC容器BeanFactory和ApplicationContext概述,依赖注入的方式,属性注入,构造器注入等案例
3、通过spring的xml bean配置或bean注解指定初始化方法,如下面实例的initMethod方法通过@bean注解指定。 销毁的时候实现的方法 1、通过java提供的@PreDestroy注释; 2、通过实现spring提供的DisposableBean接口,并...
SPRING:bean配置properties
spring的Bean配置说明
本篇文章主要介绍了Spring 中如何控制2个bean中的初始化顺序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
用于在使用spring框架的时候,放在src/java/resource的目录下,对spring的bean进行配置。
Spring,Bean调用,事务,AOP,配置详细说明 提升开发效率
spring xml配置的12個技巧 spring xml配置的12個技巧 spring xml配置的12個技巧
从本质上来说,Spring Boot就是Spring,它做了那些没有它你也会去做的Spring Bean配置。它使用“习惯优于配置”(项目中存在大量的配置,此外还内置了一个习惯性的配置,让你无需手动进行配置)的理念让你的项目快速...
从本质上来说,Spring Boot就是Spring,它做了那些没有它你也会去做的Spring Bean配置。它使用“习惯优于配置”(项目中存在大量的配置,此外还内置了一个习惯性的配置,让你无需手动进行配置)的理念让你的项目快速...
主要介绍了Spring Bean的初始化和销毁,结合实例形式详细分析了Spring Bean的初始化和销毁相关配置、使用方法及操作注意事项,需要的朋友可以参考下
Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中获取Spring配置的bean呢?下面通过本文给大家介绍Java中Spring获取bean方法小结,对spring获取bean方法相关知识感兴趣的朋友一起学习...
Spring项目bean基础配置项目代码,bean的配置中主要有bean基础配置,bean的别名配置,bean的作用范围配置。
第十章:Spring 配置元信息小马哥 · mercyblitzSpring 配置元信息Spring 配置元信息Spring Bean 配置元信息Spring
从本质上来说,Spring Boot就是Spring,它做了那些没有它你也会去做的Spring Bean配置。它使用“习惯优于配置”(项目中存在大量的配置,此外还内置了一个习惯性的配置,让你无需手动进行配置)的理念让你的项目快速...