依赖关系配置和处理器

注入其他Bean的属性值


Board.java

package bean;

public class Board {
    private  int id;
    private static String title="java";
    
    
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
}

Topic.java

package bean;

public class Topic {
       private String id;
       private String name;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
       
}


applicationContext.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"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
	<!-- 注入属性值 -->
	<bean id="board" class="bean.Board" autowire="constructor">
        <property name="id">
			<value>00101</value>
		</property>
		<property name="title">
			<value>hibernate</value>
		</property>
	</bean>
 	<bean id="topic" class="bean.Topic">
		<property name="id">

			<!-- 将Board对象的id属性注入到当前id 属性 -->
			<bean id="board.id"
				class="org.springframework.beans.factory.config.PropertyPathFactoryBean" />
		</property>
		<property name="name">
			<bean id="board.title"
				class="org.springframework.beans.factory.config.PropertyPathFactoryBean" />
		</property>
	</bean> 
</beans>

测试代码:

 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
 	    //名字是否注入过来
 	    Topic topic=context.getBean("topic",Topic.class);
 	    System.out.println(topic.getId());
 	    System.out.println(topic.getName());
运行结果:

101   //id自动转化
hibernate



注入其他关系的方法返回值


在Board.java加上

	//注入filed的值
    public String getName(){
    	return  "sql";
    }


要取出sql

把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"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
	<!-- 注入属性值 -->
	<bean id="board" class="bean.Board" autowire="constructor">
        <property name="id">
			<value>00101</value>
		</property>
		<property name="title">
			<value>hibernate</value>
		</property>
	</bean>
	<bean id="topic" class="bean.Topic">
		<property name="id">
			<bean id="board.id"
				class="org.springframework.beans.factory.config.PropertyPathFactoryBean" />
		</property>
		<property name="name">
			<!-- 将bean,Board类的title对象的id属性注入到当前id 属性 -->
			<!--  <bean id="bean.Board.title" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/> -->

		 <bean	class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
				<property name="targetObject" ref="board" />
				<property name="targetMethod" value="getName" />
			</bean> 
		</property>
	</bean>
</beans>

测试代码如上:结果101   sql

Bean后处理器

对容器中的Bean进行后处理,增强Bean的额外功能。
实现BeanPostProcessor接口

MyProcess.java

package process;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import bean.Board;

public class MyProcess implements BeanPostProcessor {
	/**
	 * 执行顺序 postProcessBeforeInitializationboard -》
	 * init-》postProcessAfterInitializationboard
	 */
	@Override
	public Object postProcessAfterInitialization(Object arg0, String arg1)
			throws BeansException {
		System.out.println("postProcessAfterInitialization" + arg1);
		Board board = new Board();
		board.setId(123);
		return board;

	}

	@Override
	public Object postProcessBeforeInitialization(Object arg0, String arg1)
			throws BeansException {
		System.out.println("postProcessBeforeInitialization" + arg1);
		return arg0;
	}

}

在Topic.java中加上声明周期方法

	public void init(){
		System.out.println("int初始化");
	}
	public void destory(){
		System.out.println("destory销毁");
	}

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"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
	<!-- 注入属性值 -->
	<bean id="board" class="bean.Board" autowire="constructor">
        <property name="id">
			<value>00101</value>
		</property>
		<property name="title">
			<value>hibernate</value>
		</property>
	</bean>
	<!-- 处理器 -->
	<!-- 配置生命周期 -->
	 <bean id="topic1" class="bean.Topic" init-method="init" destroy-method="destory" ></bean> 
	 <!-- 加载自定义bean后处理程序  -->
	 <bean class="process.MyProcess"></bean> 



</beans>


测试代码:

	ApplicationContext context = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		// 名字是否注入过来
		Board topic = context.getBean("topic1", Board.class);
		System.out.println(topic.getId());

		// 执行销毁程序
		ClassPathXmlApplicationContext applicationContext = (ClassPathXmlApplicationContext) context;
		applicationContext.close();



运行结果:







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值