依赖关系配置和处理器

前端之家收集整理的这篇文章主要介绍了依赖关系配置和处理器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

注入其他Bean的属性


Board.java

  1. package bean;
  2. public class Board {
  3. private int id;
  4. private static String title="java";
  5. public int getId() {
  6. return id;
  7. }
  8. public void setId(int id) {
  9. this.id = id;
  10. }
  11. public String getTitle() {
  12. return title;
  13. }
  14. public void setTitle(String title) {
  15. this.title = title;
  16. }
  17. }

Topic.java
  1. package bean;
  2. public class Topic {
  3. private String id;
  4. private String name;
  5. public String getId() {
  6. return id;
  7. }
  8. public void setId(String id) {
  9. this.id = id;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. }


applicationContext.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
  6. <!-- 注入属性值 -->
  7. <bean id="board" class="bean.Board" autowire="constructor">
  8. <property name="id">
  9. <value>00101</value>
  10. </property>
  11. <property name="title">
  12. <value>hibernate</value>
  13. </property>
  14. </bean>
  15. <bean id="topic" class="bean.Topic">
  16. <property name="id">
  17. <!-- 将Board对象的id属性注入到当前id 属性 -->
  18. <bean id="board.id"
  19. class="org.springframework.beans.factory.config.PropertyPathfactorybean" />
  20. </property>
  21. <property name="name">
  22. <bean id="board.title"
  23. class="org.springframework.beans.factory.config.PropertyPathfactorybean" />
  24. </property>
  25. </bean>
  26. </beans>

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

101 //id自动转化
hibernate



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


在Board.java加上

  1. //注入filed的值
  2. public String getName(){
  3. return "sql";
  4. }


要取出sql

把XML文件改成

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
  6. <!-- 注入属性值 -->
  7. <bean id="board" class="bean.Board" autowire="constructor">
  8. <property name="id">
  9. <value>00101</value>
  10. </property>
  11. <property name="title">
  12. <value>hibernate</value>
  13. </property>
  14. </bean>
  15. <bean id="topic" class="bean.Topic">
  16. <property name="id">
  17. <bean id="board.id"
  18. class="org.springframework.beans.factory.config.PropertyPathfactorybean" />
  19. </property>
  20. <property name="name">
  21. <!-- 将bean,Board类的title对象的id属性注入到当前id 属性 -->
  22. <!-- <bean id="bean.Board.title" class="org.springframework.beans.factory.config.FieldRetrievingfactorybean"/> -->
  23. <bean class="org.springframework.beans.factory.config.MethodInvokingfactorybean">
  24. <property name="targetObject" ref="board" />
  25. <property name="targetMethod" value="getName" />
  26. </bean>
  27. </property>
  28. </bean>
  29. </beans>

测试代码如上:结果101 sql

Bean后处理器

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

MyProcess.java

  1. package process;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.beans.factory.config.BeanPostProcessor;
  4. import bean.Board;
  5. public class MyProcess implements BeanPostProcessor {
  6. /**
  7. * 执行顺序 postProcessBeforeInitializationboard -》
  8. * init-》postProcessAfterInitializationboard
  9. */
  10. @Override
  11. public Object postProcessAfterInitialization(Object arg0,String arg1)
  12. throws BeansException {
  13. System.out.println("postProcessAfterInitialization" + arg1);
  14. Board board = new Board();
  15. board.setId(123);
  16. return board;
  17. }
  18. @Override
  19. public Object postProcessBeforeInitialization(Object arg0,String arg1)
  20. throws BeansException {
  21. System.out.println("postProcessBeforeInitialization" + arg1);
  22. return arg0;
  23. }
  24. }

在Topic.java中加上声明周期方法
  1. public void init(){
  2. System.out.println("int初始化");
  3. }
  4. public void destory(){
  5. System.out.println("destory销毁");
  6. }

xml文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
  6. <!-- 注入属性值 -->
  7. <bean id="board" class="bean.Board" autowire="constructor">
  8. <property name="id">
  9. <value>00101</value>
  10. </property>
  11. <property name="title">
  12. <value>hibernate</value>
  13. </property>
  14. </bean>
  15. <!-- 处理器 -->
  16. <!-- 配置生命周期 -->
  17. <bean id="topic1" class="bean.Topic" init-method="init" destroy-method="destory" ></bean>
  18. <!-- 加载自定义bean后处理程序 -->
  19. <bean class="process.MyProcess"></bean>
  20. </beans>


测试代码
  1. ApplicationContext context = new ClassPathXmlApplicationContext(
  2. "applicationContext.xml");
  3. // 名字是否注入过来
  4. Board topic = context.getBean("topic1",Board.class);
  5. System.out.println(topic.getId());
  6. // 执行销毁程序
  7. ClassPathXmlApplicationContext applicationContext = (ClassPathXmlApplicationContext) context;
  8. applicationContext.close();



运行结果:


猜你在找的设计模式相关文章