<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">
<!-- 按照模块或者层次分类 -->
<import resource="beans.xml"/>
<!--
实现原理
1. 加载xml配置文件,解析文件
2. 读取bean节点的id和class属性值
3. 使用反射技术 Class.forName("类的路径").newInstance();得到对象 默认调用无参构造方法
4. 读取property节点中的属性,调用对象的setXXX()方法进行赋值
5. getBean("id")获取对象
-->
<!--
bean:代表的就是一个javabean对象
id:唯一标识这个实体对象
class:代表的就是实体对象的类
property:代表就是属性
name:当前对象属性名
value:当前对象属性值 普通字符串
ref:属性注入一个已经存在的对象
-->
bean必须设置为scope=singleton 才能执行destory-method中的销毁方法。
<bean id="springstu" class="com.yh.Student" scope="prototype" init-method="init" destroy-method="destory">
<property name="name" value="张三"/>
<property name="age" value="20"/>
</bean>
<!-- 定义一个手机对象 -->
<bean id="springphone" class="com.yh.Phone">
<property name="brand" value="华为"/>
<property name="price" value="3500"/>
</bean>
<bean id="springstu2" class="com.yh.Student">
<property name="name" value="李四"/>
<property name="age" value="10"/>
<property name="phone" ref="springphone"/>
</bean>
<!-- 静态工厂生成一个实例对象 -->
<bean id="staticstu" class="com.yh.MyStaticFactory" factory-method="getStu"/>
<!-- 实例工厂生成实例对象 -->
<!-- 配置实例工厂bean对象 -->
<bean id="mybeanfactory" class="com.yh.MyBeanFactory"/>
<!-- 指定实例工厂对象 生成实例对象stu -->
<bean id="mystu" factory-bean="mybeanfactory" factory-method="getBean"/>
<!-- 1.写一个类实现FactoryBean接口,重写getObject方法 2.配置标准工厂类 -->
<bean id="myStandBeanFactory" class="com.yh.MyStandBeanFactory"/>
</beans>
<bean id="springphone" class="com.yh.Phone">
<property name="brand">
<bean class = "com.yh.Phone.brand"/>
</property>
<property name="price" value="3500"/>
</bean>
<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.xsd">
<!-- 使用setter方法 -->
<bean id="springstu" class="com.yh.Student" init-method="init" destroy-method="destory">
<property name="name" value="张三"/>
<property name="age" value="20"/>
</bean>
<bean id="springphone" class="com.yh.Phone">
<property name="brand" value="华为"/>
<property name="price" value="3500"/>
</bean>
<bean class="com.scope.MyBeanPost"/>
<!-- 使用有参构造方法注入参数值 -->
<bean id="ycStu" class="com.yh.Student">
<constructor-arg index="0" type="String" value="李四"/>
<constructor-arg index="1" type="int" value="10"/>
<constructor-arg index="2" type="com.yh.Phone" ref="springphone"/>
</bean>
<!-- 使用p命名空间 setter方法 -->
<bean id="pstu" class="com.yh.Student" p:name="王五" p:age="30" p:phone-ref="springphone"> </bean>
<!-- 集合注入值 -->
<bean id="user" class="com.scope.User">
<property name="names">
<set>
<value>赵六</value>
<value>tony</value>
</set>
</property>
<property name="phones">
<list>
<ref bean="springphone"/>
<ref bean="springphone"/>
</list>
</property>
<property name="cps">
<map>
<entry key="肖战" value="王一博"> </entry>
<entry key="胡歌" value="霍建华"/>
</map>
</property>
</bean>
</beans>
<bean name="user" class="com.rr.bean.User">
<property name="age" value="20"></property>
</bean>
<bean name="user2" class="com.rr.bean.User">
<constructor-arg name="car" ref="car"></constructor-arg>
</bean>
<bean name="user3" class="com.rr.bean.User" p:name="Jack" p:age="20" p:car-ref="car" ></bean>
<bean name="user4" class="com.rr.bean.User" >
<property name="name" value="#{user.name}"></property>
<property name="age" value="#{user3.age}"></property>
<property name="car" ref="car"></property>
</bean>
<bean id="mqFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<constructor-arg value="tcp://localhost:61616" />
</bean>
<bean id="mqConnection" factory-bean="mqFactory" factory-method="createConnection" init-method="start"/>
等同于
ActiveMQConnectionFactory connectionFactory =
new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = connectionFactory.createConnection();
connection.start();
<div style="display:flex;align-items:flex-start;width:100%;padding-left:2px;">
<div style="margin-right:2px;width:24px;display:flex;align-items:center;justify-content:center;flex-grow:0;flex-shrink:0;min-height:calc((1.5em + 3px) + 3px);">
<span>1.</span>
public class MyBeanFactory {
public Student getBean(){
Student s = new Student();
s.setName("赵六");
s.setAge(30);
return s;
}
}
<!-- 1. 配置实例工厂bean对象 -->
<bean id="mybeanfactory" class="com.yh.MyBeanFactory"/>
<!-- 2. 指定实例工厂对象 生成实例对象stu -->
<bean id="mystu" factory-bean="mybeanfactory" factory-method="getBean"/>
等同于 new MyBeanFactory.getBean();
评论区