控制反转(IOC)

朱雀 2022-11-29 12:55 349阅读 0赞

控制反转(IOC)

  1. public class bean1 {
  2. private String name;
  3. private int age;
  4. public String getName() {
  5. return name;
  6. }
  7. public void setName(String name) {
  8. this.name = name;
  9. }
  10. public int getAge() {
  11. return age;
  12. }
  13. public void setAge(int age) {
  14. this.age = age;
  15. }
  16. }
  17. public class student {
  18. private String name;
  19. private List<String> listValue;
  20. private Set<String> setValue;
  21. private Map mapValue;
  22. private Properties proValue;
  23. private bean1 bean1;
  24. public String getName() {
  25. return name;
  26. }
  27. public void setName(String name) {
  28. this.name = name;
  29. }
  30. public List<String> getListValue() {
  31. return listValue;
  32. }
  33. public void setListValue(List<String> listValue) {
  34. this.listValue = listValue;
  35. }
  36. public Set<String> getSetValue() {
  37. return setValue;
  38. }
  39. public void setSetValue(Set<String> setValue) {
  40. this.setValue = setValue;
  41. }
  42. public Map getMapValue() {
  43. return mapValue;
  44. }
  45. public void setMapValue(Map mapValue) {
  46. this.mapValue = mapValue;
  47. }
  48. public Properties getProValue() {
  49. return proValue;
  50. }
  51. public void setProValue(Properties proValue) {
  52. this.proValue = proValue;
  53. }
  54. public bean1 getBean1() {
  55. return bean1;
  56. }
  57. public void setBean1(bean1 bean1) {
  58. this.Bean1 = Bean1;
  59. }
  60. @Override
  61. public String toString() {
  62. return "test{" +
  63. "name='" + name + '\'' +
  64. ", listValue=" + listValue +
  65. ", setValue=" + setValue +
  66. ", mapValue=" + mapValue +
  67. ", proValue=" + proValue +
  68. '}';
  69. }
  70. }

spring帮创建对象

  1. id为唯一标识符,外界通过它来沟通,通常和类名保持一致

applicationCotext.xml

  1. <bean id ="student" class="com.spdb.gr.father">
  2. <property name = "name" value = "郭"><property>
  3. <property name = "listValue">
  4. <list>
  5. <value>list1</value>
  6. <value>list2</value>
  7. </list>
  8. <property>
  9. <property name = "setValue">
  10. <set>
  11. <value>set1</value>
  12. <value>set2</value>
  13. </set>
  14. <property>
  15. <property name = "mapValue">
  16. <map>
  17. <entry key = "key1" value = "value1"></entry>
  18. <entry key = "key2" >
  19. <value>value2</value>
  20. </entry>
  21. </map>
  22. <property>
  23. <--Properties 类的具体使用。以key=value 的 键值对的形式进行存储值。 key值不能重复。-->
  24. <property name = "propValue">
  25. <props>
  26. <prop key = "propKey1">propValue1</prop>
  27. <prop key = "propKey2">propValue2</prop>
  28. </props>
  29. <property>
  30. <--此处做对象的引用-->
  31. <property name = "bean1" ref= "bean1"><property>
  32. </bean>
  33. <bean id ="bean1" class="com.spdb.gr.bean1">
  34. <property name = "name" value = "郭"><property>
  35. <property name = "age" value = "18"><property>
  36. </bean>

注意:先读取,后创建(对象),最后引用(意为如果某一个bean中引用了其他对象,那就在最后一步进行引用(所以不会因为bean1在student之后,导致引用不上!!!))

test(一运行他就能获取applicationCotext.xml文件,加载此文件的时候就会创建bean对象了,可以在构造方法写一个打印语句,可以查看到她显现出来)

  1. public class SingletonDemo {
  2. public SingletonDemo() {
  3. System.out.println("进来了");
  4. }
  5. public void testSpring(){
  6. ApplicationCotext context = new ClassPathXmlApplicationContext("applicationCotext.xml");
  7. //此处为获取bean中的对象,第一个参数为bean中的id,第二个为对象类型
  8. Student student = context.getBean("student",Student.class);
  9. System.out.println("student.baen1.age = "+ student.getBean1.getAge());
  10. System.out.println(student);
  11. }
  12. }

发表评论

表情:
评论列表 (有 0 条评论,349人围观)

还没有评论,来说两句吧...

相关阅读

    相关 什么事控制IOC

    在控制反转之前:程序是主动创建对象的,控制全在程序员手中; 在控制反转之后:使用set注入后,程序不在有主动权,而是变成被动的接受对象。 程序员不用管理对象的创建,系统耦合

    相关 spring—控制IOC

    IOC即为Inversion of Control,中文名称为控制反转。它是一种设计模式,用于降低代码耦合度,提高代码可维护性和可扩展性。 在面向对象编程中,对象之间的依赖关

    相关 spring Ioc(控制)

    简述: spring的核心有两部分:ioc和aop (1)ioc:控制反转,之前调用一个类中的不是静态的方法,创建类的对象 new 类,再调用。现在使用spring的ioc

    相关 Spring框架 IOC控制

    对象之间的依赖由容器建立,避免对象之间依赖由对象自身建立。可以用减少代码之间的耦合度。其中最常见的方式叫做依赖注入。 当A类中用到了B类的对象b,在没有IOC时需要在A的代