加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

依赖关系配置和处理器

发布时间:2020-12-13 23:12:36 所属栏目:百科 来源:网络整理
导读:注入其他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 vo

注入其他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();



运行结果:


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读