注入其他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;
}
}
<?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";
}
把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();
运行结果:
原文链接:https://www.f2er.com/javaschema/285721.html